diff --git a/src/io/storage.rs b/src/io/storage.rs index 86430b3..10a7eb3 100644 --- a/src/io/storage.rs +++ b/src/io/storage.rs @@ -1,11 +1,11 @@ use std::{ fs::File, - io::{BufWriter, Write}, + io::{BufReader, BufWriter, Write}, path::Path, }; use anyhow::{Context, Result}; -use serde::Serialize; +use serde::{de::DeserializeOwned, Serialize}; pub fn write(path: &Path, value: &T) -> Result<()> { (|| -> Result<()> { @@ -20,3 +20,13 @@ pub fn write(path: &Path, value: &T) -> Result<()> { })() .with_context(|| format!("Failed to write file {}", path.display())) } + +pub fn read(path: &Path) -> Result { + (|| -> Result { + let file = File::open(path)?; + let reader = BufReader::new(file); + let decompressor = zstd::Decoder::new(reader)?; + Ok(bincode::deserialize_from(decompressor)?) + })() + .with_context(|| format!("Failed to read file {}", path.display())) +}