diff --git a/shroom-server/src/store.rs b/shroom-server/src/store.rs index 29f67d4..30b1239 100644 --- a/shroom-server/src/store.rs +++ b/shroom-server/src/store.rs @@ -26,21 +26,25 @@ pub type Error = std::io::Error; pub type Result = std::result::Result; -pub fn open_read_only>(p: P) -> Result>>> { - let data = std::fs::read(p)?; +impl Store>> { + pub fn open_read_only>(p: P) -> Result>>> { + let data = std::fs::read(p)?; - Ok(Store::new(Cursor::new(data))) + Ok(Store::new(Cursor::new(data))) + } } -pub fn open>(p: P) -> Result> { - let mut f = OpenOptions::new() - .read(true) - .write(true) - .create(true) - .open(p)?; - let _ = f.seek(SeekFrom::Start(0))?; +impl Store { + pub fn open>(p: P) -> Result> { + let mut f = OpenOptions::new() + .read(true) + .write(true) + .create(true) + .open(p)?; + let _ = f.seek(SeekFrom::Start(0))?; - Ok(Store::new(f)) + Ok(Store::new(f)) + } } impl Store { @@ -163,7 +167,7 @@ impl Store { #[cfg(test)] mod tests { - use super::Entry; + use super::{Entry, Store}; #[test] 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(); 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); 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(&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); 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(); let entries = store.lookup_range(0, None); 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(); 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); assert_eq!(entries, vec![entry.clone(), entry2.clone()]); } @@ -294,7 +298,7 @@ mod tests { { 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(&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); assert_eq!(entries, vec![entry.clone(), entry2.clone()]); } @@ -342,7 +346,7 @@ mod tests { { 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(&entry2).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(); } { - 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); assert_eq!(entries, vec![entry.clone(), entry2.clone(), entry3.clone()]); } @@ -387,7 +391,7 @@ mod tests { 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(&entry2).unwrap(); store.append(&entry3).unwrap();