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,21 +26,25 @@ pub type Error = std::io::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>>>> {
let data = std::fs::read(p)?;
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)?;
Ok(Store::new(Cursor::new(data)))
Ok(Store::new(Cursor::new(data)))
}
}
pub fn open<P: AsRef<Path>>(p: P) -> Result<Store<File>> {
let mut f = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(p)?;
let _ = f.seek(SeekFrom::Start(0))?;
impl Store<File> {
pub fn open<P: AsRef<Path>>(p: P) -> Result<Store<File>> {
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<T: Read + Seek> Store<T> {
@ -163,7 +167,7 @@ impl<T: Write> Store<T> {
#[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();