domani/src/token.rs
2023-07-12 19:01:31 +02:00

38 lines
1015 B
Rust

/// Provides utilites for storing and retrieving various named string tokens which are needed.
use crate::error::unexpected;
use std::{collections, sync};
pub trait Store {
fn get(&self, key: &str) -> unexpected::Result<Option<String>>;
fn set(&self, key: String, val: String) -> unexpected::Result<()>;
fn del(&self, key: &str) -> unexpected::Result<()>;
}
pub struct MemStore {
m: sync::Mutex<collections::HashMap<String, String>>,
}
impl MemStore {
pub fn new() -> MemStore {
MemStore {
m: sync::Mutex::default(),
}
}
}
impl Store for MemStore {
fn get(&self, key: &str) -> unexpected::Result<Option<String>> {
Ok(self.m.lock().unwrap().get(key).map(|s| s.to_string()))
}
fn set(&self, key: String, val: String) -> unexpected::Result<()> {
self.m.lock().unwrap().insert(key, val);
Ok(())
}
fn del(&self, key: &str) -> unexpected::Result<()> {
self.m.lock().unwrap().remove(key);
Ok(())
}
}