Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs-storage: Port BaseStorage abstraction from Kotlin #23

Merged
merged 11 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 16 additions & 19 deletions fs-storage/examples/cli.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::{Context, Result};
use fs_storage::base_storage::BaseStorage;
use fs_storage::file_storage::FileStorage;
use serde_json::Value;
use std::collections::BTreeMap;
use std::env;
use std::fs;
use std::path::Path;
Expand Down Expand Up @@ -43,21 +43,22 @@ fn read_command(args: &[String], path: &str) -> Result<()> {
vec![]
};

let mut fs = FileStorage::new("cli".to_string(), Path::new(path));
let map: BTreeMap<String, String> =
fs.read_file().context("Failed to read file")?;
let mut fs: FileStorage<String, String> =
FileStorage::new("cli".to_string(), Path::new(path));

let map = fs
.read_fs()
.expect("No Data is present on this path");
if keys.is_empty() {
for (key, value) in map {
println!("{}: {}", key, value);
}
} else {
for key in &keys {
if let Some(value) = map.get(key) {
println!("{}: {}", key, value);
} else {
eprintln!("Key '{}' not found", key);
}
}
for key in &keys {
if let Some(value) = fs.get(key) {
println!("{}: {}", key, value);
} else {
eprintln!("Key '{}' not found", key);
}
}

Expand All @@ -76,7 +77,8 @@ fn write_command(args: &[String], path: &str) -> Result<()> {
.extension()
.map_or(false, |ext| ext == "json");

let mut kv_pairs = BTreeMap::new();
let mut fs: FileStorage<String, String> =
FileStorage::new("cli".to_string(), Path::new(path));
if content_json {
let content =
fs::read_to_string(content).context("Failed to read JSON file")?;
Expand All @@ -85,7 +87,7 @@ fn write_command(args: &[String], path: &str) -> Result<()> {
if let Value::Object(object) = json {
for (key, value) in object {
if let Value::String(value_str) = value {
kv_pairs.insert(key, value_str);
fs.set(key, value_str);
} else {
println!(
"Warning: Skipping non-string value for key '{}'",
Expand All @@ -102,14 +104,9 @@ fn write_command(args: &[String], path: &str) -> Result<()> {
for pair in pairs {
let kv: Vec<&str> = pair.split(':').collect();
if kv.len() == 2 {
kv_pairs.insert(kv[0].to_string(), kv[1].to_string());
fs.set(kv[0].to_string(), kv[1].to_string());
}
}
}

let mut fs = FileStorage::new("cli".to_string(), Path::new(path));
fs.write_file(&kv_pairs)
.context("Failed to write file")?;

Ok(())
}
36 changes: 36 additions & 0 deletions fs-storage/src/base_storage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use std::collections::BTreeMap;
use std::fmt::Debug;
use std::hash::Hash;
use std::str::FromStr;

use data_error::Result;

pub trait BaseStorage<K, V>: AsRef<BTreeMap<K, V>>
where
K: FromStr + Hash + Eq + Ord + Debug + Clone,
V: Debug + Clone,
{
fn get(&self, id: &K) -> Option<&V>;
tareknaser marked this conversation as resolved.
Show resolved Hide resolved
fn set(&mut self, id: K, value: V);
tareknaser marked this conversation as resolved.
Show resolved Hide resolved
fn remove(&mut self, id: &K) -> Result<()>;

/// Remove file at stored path
fn erase(&self) -> Result<()>;

/// Check if storage is updated
///
/// This check can be used before reading the file.
fn is_storage_updated(&self) -> Result<bool>;

/// Read data from disk
///
/// Data is read as key value pairs separated by a symbol and stored
/// in a [BTreeMap] with a generic key K and V value. A handler
/// is called on the data after reading it.
fn read_fs(&mut self) -> Result<BTreeMap<K, V>>;

/// Write data to file
///
/// Data is a key-value mapping between [ResourceId] and a generic Value
fn write_fs(&mut self) -> Result<()>;
}
Loading
Loading