2022-01-13 17:40:20 +00:00
|
|
|
use crate::err::Error;
|
|
|
|
use serde::{Deserialize, Serialize};
|
2022-03-15 12:36:41 +00:00
|
|
|
use storekey::{deserialize, serialize};
|
2022-01-13 17:40:20 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize)]
|
|
|
|
pub struct Thing {
|
2022-03-07 12:25:40 +00:00
|
|
|
__: char,
|
|
|
|
_a: char,
|
|
|
|
pub ns: String,
|
|
|
|
_b: char,
|
|
|
|
pub db: String,
|
|
|
|
_c: char,
|
|
|
|
pub tb: String,
|
|
|
|
_d: char,
|
|
|
|
pub id: String,
|
2022-01-13 17:40:20 +00:00
|
|
|
}
|
|
|
|
|
2022-03-04 16:01:32 +00:00
|
|
|
impl From<Thing> for Vec<u8> {
|
|
|
|
fn from(val: Thing) -> Vec<u8> {
|
|
|
|
val.encode().unwrap()
|
2022-01-31 23:11:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Vec<u8>> for Thing {
|
|
|
|
fn from(val: Vec<u8>) -> Self {
|
|
|
|
Thing::decode(&val).unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-07 12:25:40 +00:00
|
|
|
impl From<&Vec<u8>> for Thing {
|
|
|
|
fn from(val: &Vec<u8>) -> Self {
|
|
|
|
Thing::decode(&val).unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-13 17:40:20 +00:00
|
|
|
pub fn new(ns: &str, db: &str, tb: &str, id: &str) -> Thing {
|
|
|
|
Thing::new(ns.to_string(), db.to_string(), tb.to_string(), id.to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Thing {
|
|
|
|
pub fn new(ns: String, db: String, tb: String, id: String) -> Thing {
|
|
|
|
Thing {
|
2022-03-07 12:25:40 +00:00
|
|
|
__: '/',
|
|
|
|
_a: '*',
|
2022-01-13 17:40:20 +00:00
|
|
|
ns,
|
2022-03-07 12:25:40 +00:00
|
|
|
_b: '*',
|
2022-01-13 17:40:20 +00:00
|
|
|
db,
|
2022-03-07 12:25:40 +00:00
|
|
|
_c: '*',
|
2022-01-13 17:40:20 +00:00
|
|
|
tb,
|
2022-03-07 12:25:40 +00:00
|
|
|
_d: '*',
|
2022-01-13 17:40:20 +00:00
|
|
|
id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn encode(&self) -> Result<Vec<u8>, Error> {
|
|
|
|
Ok(serialize(self)?)
|
|
|
|
}
|
|
|
|
pub fn decode(v: &[u8]) -> Result<Thing, Error> {
|
|
|
|
Ok(deserialize(v)?)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
#[test]
|
|
|
|
fn key() {
|
|
|
|
use super::*;
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let val = Thing::new(
|
|
|
|
"test".to_string(),
|
|
|
|
"test".to_string(),
|
|
|
|
"test".to_string(),
|
|
|
|
"test".into(),
|
|
|
|
);
|
|
|
|
let enc = Thing::encode(&val).unwrap();
|
|
|
|
let dec = Thing::decode(&enc).unwrap();
|
|
|
|
assert_eq!(val, dec);
|
|
|
|
}
|
|
|
|
}
|