Adding an Errors block.
This commit is contained in:
parent
a01fcbee68
commit
e624aea369
|
@ -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]
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
|
use crate::prepare_pattern::prepare_pattern;
|
||||||
use libc;
|
use libc;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{Bufreader, Bytes};
|
use std::io::{BufReader, Bytes};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
const PARITY: u8 = 0o200;
|
const PARITY: u8 = 0o200;
|
||||||
|
@ -14,10 +15,10 @@ pub struct Squozen {
|
||||||
path: Path,
|
path: Path,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub impl Squozen {
|
impl Squozen {
|
||||||
pub fn new(filename: Path) -> Result<Squozen, Error> {
|
pub fn new(filename: Path) -> Result<Squozen, Error> {
|
||||||
let mut dbfile = File::open(filename)?;
|
let mut dbfile = File::open(filename)?;
|
||||||
let mut db = Bufreader::new(dbfile);
|
let mut db = BufReader::<File>::new(dbfile);
|
||||||
let mut bigrams: [char; BIGRAMS] = [0; BIGRAMS];
|
let mut bigrams: [char; BIGRAMS] = [0; BIGRAMS];
|
||||||
db.read_exact(&mut bigrams)?;
|
db.read_exact(&mut bigrams)?;
|
||||||
Ok(Squozen {
|
Ok(Squozen {
|
||||||
|
@ -40,16 +41,16 @@ pub impl Squozen {
|
||||||
pub struct StoredPath<'a> {
|
pub struct StoredPath<'a> {
|
||||||
source: &'a Squozen,
|
source: &'a Squozen,
|
||||||
path: [char; libc::PATH_MAX],
|
path: [char; libc::PATH_MAX],
|
||||||
db: Bytes<Bufreader>,
|
db: Bytes<BufReader<File>>,
|
||||||
ch: u8,
|
ch: u8,
|
||||||
last: usize,
|
last: usize,
|
||||||
found: bool,
|
found: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub impl<'a> StoredPath {
|
impl<'a> StoredPath<'a> {
|
||||||
pub fn new(squozen: &'a mut Squozen) -> StoredPath {
|
pub fn new(squozen: &'a mut Squozen) -> StoredPath {
|
||||||
let mut dbfile = File::open(&squozen.path)?;
|
let mut dbfile = File::open(&squozen.path)?;
|
||||||
let mut dbbuffer = Bufreader::new(dbfile);
|
let mut dbbuffer = BufReader::<File>::new(dbfile);
|
||||||
dbbuffer.seek(BIGRAMS);
|
dbbuffer.seek(BIGRAMS);
|
||||||
let mut db = dbbuffer.bytes();
|
let mut db = dbbuffer.bytes();
|
||||||
let mut ch = db.next()?;
|
let mut ch = db.next()?;
|
||||||
|
@ -80,8 +81,8 @@ pub impl<'a> StoredPath {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub impl<'a> Iterator for StoredPath {
|
impl<'a> Iterator for StoredPath<'a> {
|
||||||
type Item = &[char; libc::PATH_MAX];
|
type Item = &'a [char; libc::PATH_MAX];
|
||||||
|
|
||||||
fn next(&mut self) -> Option<&Self::Item> {
|
fn next(&mut self) -> Option<&Self::Item> {
|
||||||
let offset = self.get_offset();
|
let offset = self.get_offset();
|
||||||
|
@ -92,7 +93,7 @@ pub impl<'a> Iterator for StoredPath {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if self.ch < PARITY {
|
if self.ch < PARITY {
|
||||||
self.path[self.last + position] = ch;
|
self.path[self.last + position] = self.ch;
|
||||||
position += 1;
|
position += 1;
|
||||||
} else {
|
} else {
|
||||||
let bg = self.ch & PARITY - 1;
|
let bg = self.ch & PARITY - 1;
|
||||||
|
@ -105,16 +106,24 @@ pub impl<'a> Iterator for StoredPath {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct FoundPath {
|
pub struct FoundPath<'a> {
|
||||||
stored: StoredPath,
|
stored: StoredPath<'a>,
|
||||||
pattern: Vec<u8>,
|
pattern: Vec<u8>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub impl<'a> FoundPath {
|
impl<'a> FoundPath<'a> {
|
||||||
pub fn new(squozen: &'a Squozen, path: &[u8]) -> Result<FoundPath, Error> {
|
pub fn new(squozen: &'a Squozen, path: &[u8]) -> Result<FoundPath<'a>, Error> {
|
||||||
Ok(FoundPath {
|
Ok(FoundPath {
|
||||||
stored: StoredPath::new(squozen),
|
stored: StoredPath::new(squozen),
|
||||||
pattern: prepare_pattern(path)?,
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue