surrealpatch/lib/src/key/ix.rs
2023-05-12 19:40:45 -04:00

69 lines
1.2 KiB
Rust

use derive::Key;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)]
pub struct Ix<'a> {
__: u8,
_a: u8,
pub ns: &'a str,
_b: u8,
pub db: &'a str,
_c: u8,
pub tb: &'a str,
_d: u8,
_e: u8,
_f: u8,
pub ix: &'a str,
}
pub fn new<'a>(ns: &'a str, db: &'a str, tb: &'a str, ix: &'a str) -> Ix<'a> {
Ix::new(ns, db, tb, ix)
}
pub fn prefix(ns: &str, db: &str, tb: &str) -> Vec<u8> {
let mut k = super::table::new(ns, db, tb).encode().unwrap();
k.extend_from_slice(&[b'!', b'i', b'x', 0x00]);
k
}
pub fn suffix(ns: &str, db: &str, tb: &str) -> Vec<u8> {
let mut k = super::table::new(ns, db, tb).encode().unwrap();
k.extend_from_slice(&[b'!', b'i', b'x', 0xff]);
k
}
impl<'a> Ix<'a> {
pub fn new(ns: &'a str, db: &'a str, tb: &'a str, ix: &'a str) -> Self {
Self {
__: b'/',
_a: b'*',
ns,
_b: b'*',
db,
_c: b'*',
tb,
_d: b'!',
_e: b'i',
_f: b'x',
ix,
}
}
}
#[cfg(test)]
mod tests {
#[test]
fn key() {
use super::*;
#[rustfmt::skip]
let val = Ix::new(
"test",
"test",
"test",
"test",
);
let enc = Ix::encode(&val).unwrap();
let dec = Ix::decode(&enc).unwrap();
assert_eq!(val, dec);
}
}