This repository has been archived by the owner on Feb 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.rs
94 lines (82 loc) · 2.71 KB
/
schema.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use std::collections::HashMap;
use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, PartialEq, Serialize)]
pub struct Schema {
pub namespaces: HashMap<String, Namespace>,
}
#[derive(Debug, Deserialize, PartialEq, Serialize)]
pub struct Namespace {
#[serde(default)]
pub relations: HashMap<String, Vec<TypeRestriction>>,
#[serde(default)]
pub permissions: HashMap<String, Rewrite>,
}
#[derive(Debug, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum TypeRestriction {
Namespace(String),
NamespaceAction(String, String),
}
#[derive(Debug, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum Rewrite {
ComputedUserset(String),
TupleToUserset(String, String),
Union(Vec<Rewrite>),
Intersection(Vec<Rewrite>),
Exclusion(Box<Rewrite>, Box<Rewrite>),
}
impl Schema {
pub fn is_relation(&self, namespace: &str, action: &str) -> bool {
self.namespaces
.get(namespace)
.and_then(|ns| ns.relations.get(action))
.is_some()
}
pub fn is_permission(&self, namespace: &str, action: &str) -> bool {
self.namespaces
.get(namespace)
.and_then(|ns| ns.permissions.get(action))
.is_some()
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn ser_then_der_works() {
let schema = Schema {
namespaces: HashMap::from([
(
"user".to_string(),
Namespace {
relations: HashMap::new(),
permissions: HashMap::new(),
},
),
(
"document".to_string(),
Namespace {
relations: HashMap::from([(
"viewer".to_string(),
vec![TypeRestriction::Namespace("user".to_string())],
)]),
permissions: HashMap::from([(
"can_view".to_string(),
Rewrite::Union(vec![
Rewrite::ComputedUserset("viewer".to_string()),
Rewrite::TupleToUserset(
"parent".to_string(),
"can_view".to_string(),
),
]),
)]),
},
),
]),
};
let serialized = serde_json::to_string(&schema).unwrap();
let deserialized: Schema = serde_json::from_str(&serialized).unwrap();
assert_eq!(schema, deserialized);
}
}