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 St {
|
2022-03-07 12:25:40 +00:00
|
|
|
__: char,
|
|
|
|
_a: char,
|
|
|
|
pub ns: String,
|
|
|
|
_b: char,
|
|
|
|
pub db: String,
|
|
|
|
_c: char,
|
|
|
|
_d: char,
|
|
|
|
_e: char,
|
|
|
|
pub sc: String,
|
|
|
|
_f: char,
|
|
|
|
_g: char,
|
|
|
|
_h: char,
|
|
|
|
pub tk: String,
|
2022-01-13 17:40:20 +00:00
|
|
|
}
|
|
|
|
|
2022-03-04 16:01:32 +00:00
|
|
|
impl From<St> for Vec<u8> {
|
|
|
|
fn from(val: St) -> Vec<u8> {
|
|
|
|
val.encode().unwrap()
|
2022-01-31 23:11:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Vec<u8>> for St {
|
|
|
|
fn from(val: Vec<u8>) -> Self {
|
|
|
|
St::decode(&val).unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-13 17:40:20 +00:00
|
|
|
pub fn new(ns: &str, db: &str, sc: &str, tk: &str) -> St {
|
|
|
|
St::new(ns.to_string(), db.to_string(), sc.to_string(), tk.to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
impl St {
|
|
|
|
pub fn new(ns: String, db: String, sc: String, tk: String) -> St {
|
|
|
|
St {
|
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: '!',
|
|
|
|
_d: 's',
|
|
|
|
_e: 't',
|
2022-01-13 17:40:20 +00:00
|
|
|
sc,
|
2022-03-07 12:25:40 +00:00
|
|
|
_f: '!',
|
|
|
|
_g: 't',
|
|
|
|
_h: 'k',
|
2022-01-13 17:40:20 +00:00
|
|
|
tk,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn encode(&self) -> Result<Vec<u8>, Error> {
|
|
|
|
Ok(serialize(self)?)
|
|
|
|
}
|
|
|
|
pub fn decode(v: &[u8]) -> Result<St, Error> {
|
|
|
|
Ok(deserialize(v)?)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
#[test]
|
|
|
|
fn key() {
|
|
|
|
use super::*;
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let val = St::new(
|
|
|
|
"test".to_string(),
|
|
|
|
"test".to_string(),
|
|
|
|
"test".to_string(),
|
|
|
|
"test".to_string(),
|
|
|
|
);
|
|
|
|
let enc = St::encode(&val).unwrap();
|
|
|
|
let dec = St::decode(&enc).unwrap();
|
|
|
|
assert_eq!(val, dec);
|
|
|
|
}
|
|
|
|
}
|