surrealdb/key/database/
all.rs

1//! Stores the key prefix for all keys under a database
2use crate::key::error::KeyCategory;
3use crate::key::key_req::KeyRequirements;
4use derive::Key;
5use serde::{Deserialize, Serialize};
6
7#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)]
8pub struct All<'a> {
9	__: u8,
10	_a: u8,
11	pub ns: &'a str,
12	_b: u8,
13	pub db: &'a str,
14}
15
16pub fn new<'a>(ns: &'a str, db: &'a str) -> All<'a> {
17	All::new(ns, db)
18}
19
20impl KeyRequirements for All<'_> {
21	fn key_category(&self) -> KeyCategory {
22		KeyCategory::DatabaseRoot
23	}
24}
25
26impl<'a> All<'a> {
27	pub fn new(ns: &'a str, db: &'a str) -> Self {
28		Self {
29			__: b'/', // /
30			_a: b'*', // *
31			ns,
32			_b: b'*', // *
33			db,
34		}
35	}
36}
37
38#[cfg(test)]
39mod tests {
40	#[test]
41	fn key() {
42		use super::*;
43		#[rustfmt::skip]
44		let val = All::new(
45			"testns",
46			"testdb",
47		);
48		let enc = All::encode(&val).unwrap();
49		assert_eq!(enc, b"/*testns\0*testdb\0");
50
51		let dec = All::decode(&enc).unwrap();
52		assert_eq!(val, dec);
53	}
54}