spicedb_rust/spicedb/
wrappers.rs

1use super::consistency::Requirement;
2use super::LookupPermissionship;
3
4/// Wrapper enum to shorten the expressions needed to construct the gRPC `Consistency` type
5#[derive(Clone, Debug, PartialEq)]
6pub enum Consistency {
7    MinimizeLatency,
8    AtLeastAsFresh(super::ZedToken),
9    AtExactSnapshot(super::ZedToken),
10    FullyConsistent,
11}
12
13impl From<Consistency> for super::Consistency {
14    fn from(consistency: Consistency) -> Self {
15        let requirement = match consistency {
16            Consistency::MinimizeLatency => Requirement::MinimizeLatency(true),
17            Consistency::AtLeastAsFresh(token) => Requirement::AtLeastAsFresh(token),
18            Consistency::AtExactSnapshot(token) => Requirement::AtExactSnapshot(token),
19            Consistency::FullyConsistent => Requirement::FullyConsistent(true),
20        };
21        super::Consistency {
22            requirement: Some(requirement),
23        }
24    }
25}
26
27#[derive(Clone, Debug, PartialEq)]
28pub struct SubjectReference {
29    pub object: super::ObjectReference,
30    pub optional_relation: Option<String>,
31}
32
33impl From<super::SubjectReference> for SubjectReference {
34    fn from(subject: super::SubjectReference) -> Self {
35        SubjectReference {
36            object: subject.object.unwrap(),
37            optional_relation: if subject.optional_relation.is_empty() {
38                None
39            } else {
40                Some(subject.optional_relation)
41            },
42        }
43    }
44}
45
46#[derive(Clone, Debug, PartialEq)]
47pub struct Relationship {
48    pub resource: super::ObjectReference,
49    pub relation: String,
50    pub subject: SubjectReference,
51    pub optional_caveat: Option<super::ContextualizedCaveat>,
52}
53
54impl From<super::Relationship> for Relationship {
55    fn from(rel: super::Relationship) -> Self {
56        Relationship {
57            resource: rel.resource.unwrap(),
58            relation: rel.relation,
59            subject: rel.subject.unwrap().into(),
60            optional_caveat: rel.optional_caveat,
61        }
62    }
63}
64
65/// Response struct without the stupid optional types due to proto3 and a `From` impl that
66/// assumes the `validate` rules defined in the proto file to be upheld, otherwise it panics.
67#[derive(Clone, Debug, PartialEq)]
68pub struct ReadRelationshipsResponse {
69    pub read_at: super::ZedToken,
70    pub relationships: Vec<Relationship>,
71    pub after_result_cursor: Option<super::Cursor>,
72}
73
74impl From<super::ReadRelationshipsResponse> for ReadRelationshipsResponse {
75    fn from(resp: super::ReadRelationshipsResponse) -> Self {
76        ReadRelationshipsResponse {
77            read_at: resp.read_at.unwrap(),
78            relationships: resp.relationship.into_iter().map(|r| r.into()).collect(),
79            after_result_cursor: resp.after_result_cursor,
80        }
81    }
82}
83
84/// Wrapper struct for the LookupResourcesResponse, since it looks up all resources of a specific
85/// type we can be sure that all Ids are also of the same type.
86pub struct LookupResourcesResponse<Id> {
87    pub id: Id,
88    pub looked_up_at: Option<super::ZedToken>,
89    pub permissionship: LookupPermissionship,
90    pub missing_caveats: Vec<String>,
91    pub after_result_cursor: Option<super::Cursor>,
92}
93
94/// Wrapper struct for the ReadSchemaResponse, with validation presuppositions applied
95pub struct ReadSchemaResponse {
96    pub schema_text: String,
97    pub read_at: super::ZedToken,
98}
99
100impl From<super::ReadSchemaResponse> for ReadSchemaResponse {
101    fn from(resp: super::ReadSchemaResponse) -> Self {
102        ReadSchemaResponse {
103            schema_text: resp.schema_text,
104            read_at: resp.read_at.unwrap(),
105        }
106    }
107}