diff --git a/crates/errors/Cargo.toml b/crates/errors/Cargo.toml new file mode 100644 index 0000000..926ecdd --- /dev/null +++ b/crates/errors/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "errors" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/crates/errors/src/lib.rs b/crates/errors/src/lib.rs new file mode 100644 index 0000000..7d12d9a --- /dev/null +++ b/crates/errors/src/lib.rs @@ -0,0 +1,14 @@ +pub fn add(left: usize, right: usize) -> usize { + left + right +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_works() { + let result = add(2, 2); + assert_eq!(result, 4); + } +} diff --git a/crates/squozen/src/squozen.rs b/crates/squozen/src/squozen.rs index a8d2f34..5608128 100644 --- a/crates/squozen/src/squozen.rs +++ b/crates/squozen/src/squozen.rs @@ -1,6 +1,7 @@ +use crate::prepare_pattern::prepare_pattern; use libc; use std::fs::File; -use std::io::{Bufreader, Bytes}; +use std::io::{BufReader, Bytes}; use std::path::Path; const PARITY: u8 = 0o200; @@ -14,10 +15,10 @@ pub struct Squozen { path: Path, } -pub impl Squozen { +impl Squozen { pub fn new(filename: Path) -> Result { let mut dbfile = File::open(filename)?; - let mut db = Bufreader::new(dbfile); + let mut db = BufReader::::new(dbfile); let mut bigrams: [char; BIGRAMS] = [0; BIGRAMS]; db.read_exact(&mut bigrams)?; Ok(Squozen { @@ -40,16 +41,16 @@ pub impl Squozen { pub struct StoredPath<'a> { source: &'a Squozen, path: [char; libc::PATH_MAX], - db: Bytes, + db: Bytes>, ch: u8, last: usize, found: bool, } -pub impl<'a> StoredPath { +impl<'a> StoredPath<'a> { pub fn new(squozen: &'a mut Squozen) -> StoredPath { let mut dbfile = File::open(&squozen.path)?; - let mut dbbuffer = Bufreader::new(dbfile); + let mut dbbuffer = BufReader::::new(dbfile); dbbuffer.seek(BIGRAMS); let mut db = dbbuffer.bytes(); let mut ch = db.next()?; @@ -80,8 +81,8 @@ pub impl<'a> StoredPath { } } -pub impl<'a> Iterator for StoredPath { - type Item = &[char; libc::PATH_MAX]; +impl<'a> Iterator for StoredPath<'a> { + type Item = &'a [char; libc::PATH_MAX]; fn next(&mut self) -> Option<&Self::Item> { let offset = self.get_offset(); @@ -92,7 +93,7 @@ pub impl<'a> Iterator for StoredPath { break; } if self.ch < PARITY { - self.path[self.last + position] = ch; + self.path[self.last + position] = self.ch; position += 1; } else { let bg = self.ch & PARITY - 1; @@ -105,16 +106,24 @@ pub impl<'a> Iterator for StoredPath { } } -pub struct FoundPath { - stored: StoredPath, +pub struct FoundPath<'a> { + stored: StoredPath<'a>, pattern: Vec, } -pub impl<'a> FoundPath { - pub fn new(squozen: &'a Squozen, path: &[u8]) -> Result { +impl<'a> FoundPath<'a> { + pub fn new(squozen: &'a Squozen, path: &[u8]) -> Result, Error> { Ok(FoundPath { stored: StoredPath::new(squozen), pattern: prepare_pattern(path)?, }) } } + +impl<'a> Iterator for FoundPath<'a> { + type Item = &'a [char; libc::PATH_MAX]; + + fn next(&mut self) -> Option<&Self::Item> { + self.stored.next() + } +}