38 lines
1015 B
Rust
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(())
|
||
|
}
|
||
|
}
|