Skip to content

Commit

Permalink
Make file_storage generic
Browse files Browse the repository at this point in the history
  • Loading branch information
twitu committed Feb 21, 2024
1 parent 654a7f9 commit 29b71c7
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 170 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ env_logger = "0.9.0"
serde_json = "1.0.82"
serde = { version = "1.0.138", features = ["derive"] }
url = { version = "2.2.2", features= ["serde"] }
thiserror = "1.0.57"
162 changes: 0 additions & 162 deletions src/id.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub mod errors;
pub use errors::{ArklibError, Result};

pub mod id;
pub mod storage;
16 changes: 9 additions & 7 deletions src/storage/file_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use std::{
path::{Path, PathBuf},
};

use crate::id::ResourceId;
use crate::{ArklibError, Result};

const LOG_PREFIX: &str = "[file-storage]";
Expand Down Expand Up @@ -37,14 +36,16 @@ impl FileStorage {
/// Read data from disk
///
/// Data is read as a key value pairs separated by a symbol and stored
/// in a [HashMap] with [ResourceId] key and a generic V value. A handler
/// in a [HashMap] with a generic key K and V value. A handler
/// is called on the data after reading it.
pub fn read_from_disk<V>(
pub fn read_from_disk<K, V>(
&self,
handle: impl FnOnce(HashMap<ResourceId, V>),
handle: impl FnOnce(HashMap<K, V>),
) -> Result<()>
where
K: FromStr + std::hash::Hash + std::cmp::Eq,
V: FromStr,
ArklibError: From<<K as FromStr>::Err>,
ArklibError: From<<V as FromStr>::Err>,
{
let new_timestamp = fs::metadata(&self.path)?.modified()?;
Expand Down Expand Up @@ -77,7 +78,7 @@ impl FileStorage {

let parts: Vec<&str> =
line.split(KEY_VALUE_SEPARATOR).collect();
let id = ResourceId::from_str(parts[0])?;
let id = K::from_str(parts[0])?;
let value = V::from_str(parts[1])?;

// TODO: neutral value check when introducing monoids
Expand All @@ -99,11 +100,12 @@ impl FileStorage {
/// Write data to file
///
/// Data is a key-value mapping between [ResourceId] and a generic Value
pub fn write_to_disk<V>(
pub fn write_to_disk<K, V>(
&mut self,
value_by_id: &HashMap<ResourceId, V>,
value_by_id: &HashMap<K, V>,
) -> Result<()>
where
K: Display,
V: Display,
{
fs::create_dir_all(self.path.parent().unwrap())?;
Expand Down

0 comments on commit 29b71c7

Please sign in to comment.