2020-01-06 16:32:56 +00:00
|
|
|
//! A storage container for client data
|
|
|
|
|
|
|
|
#[allow(unused_imports)]
|
|
|
|
use log::{debug, error, info, trace, warn};
|
|
|
|
|
|
|
|
use quick_error::quick_error;
|
|
|
|
|
|
|
|
quick_error! {
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum CofferError {
|
2020-01-08 22:03:49 +00:00
|
|
|
Msg(err: &'static str) {
|
|
|
|
from(err)
|
|
|
|
display("{}", err)
|
|
|
|
}
|
|
|
|
Other(err: Box<dyn std::error::Error>) {
|
|
|
|
cause(&**err)
|
|
|
|
}
|
2020-01-06 16:32:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type CofferResult<T> = Result<T, CofferError>;
|
|
|
|
|
|
|
|
/// Values supported by a `Coffer`
|
2020-01-08 22:03:49 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2020-01-06 16:32:56 +00:00
|
|
|
pub enum CofferValue {
|
|
|
|
/// A UTF-8 encoded string
|
|
|
|
String(String),
|
|
|
|
/// A 32-bit integer
|
|
|
|
Integer(i32),
|
|
|
|
/// An opaque blob of data
|
|
|
|
Blob(Vec<u8>)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A path to a value
|
2020-01-08 22:03:49 +00:00
|
|
|
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
|
2020-01-06 16:32:56 +00:00
|
|
|
pub struct CofferPath(Vec<String>);
|
|
|
|
|
|
|
|
/// Interface for interacting with a `Coffer`
|
|
|
|
pub trait Coffer {
|
|
|
|
/// Put `value` at `path`. Errors if there is already a value at `path`.
|
2020-01-08 22:03:49 +00:00
|
|
|
fn put(&mut self, path: CofferPath, value: CofferValue) -> CofferResult<()>;
|
|
|
|
|
2020-01-06 16:32:56 +00:00
|
|
|
/// Push `value` to `path`. Replaces existing values silently.
|
2020-01-08 22:03:49 +00:00
|
|
|
fn push(&mut self, path: CofferPath, value: CofferValue);
|
|
|
|
|
2020-01-06 16:32:56 +00:00
|
|
|
/// Retrieve `value` at path. Errors if there is no `value` at path.
|
2020-01-08 22:03:49 +00:00
|
|
|
fn get(&self, path: CofferPath) -> CofferResult<CofferValue>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl <T> From<Vec<T>> for CofferPath
|
|
|
|
where T: AsRef<str>
|
|
|
|
{
|
|
|
|
fn from(val: Vec<T>) -> Self {
|
|
|
|
CofferPath::from(&val)
|
|
|
|
}
|
2020-01-06 16:32:56 +00:00
|
|
|
}
|
|
|
|
|
2020-01-08 22:03:49 +00:00
|
|
|
impl <T> From<&Vec<T>> for CofferPath
|
2020-01-06 16:32:56 +00:00
|
|
|
where T: AsRef<str>
|
|
|
|
{
|
2020-01-08 22:03:49 +00:00
|
|
|
fn from(val: &Vec<T>) -> Self {
|
2020-01-06 16:32:56 +00:00
|
|
|
let col = val.iter().map(|p| {(*p).as_ref().to_owned()}).collect();
|
|
|
|
CofferPath(col)
|
|
|
|
}
|
|
|
|
}
|