surrealpatch/lib/src/key/db.rs

60 lines
1 KiB
Rust
Raw Normal View History

2022-06-15 07:41:23 +00:00
use derive::Key;
use serde::{Deserialize, Serialize};
2022-06-15 07:41:23 +00:00
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)]
pub struct Db {
__: u8,
_a: u8,
2022-03-07 12:25:40 +00:00
pub ns: String,
_b: u8,
_c: u8,
_d: u8,
2022-03-07 12:25:40 +00:00
pub db: String,
}
pub fn new(ns: &str, db: &str) -> Db {
Db::new(ns.to_string(), db.to_string())
}
pub fn prefix(ns: &str) -> Vec<u8> {
2022-04-06 11:06:33 +00:00
let mut k = super::namespace::new(ns).encode().unwrap();
k.extend_from_slice(&[0x21, 0x64, 0x62, 0x00]);
k
}
pub fn suffix(ns: &str) -> Vec<u8> {
2022-04-06 11:06:33 +00:00
let mut k = super::namespace::new(ns).encode().unwrap();
k.extend_from_slice(&[0x21, 0x64, 0x62, 0xff]);
k
}
impl Db {
pub fn new(ns: String, db: String) -> Db {
Db {
__: 0x2f, // /
_a: 0x2a, // *
ns,
_b: 0x21, // !
_c: 0x64, // d
2022-04-01 22:23:54 +00:00
_d: 0x62, // b
db,
}
}
}
#[cfg(test)]
mod tests {
#[test]
fn key() {
use super::*;
#[rustfmt::skip]
let val = Db::new(
"test".to_string(),
"test".to_string(),
);
let enc = Db::encode(&val).unwrap();
let dec = Db::decode(&enc).unwrap();
assert_eq!(val, dec);
}
}