Slight ergonomic refactor

This commit is contained in:
Kelvin Ly 2025-02-15 11:17:18 -05:00
parent 7dd74ac36c
commit ed1d6cb43a
1 changed files with 29 additions and 25 deletions

View File

@ -26,13 +26,16 @@ pub type Error = std::io::Error;
pub type Result<T> = std::result::Result<T, Error>; pub type Result<T> = std::result::Result<T, Error>;
pub fn open_read_only<P: AsRef<Path>>(p: P) -> Result<Store<Cursor<Vec<u8>>>> { impl Store<Cursor<Vec<u8>>> {
pub fn open_read_only<P: AsRef<Path>>(p: P) -> Result<Store<Cursor<Vec<u8>>>> {
let data = std::fs::read(p)?; let data = std::fs::read(p)?;
Ok(Store::new(Cursor::new(data))) Ok(Store::new(Cursor::new(data)))
}
} }
pub fn open<P: AsRef<Path>>(p: P) -> Result<Store<File>> { impl Store<File> {
pub fn open<P: AsRef<Path>>(p: P) -> Result<Store<File>> {
let mut f = OpenOptions::new() let mut f = OpenOptions::new()
.read(true) .read(true)
.write(true) .write(true)
@ -41,6 +44,7 @@ pub fn open<P: AsRef<Path>>(p: P) -> Result<Store<File>> {
let _ = f.seek(SeekFrom::Start(0))?; let _ = f.seek(SeekFrom::Start(0))?;
Ok(Store::new(f)) Ok(Store::new(f))
}
} }
impl<T: Read + Seek> Store<T> { impl<T: Read + Seek> Store<T> {
@ -163,7 +167,7 @@ impl<T: Write> Store<T> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::Entry; use super::{Entry, Store};
#[test] #[test]
fn append_read() { fn append_read() {
@ -177,7 +181,7 @@ mod tests {
}; };
{ {
let mut store = super::open(temp_file.path()).unwrap(); let mut store = Store::open(temp_file.path()).unwrap();
store.append(&entry).unwrap(); store.append(&entry).unwrap();
let entries = store.lookup_range(0, None); let entries = store.lookup_range(0, None);
@ -185,7 +189,7 @@ mod tests {
} }
{ {
let store = super::open_read_only(temp_file.path()).unwrap(); let store = Store::open_read_only(temp_file.path()).unwrap();
let entries = store.lookup_range(0, None); let entries = store.lookup_range(0, None);
assert_eq!(entries, vec![entry.clone()]); assert_eq!(entries, vec![entry.clone()]);
} }
@ -211,7 +215,7 @@ mod tests {
}; };
{ {
let mut store = super::open(temp_file.path()).unwrap(); let mut store = Store::open(temp_file.path()).unwrap();
store.append(&entry).unwrap(); store.append(&entry).unwrap();
store.append(&entry2).unwrap(); store.append(&entry2).unwrap();
@ -220,7 +224,7 @@ mod tests {
} }
{ {
let store = super::open_read_only(temp_file.path()).unwrap(); let store = Store::open_read_only(temp_file.path()).unwrap();
let entries = store.lookup_range(0, None); let entries = store.lookup_range(0, None);
assert_eq!(entries, vec![entry.clone(), entry2.clone()]); assert_eq!(entries, vec![entry.clone(), entry2.clone()]);
} }
@ -246,14 +250,14 @@ mod tests {
}; };
{ {
let mut store = super::open(temp_file.path()).unwrap(); let mut store = Store::open(temp_file.path()).unwrap();
store.append(&entry).unwrap(); store.append(&entry).unwrap();
let entries = store.lookup_range(0, None); let entries = store.lookup_range(0, None);
assert_eq!(entries, vec![entry.clone()]); assert_eq!(entries, vec![entry.clone()]);
} }
{ {
let mut store = super::open(temp_file.path()).unwrap(); let mut store = Store::open(temp_file.path()).unwrap();
store.append(&entry2).unwrap(); store.append(&entry2).unwrap();
let entries = store.lookup_range(0, None); let entries = store.lookup_range(0, None);
@ -261,7 +265,7 @@ mod tests {
} }
{ {
let store = super::open_read_only(temp_file.path()).unwrap(); let store = Store::open_read_only(temp_file.path()).unwrap();
let entries = store.lookup_range(0, None); let entries = store.lookup_range(0, None);
assert_eq!(entries, vec![entry.clone(), entry2.clone()]); assert_eq!(entries, vec![entry.clone(), entry2.clone()]);
} }
@ -294,7 +298,7 @@ mod tests {
{ {
use std::io::Write; use std::io::Write;
let mut store = super::open(temp_file.path()).unwrap(); let mut store = Store::open(temp_file.path()).unwrap();
store.append(&entry).unwrap(); store.append(&entry).unwrap();
store.append(&entry2).unwrap(); store.append(&entry2).unwrap();
@ -309,7 +313,7 @@ mod tests {
} }
{ {
let store = super::open_read_only(temp_file.path()).unwrap(); let store = Store::open_read_only(temp_file.path()).unwrap();
let entries = store.lookup_range(0, None); let entries = store.lookup_range(0, None);
assert_eq!(entries, vec![entry.clone(), entry2.clone()]); assert_eq!(entries, vec![entry.clone(), entry2.clone()]);
} }
@ -342,7 +346,7 @@ mod tests {
{ {
use std::io::Write; use std::io::Write;
let mut store = super::open(temp_file.path()).unwrap(); let mut store = Store::open(temp_file.path()).unwrap();
store.append(&entry).unwrap(); store.append(&entry).unwrap();
store.append(&entry2).unwrap(); store.append(&entry2).unwrap();
store.contents.write(&[45, 34, 44]).unwrap(); store.contents.write(&[45, 34, 44]).unwrap();
@ -351,12 +355,12 @@ mod tests {
} }
{ {
let mut store = super::open(temp_file.path()).unwrap(); let mut store = Store::open(temp_file.path()).unwrap();
store.append(&entry3).unwrap(); store.append(&entry3).unwrap();
} }
{ {
let store = super::open_read_only(temp_file.path()).unwrap(); let store = Store::open_read_only(temp_file.path()).unwrap();
let entries = store.lookup_range(0, None); let entries = store.lookup_range(0, None);
assert_eq!(entries, vec![entry.clone(), entry2.clone(), entry3.clone()]); assert_eq!(entries, vec![entry.clone(), entry2.clone(), entry3.clone()]);
} }
@ -387,7 +391,7 @@ mod tests {
humidifer: vec![true], humidifer: vec![true],
}; };
let mut store = super::open(temp_file.path()).unwrap(); let mut store = Store::open(temp_file.path()).unwrap();
store.append(&entry).unwrap(); store.append(&entry).unwrap();
store.append(&entry2).unwrap(); store.append(&entry2).unwrap();
store.append(&entry3).unwrap(); store.append(&entry3).unwrap();