/*
* This file is part of espanso.
*
* Copyright (C) 2019-2021 Federico Terzi
*
* espanso is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* espanso is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with espanso. If not, see .
*/
use anyhow::Result;
use serde_json::Value;
use std::{
collections::HashMap,
path::{Path, PathBuf},
sync::{Arc, Mutex},
};
use thiserror::Error;
use super::KVS;
const DEFAULT_KVS_DIR_NAME: &str = "kvs";
#[derive(Clone)]
pub struct PersistentJsonKVS {
kvs_dir: PathBuf,
store: Arc>>,
}
impl PersistentJsonKVS {
pub fn new(base_dir: &Path) -> Result {
let kvs_dir = base_dir.join(DEFAULT_KVS_DIR_NAME);
if !kvs_dir.is_dir() {
std::fs::create_dir_all(&kvs_dir)?;
}
Ok(Self {
kvs_dir,
store: Arc::new(Mutex::new(HashMap::new())),
})
}
}
impl KVS for PersistentJsonKVS {
fn get(&self, key: &str) -> Result