huskies: merge 858
This commit is contained in:
@@ -5,6 +5,7 @@ use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::Mutex;
|
||||
|
||||
/// Trait for a simple key-value store that can get, set, delete, and persist entries.
|
||||
pub trait StoreOps: Send + Sync {
|
||||
fn get(&self, key: &str) -> Option<Value>;
|
||||
fn set(&self, key: &str, value: Value);
|
||||
@@ -12,12 +13,14 @@ pub trait StoreOps: Send + Sync {
|
||||
fn save(&self) -> Result<(), String>;
|
||||
}
|
||||
|
||||
/// A JSON-backed file store that persists key-value data to a single file on disk.
|
||||
pub struct JsonFileStore {
|
||||
path: PathBuf,
|
||||
data: Mutex<HashMap<String, Value>>,
|
||||
}
|
||||
|
||||
impl JsonFileStore {
|
||||
/// Create a new store backed by `path`, loading existing data if the file is present.
|
||||
pub fn new(path: PathBuf) -> Result<Self, String> {
|
||||
let data = if path.exists() {
|
||||
let content =
|
||||
@@ -38,10 +41,12 @@ impl JsonFileStore {
|
||||
})
|
||||
}
|
||||
|
||||
/// Convenience constructor accepting any path type; delegates to [`Self::new`].
|
||||
pub fn from_path<P: AsRef<Path>>(path: P) -> Result<Self, String> {
|
||||
Self::new(path.as_ref().to_path_buf())
|
||||
}
|
||||
|
||||
/// Return the path to the backing JSON file.
|
||||
#[allow(dead_code)]
|
||||
pub fn path(&self) -> &Path {
|
||||
&self.path
|
||||
|
||||
Reference in New Issue
Block a user