2022-03-18 07:24:36 +00:00
|
|
|
use crate::sql::id::Id;
|
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)]
|
2022-01-13 17:40:20 +00:00
|
|
|
pub struct Thing {
|
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,
|
2022-03-07 12:25:40 +00:00
|
|
|
pub tb: String,
|
2022-03-16 15:33:43 +00:00
|
|
|
_d: u8,
|
2022-03-18 07:24:36 +00:00
|
|
|
pub id: Id,
|
2022-01-13 17:40:20 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 07:24:36 +00:00
|
|
|
pub fn new(ns: &str, db: &str, tb: &str, id: &Id) -> Thing {
|
|
|
|
Thing::new(ns.to_string(), db.to_string(), tb.to_string(), id.to_owned())
|
2022-01-13 17:40:20 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 07:21:22 +00:00
|
|
|
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(&[0x2a, 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(&[0x2a, 0xff]);
|
|
|
|
k
|
|
|
|
}
|
|
|
|
|
2022-01-13 17:40:20 +00:00
|
|
|
impl Thing {
|
2022-03-18 07:24:36 +00:00
|
|
|
pub fn new(ns: String, db: String, tb: String, id: Id) -> Thing {
|
2022-01-13 17:40:20 +00:00
|
|
|
Thing {
|
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: 0x2a, // *
|
2022-01-13 17:40:20 +00:00
|
|
|
tb,
|
2022-03-16 15:33:43 +00:00
|
|
|
_d: 0x2a, // *
|
2022-01-13 17:40:20 +00:00
|
|
|
id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
#[test]
|
|
|
|
fn key() {
|
|
|
|
use super::*;
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let val = Thing::new(
|
|
|
|
"test".to_string(),
|
|
|
|
"test".to_string(),
|
|
|
|
"test".to_string(),
|
|
|
|
"test".into(),
|
|
|
|
);
|
|
|
|
let enc = Thing::encode(&val).unwrap();
|
|
|
|
let dec = Thing::decode(&enc).unwrap();
|
|
|
|
assert_eq!(val, dec);
|
|
|
|
}
|
2023-01-14 19:28:33 +00:00
|
|
|
#[test]
|
|
|
|
fn key_complex() {
|
|
|
|
use super::*;
|
|
|
|
//
|
|
|
|
let id1 = "['test']";
|
|
|
|
let (_, id1) = crate::sql::id::id(id1).expect("Failed to parse the ID");
|
|
|
|
let val = Thing::new("test".to_string(), "test".to_string(), "test".to_string(), id1);
|
|
|
|
let enc = Thing::encode(&val).unwrap();
|
|
|
|
let dec = Thing::decode(&enc).unwrap();
|
|
|
|
assert_eq!(val, dec);
|
|
|
|
println!("---");
|
|
|
|
//
|
|
|
|
let id2 = "['f8e238f2-e734-47b8-9a16-476b291bd78a']";
|
|
|
|
let (_, id2) = crate::sql::id::id(id2).expect("Failed to parse the ID");
|
|
|
|
let val = Thing::new("test".to_string(), "test".to_string(), "test".to_string(), id2);
|
|
|
|
let enc = Thing::encode(&val).unwrap();
|
|
|
|
let dec = Thing::decode(&enc).unwrap();
|
|
|
|
assert_eq!(val, dec);
|
|
|
|
println!("---");
|
|
|
|
}
|
2022-01-13 17:40:20 +00:00
|
|
|
}
|