use crate::err::Error; use crate::key::bytes::{deserialize, serialize}; use crate::key::BASE; use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize)] pub struct Ns { kv: String, _a: String, ns: String, } impl Into> for Ns { fn into(self) -> Vec { self.encode().unwrap() } } impl From> for Ns { fn from(val: Vec) -> Self { Ns::decode(&val).unwrap() } } pub fn new(ns: &str) -> Ns { Ns::new(ns.to_string()) } impl Ns { pub fn new(ns: String) -> Ns { Ns { kv: BASE.to_owned(), _a: String::from("!ns"), ns, } } pub fn encode(&self) -> Result, Error> { Ok(serialize(self)?) } pub fn decode(v: &[u8]) -> Result { Ok(deserialize::(v)?) } } #[cfg(test)] mod tests { #[test] fn key() { use super::*; #[rustfmt::skip] let val = Ns::new( "test".to_string(), ); let enc = Ns::encode(&val).unwrap(); let dec = Ns::decode(&enc).unwrap(); assert_eq!(val, dec); } }