surrealdb/key/scope/
all.rs

1//! Stores the key prefix for all keys under a scope
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 Scope<'a> {
9	__: u8,
10	_a: u8,
11	pub ns: &'a str,
12	_b: u8,
13	pub db: &'a str,
14	_c: u8,
15	pub sc: &'a str,
16}
17
18pub fn new<'a>(ns: &'a str, db: &'a str, sc: &'a str) -> Scope<'a> {
19	Scope::new(ns, db, sc)
20}
21
22impl KeyRequirements for Scope<'_> {
23	fn key_category(&self) -> KeyCategory {
24		KeyCategory::ScopeRoot
25	}
26}
27
28impl<'a> Scope<'a> {
29	pub fn new(ns: &'a str, db: &'a str, sc: &'a str) -> Self {
30		Self {
31			__: b'/',
32			_a: b'*',
33			ns,
34			_b: b'*',
35			db,
36			_c: super::CHAR,
37			sc,
38		}
39	}
40}
41
42#[cfg(test)]
43mod tests {
44	#[test]
45	fn key() {
46		use super::*;
47		#[rustfmt::skip]
48		let val = Scope::new(
49			"testns",
50			"testdb",
51			"testsc",
52		);
53		let enc = Scope::encode(&val).unwrap();
54		assert_eq!(enc, b"/*testns\0*testdb\0\xb1testsc\0");
55
56		let dec = Scope::decode(&enc).unwrap();
57		assert_eq!(val, dec);
58	}
59}