2022-06-15 07:41:23 +00:00
|
|
|
use derive::Key;
|
2022-01-13 17:40:20 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2022-06-15 07:41:23 +00:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)]
|
2023-04-29 16:44:09 +00:00
|
|
|
pub struct Db<'a> {
|
2022-03-16 15:33:43 +00:00
|
|
|
__: u8,
|
|
|
|
_a: u8,
|
2023-04-29 16:44:09 +00:00
|
|
|
pub ns: &'a str,
|
2022-03-16 15:33:43 +00:00
|
|
|
_b: u8,
|
|
|
|
_c: u8,
|
|
|
|
_d: u8,
|
2023-04-29 16:44:09 +00:00
|
|
|
pub db: &'a str,
|
2022-01-13 17:40:20 +00:00
|
|
|
}
|
|
|
|
|
2023-04-29 16:44:09 +00:00
|
|
|
pub fn new<'a>(ns: &'a str, db: &'a str) -> Db<'a> {
|
|
|
|
Db::new(ns, db)
|
2022-01-13 17:40:20 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 07:21:22 +00:00
|
|
|
pub fn prefix(ns: &str) -> Vec<u8> {
|
2022-04-06 11:06:33 +00:00
|
|
|
let mut k = super::namespace::new(ns).encode().unwrap();
|
2023-05-12 23:40:45 +00:00
|
|
|
k.extend_from_slice(&[b'!', b'd', b'b', 0x00]);
|
2022-03-18 07:21:22 +00:00
|
|
|
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();
|
2023-05-12 23:40:45 +00:00
|
|
|
k.extend_from_slice(&[b'!', b'd', b'b', 0xff]);
|
2022-03-18 07:21:22 +00:00
|
|
|
k
|
|
|
|
}
|
|
|
|
|
2023-04-29 16:44:09 +00:00
|
|
|
impl<'a> Db<'a> {
|
|
|
|
pub fn new(ns: &'a str, db: &'a str) -> Self {
|
|
|
|
Self {
|
2023-05-12 23:40:45 +00:00
|
|
|
__: b'/',
|
|
|
|
_a: b'*',
|
2022-01-13 17:40:20 +00:00
|
|
|
ns,
|
2023-05-12 23:40:45 +00:00
|
|
|
_b: b'!',
|
|
|
|
_c: b'd',
|
|
|
|
_d: b'b',
|
2022-01-13 17:40:20 +00:00
|
|
|
db,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
#[test]
|
|
|
|
fn key() {
|
|
|
|
use super::*;
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let val = Db::new(
|
2023-04-29 16:44:09 +00:00
|
|
|
"test",
|
|
|
|
"test",
|
2022-01-13 17:40:20 +00:00
|
|
|
);
|
|
|
|
let enc = Db::encode(&val).unwrap();
|
|
|
|
let dec = Db::decode(&enc).unwrap();
|
|
|
|
assert_eq!(val, dec);
|
|
|
|
}
|
|
|
|
}
|