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)]
|
2022-02-23 17:15:49 +00:00
|
|
|
pub struct Dl {
|
2022-03-16 15:33:43 +00:00
|
|
|
__: u8,
|
|
|
|
_a: u8,
|
2022-03-07 12:25:40 +00:00
|
|
|
pub ns: String,
|
2022-03-16 15:33:43 +00:00
|
|
|
_b: u8,
|
2022-03-07 12:25:40 +00:00
|
|
|
pub db: String,
|
2022-03-16 15:33:43 +00:00
|
|
|
_c: u8,
|
|
|
|
_d: u8,
|
|
|
|
_e: u8,
|
2022-03-07 12:25:40 +00:00
|
|
|
pub dl: String,
|
2022-01-13 17:40:20 +00:00
|
|
|
}
|
|
|
|
|
2022-03-04 16:01:32 +00:00
|
|
|
impl From<Dl> for Vec<u8> {
|
|
|
|
fn from(val: Dl) -> Vec<u8> {
|
|
|
|
val.encode().unwrap()
|
2022-01-31 23:11:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-23 17:15:49 +00:00
|
|
|
impl From<Vec<u8>> for Dl {
|
2022-01-31 23:11:06 +00:00
|
|
|
fn from(val: Vec<u8>) -> Self {
|
2022-02-23 17:15:49 +00:00
|
|
|
Dl::decode(&val).unwrap()
|
2022-01-31 23:11:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-07 12:25:40 +00:00
|
|
|
pub fn new(ns: &str, db: &str, dl: &str) -> Dl {
|
|
|
|
Dl::new(ns.to_string(), db.to_string(), dl.to_string())
|
2022-01-13 17:40:20 +00:00
|
|
|
}
|
|
|
|
|
2022-02-23 17:15:49 +00:00
|
|
|
impl Dl {
|
2022-03-07 12:25:40 +00:00
|
|
|
pub fn new(ns: String, db: String, dl: String) -> Dl {
|
2022-02-23 17:15:49 +00:00
|
|
|
Dl {
|
2022-03-16 15:33:43 +00:00
|
|
|
__: 0x2f, // /
|
|
|
|
_a: 0x2a, // *
|
2022-01-13 17:40:20 +00:00
|
|
|
ns,
|
2022-03-16 15:33:43 +00:00
|
|
|
_b: 0x2a, // *
|
2022-01-13 17:40:20 +00:00
|
|
|
db,
|
2022-03-16 15:33:43 +00:00
|
|
|
_c: 0x21, // !
|
|
|
|
_d: 0x64, // d
|
|
|
|
_e: 0x6c, // l
|
2022-03-07 12:25:40 +00:00
|
|
|
dl,
|
2022-01-13 17:40:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn encode(&self) -> Result<Vec<u8>, Error> {
|
|
|
|
Ok(serialize(self)?)
|
|
|
|
}
|
2022-02-23 17:15:49 +00:00
|
|
|
pub fn decode(v: &[u8]) -> Result<Dl, Error> {
|
2022-01-13 17:40:20 +00:00
|
|
|
Ok(deserialize(v)?)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
#[test]
|
|
|
|
fn key() {
|
|
|
|
use super::*;
|
|
|
|
#[rustfmt::skip]
|
2022-02-23 17:15:49 +00:00
|
|
|
let val = Dl::new(
|
2022-01-13 17:40:20 +00:00
|
|
|
"test".to_string(),
|
|
|
|
"test".to_string(),
|
|
|
|
"test".to_string(),
|
|
|
|
);
|
2022-02-23 17:15:49 +00:00
|
|
|
let enc = Dl::encode(&val).unwrap();
|
|
|
|
let dec = Dl::decode(&enc).unwrap();
|
2022-01-13 17:40:20 +00:00
|
|
|
assert_eq!(val, dec);
|
|
|
|
}
|
|
|
|
}
|