use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;
use serde_json::Value;
pub trait StateStore: Send + Sync {
fn get(&self, ns: &str, key: &str) -> Result<Option<Value>, String>;
fn set(&self, ns: &str, key: &str, value: Value) -> Result<(), String>;
fn delete(&self, ns: &str, key: &str) -> Result<bool, String>;
fn keys(&self, ns: &str) -> Result<Vec<String>, String>;
fn has(&self, ns: &str, key: &str) -> Result<bool, String>;
fn set_nx(&self, ns: &str, key: &str, value: Value) -> Result<bool, String>;
fn incr(&self, ns: &str, key: &str, delta: f64, default: f64) -> Result<f64, String>;
}
pub struct JsonFileStore;
impl JsonFileStore {
fn state_dir() -> Result<PathBuf, String> {
let home = dirs::home_dir().ok_or("Cannot determine home directory")?;
let dir = home.join(".algocline").join("state");
if !dir.exists() {
fs::create_dir_all(&dir).map_err(|e| format!("Failed to create state dir: {e}"))?;
}
Ok(dir)
}
fn state_path(ns: &str) -> Result<PathBuf, String> {
if ns.contains('/')
|| ns.contains('\\')
|| ns.contains("..")
|| ns.contains('\0')
|| ns.is_empty()
{
return Err(format!("Invalid namespace: '{ns}'"));
}
Ok(Self::state_dir()?.join(format!("{ns}.json")))
}
fn load(ns: &str) -> Result<HashMap<String, Value>, String> {
let path = Self::state_path(ns)?;
if !path.exists() {
return Ok(HashMap::new());
}
let content =
fs::read_to_string(&path).map_err(|e| format!("Failed to read state '{ns}': {e}"))?;
serde_json::from_str(&content).map_err(|e| format!("Failed to parse state '{ns}': {e}"))
}
fn save(ns: &str, data: &HashMap<String, Value>) -> Result<(), String> {
let path = Self::state_path(ns)?;
let tmp = path.with_extension("json.tmp");
let content = serde_json::to_string_pretty(data)
.map_err(|e| format!("Failed to serialize state: {e}"))?;
fs::write(&tmp, &content).map_err(|e| format!("Failed to write state tmp: {e}"))?;
fs::rename(&tmp, &path).map_err(|e| format!("Failed to rename state file: {e}"))?;
Ok(())
}
}
impl StateStore for JsonFileStore {
fn get(&self, ns: &str, key: &str) -> Result<Option<Value>, String> {
let state = Self::load(ns)?;
Ok(state.get(key).cloned())
}
fn set(&self, ns: &str, key: &str, value: Value) -> Result<(), String> {
let mut state = Self::load(ns)?;
state.insert(key.to_string(), value);
Self::save(ns, &state)
}
fn delete(&self, ns: &str, key: &str) -> Result<bool, String> {
let mut state = Self::load(ns)?;
let existed = state.remove(key).is_some();
if existed {
Self::save(ns, &state)?;
}
Ok(existed)
}
fn keys(&self, ns: &str) -> Result<Vec<String>, String> {
let state = Self::load(ns)?;
Ok(state.keys().cloned().collect())
}
fn has(&self, ns: &str, key: &str) -> Result<bool, String> {
let state = Self::load(ns)?;
Ok(state.contains_key(key))
}
fn set_nx(&self, ns: &str, key: &str, value: Value) -> Result<bool, String> {
let mut state = Self::load(ns)?;
if state.contains_key(key) {
return Ok(false);
}
state.insert(key.to_string(), value);
Self::save(ns, &state)?;
Ok(true)
}
fn incr(&self, ns: &str, key: &str, delta: f64, default: f64) -> Result<f64, String> {
let mut state = Self::load(ns)?;
let current = match state.get(key) {
Some(v) => v
.as_f64()
.ok_or_else(|| format!("incr: value at '{key}' is not a number"))?,
None => default,
};
let new_val = current + delta;
state.insert(key.to_string(), serde_json::json!(new_val));
Self::save(ns, &state)?;
Ok(new_val)
}
}
static STORE: JsonFileStore = JsonFileStore;
pub fn get(ns: &str, key: &str) -> Result<Option<Value>, String> {
STORE.get(ns, key)
}
pub fn set(ns: &str, key: &str, value: Value) -> Result<(), String> {
STORE.set(ns, key, value)
}
pub fn delete(ns: &str, key: &str) -> Result<bool, String> {
STORE.delete(ns, key)
}
pub fn keys(ns: &str) -> Result<Vec<String>, String> {
STORE.keys(ns)
}
pub fn has(ns: &str, key: &str) -> Result<bool, String> {
STORE.has(ns, key)
}
pub fn set_nx(ns: &str, key: &str, value: Value) -> Result<bool, String> {
STORE.set_nx(ns, key, value)
}
pub fn incr(ns: &str, key: &str, delta: f64, default: f64) -> Result<f64, String> {
STORE.incr(ns, key, delta, default)
}
#[cfg(test)]
mod tests {
use super::*;
fn cleanup(ns: &str) {
let _ = std::fs::remove_file(JsonFileStore::state_path(ns).unwrap());
}
struct CleanupGuard(&'static str);
impl Drop for CleanupGuard {
fn drop(&mut self) {
let _ = std::fs::remove_file(JsonFileStore::state_path(self.0).unwrap());
}
}
#[test]
fn roundtrip() {
let ns = "_test_roundtrip";
cleanup(ns);
let _g = CleanupGuard(ns);
set(ns, "count", serde_json::json!(42)).unwrap();
set(ns, "name", serde_json::json!("algocline")).unwrap();
assert_eq!(get(ns, "count").unwrap(), Some(serde_json::json!(42)));
assert_eq!(
get(ns, "name").unwrap(),
Some(serde_json::json!("algocline"))
);
assert_eq!(get(ns, "missing").unwrap(), None);
let k = keys(ns).unwrap();
assert!(k.contains(&"count".to_string()));
assert!(k.contains(&"name".to_string()));
assert!(delete(ns, "count").unwrap());
assert!(!delete(ns, "count").unwrap());
assert_eq!(get(ns, "count").unwrap(), None);
}
#[test]
fn invalid_namespace() {
assert!(JsonFileStore::state_path("../evil").is_err());
assert!(JsonFileStore::state_path("foo/bar").is_err());
assert!(JsonFileStore::state_path("foo\\bar").is_err());
assert!(JsonFileStore::state_path("").is_err());
assert!(JsonFileStore::state_path("foo\0bar").is_err());
}
#[test]
fn get_nonexistent_namespace_returns_empty() {
let result = get("_test_nonexistent_ns_12345", "any_key").unwrap();
assert_eq!(result, None);
}
#[test]
fn keys_nonexistent_namespace_returns_empty() {
let result = keys("_test_nonexistent_ns_12345").unwrap();
assert!(result.is_empty());
}
#[test]
fn delete_nonexistent_key_returns_false() {
let ns = "_test_delete_nonexistent";
cleanup(ns);
assert!(!delete(ns, "nope").unwrap());
}
#[test]
fn set_overwrites_existing_value() {
let ns = "_test_overwrite";
cleanup(ns);
let _g = CleanupGuard(ns);
set(ns, "k", serde_json::json!(1)).unwrap();
set(ns, "k", serde_json::json!(2)).unwrap();
assert_eq!(get(ns, "k").unwrap(), Some(serde_json::json!(2)));
}
#[test]
fn state_path_valid_namespaces() {
assert!(JsonFileStore::state_path("default").is_ok());
assert!(JsonFileStore::state_path("my-app").is_ok());
assert!(JsonFileStore::state_path("test_123").is_ok());
}
#[test]
fn has_returns_existence() {
let ns = "_test_has";
cleanup(ns);
let _g = CleanupGuard(ns);
assert!(!has(ns, "x").unwrap());
set(ns, "x", serde_json::json!(1)).unwrap();
assert!(has(ns, "x").unwrap());
}
#[test]
fn set_nx_only_sets_if_absent() {
let ns = "_test_set_nx";
cleanup(ns);
let _g = CleanupGuard(ns);
assert!(set_nx(ns, "k", serde_json::json!("first")).unwrap());
assert!(!set_nx(ns, "k", serde_json::json!("second")).unwrap());
assert_eq!(
get(ns, "k").unwrap(),
Some(serde_json::json!("first")),
"set_nx should not overwrite"
);
}
#[test]
fn incr_initialises_and_increments() {
let ns = "_test_incr";
cleanup(ns);
let _g = CleanupGuard(ns);
let v = incr(ns, "counter", 1.0, 0.0).unwrap();
assert!((v - 1.0).abs() < f64::EPSILON);
let v = incr(ns, "counter", 5.0, 0.0).unwrap();
assert!((v - 6.0).abs() < f64::EPSILON);
let v = incr(ns, "counter", -2.0, 0.0).unwrap();
assert!((v - 4.0).abs() < f64::EPSILON);
}
#[test]
fn incr_rejects_non_numeric() {
let ns = "_test_incr_err";
cleanup(ns);
let _g = CleanupGuard(ns);
set(ns, "s", serde_json::json!("hello")).unwrap();
let err = incr(ns, "s", 1.0, 0.0).unwrap_err();
assert!(err.contains("not a number"), "got: {err}");
}
#[test]
fn incr_custom_default() {
let ns = "_test_incr_default";
cleanup(ns);
let _g = CleanupGuard(ns);
let v = incr(ns, "score", 10.0, 100.0).unwrap();
assert!((v - 110.0).abs() < f64::EPSILON, "100 + 10 = 110");
}
}
#[cfg(test)]
mod proptests {
use super::*;
use proptest::prelude::*;
proptest! {
#[test]
fn roundtrip_arbitrary_values(
key in "[a-z]{1,20}",
val in any::<i64>(),
) {
let ns = "_proptest_rt";
let json_val = serde_json::json!(val);
set(ns, &key, json_val.clone()).unwrap();
let got = get(ns, &key).unwrap();
prop_assert_eq!(got, Some(json_val));
let _ = delete(ns, &key);
}
#[test]
fn traversal_always_rejected(
prefix in "[a-z]{0,5}",
suffix in "[a-z]{0,5}",
) {
let evil = format!("{prefix}/../{suffix}");
prop_assert!(JsonFileStore::state_path(&evil).is_err());
}
#[test]
fn nul_byte_always_rejected(
prefix in "[a-z]{0,10}",
suffix in "[a-z]{0,10}",
) {
let evil = format!("{prefix}\0{suffix}");
prop_assert!(JsonFileStore::state_path(&evil).is_err());
}
}
}