1use crate::key::error::KeyCategory;
3use crate::key::key_req::KeyRequirements;
4use derive::Key;
5use serde::{Deserialize, Serialize};
6use uuid::Uuid;
7
8#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)]
14pub struct Lq<'a> {
15 __: u8,
16 _a: u8,
17 #[serde(with = "uuid::serde::compact")]
18 pub nd: Uuid,
19 _b: u8,
20 _c: u8,
21 _d: u8,
22 #[serde(with = "uuid::serde::compact")]
23 pub lq: Uuid,
24 _e: u8,
25 pub ns: &'a str,
26 _f: u8,
27 pub db: &'a str,
28}
29
30pub fn new<'a>(nd: Uuid, lq: Uuid, ns: &'a str, db: &'a str) -> Lq<'a> {
31 Lq::new(nd, lq, ns, db)
32}
33
34pub fn prefix_nd(nd: &Uuid) -> Vec<u8> {
35 let mut k = [b'/', b'$'].to_vec();
36 k.extend_from_slice(nd.as_bytes());
37 k.extend_from_slice(&[0x00]);
38 k
39}
40
41pub fn suffix_nd(nd: &Uuid) -> Vec<u8> {
42 let mut k = [b'/', b'$'].to_vec();
43 k.extend_from_slice(nd.as_bytes());
44 k.extend_from_slice(&[0xff]);
45 k
46}
47
48impl KeyRequirements for Lq<'_> {
49 fn key_category(&self) -> KeyCategory {
50 KeyCategory::NodeLiveQuery
51 }
52}
53
54impl<'a> Lq<'a> {
55 pub fn new(nd: Uuid, lq: Uuid, ns: &'a str, db: &'a str) -> Self {
56 Self {
57 __: b'/',
58 _a: b'$',
59 nd,
60 _b: b'!',
61 _c: b'l',
62 _d: b'q',
63 lq,
64 _e: b'*',
65 ns,
66 _f: b'*',
67 db,
68 }
69 }
70}
71
72#[cfg(test)]
73mod tests {
74
75 #[test]
76 fn key() {
77 use super::*;
78 #[rustfmt::skip]
79 let nd = Uuid::from_bytes([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10]);
80 #[rustfmt::skip]
81 let lq = Uuid::from_bytes([0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20]);
82 let val = Lq::new(nd, lq, "testns", "testdb");
83 let enc = Lq::encode(&val).unwrap();
84 assert_eq!(
85 enc,
86 b"/$\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\
87 !lq\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20\
88 *testns\0*testdb\0"
89 );
90
91 let dec = Lq::decode(&enc).unwrap();
92 assert_eq!(val, dec);
93 }
94
95 #[test]
96 fn prefix_nd() {
97 use super::*;
98 let nd = Uuid::from_bytes([
99 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
100 0x0f, 0x10,
101 ]);
102 let val = prefix_nd(&nd);
103 assert_eq!(val, b"/$\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x00");
104 }
105
106 #[test]
107 fn suffix_nd() {
108 use super::*;
109 let nd = Uuid::from_bytes([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
110 let val = suffix_nd(&nd);
111 assert_eq!(val, b"/$\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\xff");
112 }
113}