Skip to content

Commit

Permalink
initial rust source code
Browse files Browse the repository at this point in the history
  • Loading branch information
d33p0st committed Sep 12, 2024
1 parent 420e445 commit 1d15ff6
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
mod scan;

use pyo3::prelude::*;

#[pymodule]
#[pyo3(name = "rlib")]
fn rlib(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<scan::Scanner>()?;
Ok(())
}
58 changes: 58 additions & 0 deletions src/scan.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use pyo3::prelude::*;
use rustypath::RPath;


#[pyclass]
pub struct Scanner{
directory: RPath,
contents: Vec<RPath>,
}

#[pymethods]
impl Scanner {
#[new]
pub fn new(directory: &str) -> PyResult<Self> {
Ok(Self {
directory: RPath::from(directory),
contents: Vec::new(),
})
}

pub fn scan(&mut self) -> PyResult<()> {
let mut content: Vec<RPath> = Vec::new();
let mut dirs: Vec<RPath> = vec![self.directory.clone()];

while let Some(current_dir) = dirs.pop() {
for entry in current_dir.read_dir()? {
let entry = entry?;
let path = entry.path();

if path.is_dir() {
dirs.push(RPath::from(path));
} else {
content.push(RPath::from(path));
}
}
}

self.contents = content;

Ok(())
}

pub fn files(&self) -> PyResult<Vec<RPath>> {
Ok(self.contents.clone())
}

pub fn isolate(&mut self, r#type: &str) -> PyResult<Vec<RPath>> {
let mut isolated: Vec<RPath> = Vec::new();

for file in &self.contents {
if file.if_extension(r#type) {
isolated.push(file.to_owned());
}
}

Ok(isolated)
}
}

0 comments on commit 1d15ff6

Please sign in to comment.