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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
//! Google Zanzibar–inspired Relationship-Based Access Control (ReBAC).
//!
//! Implements a tuple-based authorization model where access decisions are derived
//! from relationships between subjects and objects, enabling questions like
//! "does User X own Folder Y that contains Document Z?"
//!
//! # Core Concepts
//!
//! - **Tuple** — `(object, relation, subject)` e.g. `("doc:readme", "viewer", "user:alice")`
//! - **Namespace** — a type of object (e.g. `document`, `folder`, `group`)
//! - **Relation** — named edge between an object and a subject (e.g. `owner`, `viewer`)
//! - **Userset rewrite** — indirect relationships via union, intersection, or computed paths
//!
//! # References
//!
//! - [Zanzibar: Google's Consistent, Global Authorization System](https://research.google/pubs/pub48190/)
//! - [OpenFGA / Zanzibar model](https://openfga.dev/docs/concepts)
use crate::errors::{AuthError, Result};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet, VecDeque};
use std::sync::Arc;
use tokio::sync::RwLock;
// ── Relation tuple ──────────────────────────────────────────────────
/// A single relationship tuple: (object, relation, subject).
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct RelationTuple {
/// Object identifier (e.g. "document:readme").
pub object: String,
/// Relation name (e.g. "viewer", "owner", "parent").
pub relation: String,
/// Subject — either a direct user ID or a userset reference
/// (e.g. "user:alice" or "group:engineering#member").
pub subject: String,
}
impl RelationTuple {
/// Create a new relation tuple.
pub fn new(
object: impl Into<String>,
relation: impl Into<String>,
subject: impl Into<String>,
) -> Self {
Self {
object: object.into(),
relation: relation.into(),
subject: subject.into(),
}
}
/// Parse the subject's namespace and optional userset relation.
///
/// `"user:alice"` → `("user", "alice", None)`
/// `"group:eng#member"` → `("group", "eng", Some("member"))`
pub fn parse_subject(&self) -> Option<(&str, &str, Option<&str>)> {
let (ns_id, userset) = if let Some(hash_pos) = self.subject.find('#') {
(&self.subject[..hash_pos], Some(&self.subject[hash_pos + 1..]))
} else {
(self.subject.as_str(), None)
};
let colon_pos = ns_id.find(':')?;
Some((&ns_id[..colon_pos], &ns_id[colon_pos + 1..], userset))
}
/// Parse the object's namespace and ID.
///
/// `"document:readme"` → `("document", "readme")`
pub fn parse_object(&self) -> Option<(&str, &str)> {
let pos = self.object.find(':')?;
Some((&self.object[..pos], &self.object[pos + 1..]))
}
}
// ── Namespace configuration ─────────────────────────────────────────
/// Defines the valid relations for a namespace and how they compute.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NamespaceConfig {
/// Namespace name (e.g. "document").
pub name: String,
/// Direct relation definitions.
pub relations: HashMap<String, RelationDef>,
}
/// Definition of a relation within a namespace.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RelationDef {
/// Direct assignment allowed.
#[serde(default = "default_true")]
pub direct: bool,
/// Compute via union of other relation's subjects.
/// e.g. `viewer` includes all `editor` subjects.
#[serde(default)]
pub union: Vec<String>,
/// Compute via a tuple-to-userset rewrite.
/// e.g. `viewer` includes `parent#viewer` — traverse the `parent` relation
/// on the object, then check `viewer` on those parent objects.
#[serde(default)]
pub tuple_to_userset: Vec<TupleToUserset>,
}
fn default_true() -> bool {
true
}
/// A tuple-to-userset rewrite rule.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TupleToUserset {
/// The relation to traverse on the current object (e.g. "parent").
pub tupleset_relation: String,
/// The relation to check on the resolved object (e.g. "viewer").
pub computed_userset_relation: String,
}
// ── Zanzibar Store ──────────────────────────────────────────────────
/// In-memory Zanzibar-style relationship store with namespace configuration.
pub struct ZanzibarStore {
/// Namespace configurations.
namespaces: Arc<RwLock<HashMap<String, NamespaceConfig>>>,
/// All stored relation tuples, indexed by object.
tuples: Arc<RwLock<HashMap<String, Vec<RelationTuple>>>>,
/// Maximum graph traversal depth to prevent cycles.
max_depth: usize,
}
impl ZanzibarStore {
/// Create a new Zanzibar store with the given max traversal depth.
pub fn new(max_depth: usize) -> Self {
Self {
namespaces: Arc::new(RwLock::new(HashMap::new())),
tuples: Arc::new(RwLock::new(HashMap::new())),
max_depth,
}
}
/// Register a namespace configuration.
pub async fn add_namespace(&self, config: NamespaceConfig) {
self.namespaces
.write()
.await
.insert(config.name.clone(), config);
}
/// Write a relation tuple.
pub async fn write_tuple(&self, tuple: RelationTuple) -> Result<()> {
// Validate namespace/relation
if let Some((ns, _)) = tuple.parse_object() {
let namespaces = self.namespaces.read().await;
if let Some(ns_config) = namespaces.get(ns) {
if !ns_config.relations.contains_key(&tuple.relation) {
return Err(AuthError::validation(&format!(
"Relation '{}' not defined in namespace '{}'",
tuple.relation, ns
)));
}
}
}
self.tuples
.write()
.await
.entry(tuple.object.clone())
.or_default()
.push(tuple);
Ok(())
}
/// Delete a specific relation tuple.
pub async fn delete_tuple(&self, tuple: &RelationTuple) -> bool {
let mut tuples = self.tuples.write().await;
if let Some(list) = tuples.get_mut(&tuple.object) {
let before = list.len();
list.retain(|t| t != tuple);
list.len() < before
} else {
false
}
}
/// Read all tuples for an object, optionally filtered by relation.
pub async fn read_tuples(
&self,
object: &str,
relation: Option<&str>,
) -> Vec<RelationTuple> {
let tuples = self.tuples.read().await;
match tuples.get(object) {
Some(list) => {
if let Some(rel) = relation {
list.iter().filter(|t| t.relation == rel).cloned().collect()
} else {
list.clone()
}
}
None => Vec::new(),
}
}
/// **Check** — the core Zanzibar operation.
///
/// Determines whether `subject` has the `relation` on `object` by
/// traversing direct tuples, union rewrites, and tuple-to-userset paths.
pub async fn check(
&self,
object: &str,
relation: &str,
subject: &str,
) -> Result<bool> {
let mut visited = HashSet::new();
self.check_internal(object, relation, subject, 0, &mut visited)
.await
}
#[allow(clippy::only_used_in_recursion)]
fn check_internal<'a>(
&'a self,
object: &'a str,
relation: &'a str,
subject: &'a str,
depth: usize,
visited: &'a mut HashSet<String>,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<bool>> + Send + 'a>> {
Box::pin(async move {
if depth > self.max_depth {
return Err(AuthError::internal(
"Zanzibar check exceeded maximum traversal depth",
));
}
let visit_key = format!("{object}#{relation}@{subject}");
if !visited.insert(visit_key) {
return Ok(false); // Cycle detected
}
// 1. Direct check
let direct_tuples = self.read_tuples(object, Some(relation)).await;
for t in &direct_tuples {
if t.subject == subject {
return Ok(true);
}
// Userset reference: e.g. subject = "group:eng#member"
if let Some((_, sub_id, Some(sub_rel))) = t.parse_subject() {
let (sub_ns, _) = t.subject.split_once('#').unwrap_or((&t.subject, ""));
// Check if `subject` has `sub_rel` on the referenced object
if self
.check_internal(sub_ns, sub_rel, subject, depth + 1, visited)
.await?
{
let _ = sub_id; // used for the userset resolution
return Ok(true);
}
}
}
// 2. Union rewrites
if let Some((ns, _)) = object.split_once(':') {
let namespaces = self.namespaces.read().await;
if let Some(ns_config) = namespaces.get(ns) {
if let Some(rel_def) = ns_config.relations.get(relation) {
// Check union relations
for union_rel in &rel_def.union {
if self
.check_internal(object, union_rel, subject, depth + 1, visited)
.await?
{
return Ok(true);
}
}
// 3. Tuple-to-userset rewrites
for ttu in &rel_def.tuple_to_userset {
let parent_tuples = self
.read_tuples(object, Some(&ttu.tupleset_relation))
.await;
for pt in &parent_tuples {
if self
.check_internal(
&pt.subject,
&ttu.computed_userset_relation,
subject,
depth + 1,
visited,
)
.await?
{
return Ok(true);
}
}
}
}
}
}
Ok(false)
})
}
/// **Expand** — list all subjects that have a given relation on an object.
pub async fn expand(
&self,
object: &str,
relation: &str,
) -> Result<Vec<String>> {
let mut result = Vec::new();
let mut visited = HashSet::new();
self.expand_internal(object, relation, 0, &mut result, &mut visited)
.await?;
Ok(result)
}
fn expand_internal<'a>(
&'a self,
object: &'a str,
relation: &'a str,
depth: usize,
result: &'a mut Vec<String>,
visited: &'a mut HashSet<String>,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<()>> + Send + 'a>> {
Box::pin(async move {
if depth > self.max_depth {
return Ok(());
}
let visit_key = format!("{object}#{relation}");
if !visited.insert(visit_key) {
return Ok(());
}
// Direct subjects
let tuples = self.read_tuples(object, Some(relation)).await;
for t in &tuples {
if t.subject.contains('#') {
// Userset: expand the referenced object's relation
let (ref_obj, ref_rel) = t.subject.split_once('#').unwrap();
self.expand_internal(ref_obj, ref_rel, depth + 1, result, visited)
.await?;
} else if !result.contains(&t.subject) {
result.push(t.subject.clone());
}
}
// Union rewrites
if let Some((ns, _)) = object.split_once(':') {
let namespaces = self.namespaces.read().await;
if let Some(ns_config) = namespaces.get(ns) {
if let Some(rel_def) = ns_config.relations.get(relation) {
for union_rel in &rel_def.union {
self.expand_internal(object, union_rel, depth + 1, result, visited)
.await?;
}
for ttu in &rel_def.tuple_to_userset {
let parent_tuples = self
.read_tuples(object, Some(&ttu.tupleset_relation))
.await;
for pt in &parent_tuples {
self.expand_internal(
&pt.subject,
&ttu.computed_userset_relation,
depth + 1,
result,
visited,
)
.await?;
}
}
}
}
}
Ok(())
})
}
/// **List objects** — find all objects of a given type where the subject
/// has the specified relation (reverse lookup, uses BFS).
pub async fn list_objects(
&self,
object_type: &str,
relation: &str,
subject: &str,
) -> Result<Vec<String>> {
let prefix = format!("{object_type}:");
let tuples = self.tuples.read().await;
let mut found = Vec::new();
let mut queue: VecDeque<String> = VecDeque::new();
let mut seen = HashSet::new();
// Seed: all objects of this type
for key in tuples.keys() {
if key.starts_with(&prefix) {
queue.push_back(key.clone());
}
}
while let Some(obj) = queue.pop_front() {
if !seen.insert(obj.clone()) {
continue;
}
// We need to release the lock for the async check
drop(tuples);
if self.check(&obj, relation, subject).await? {
found.push(obj);
}
// Re-acquire for next iteration is not needed since we already collected keys
break; // We can't hold tuples across await, so do it differently
}
// Simpler approach: collect candidate objects first, then check each
let candidates: Vec<String> = {
let tuples = self.tuples.read().await;
tuples
.keys()
.filter(|k| k.starts_with(&prefix))
.cloned()
.collect()
};
let mut result = Vec::new();
for obj in candidates {
if self.check(&obj, relation, subject).await? {
result.push(obj);
}
}
Ok(result)
}
/// Get the count of stored tuples.
pub async fn tuple_count(&self) -> usize {
self.tuples
.read()
.await
.values()
.map(|v| v.len())
.sum()
}
}
impl Default for ZanzibarStore {
fn default() -> Self {
Self::new(15)
}
}
#[cfg(test)]
mod tests {
use super::*;
// ── Tuple parsing ───────────────────────────────────────────
#[test]
fn test_tuple_parse_object() {
let t = RelationTuple::new("document:readme", "viewer", "user:alice");
let (ns, id) = t.parse_object().unwrap();
assert_eq!(ns, "document");
assert_eq!(id, "readme");
}
#[test]
fn test_tuple_parse_subject_direct() {
let t = RelationTuple::new("document:readme", "viewer", "user:alice");
let (ns, id, userset) = t.parse_subject().unwrap();
assert_eq!(ns, "user");
assert_eq!(id, "alice");
assert_eq!(userset, None);
}
#[test]
fn test_tuple_parse_subject_userset() {
let t = RelationTuple::new("document:readme", "viewer", "group:eng#member");
let (ns, id, userset) = t.parse_subject().unwrap();
assert_eq!(ns, "group");
assert_eq!(id, "eng");
assert_eq!(userset, Some("member"));
}
#[test]
fn test_tuple_serialization() {
let t = RelationTuple::new("doc:1", "viewer", "user:alice");
let json = serde_json::to_string(&t).unwrap();
let parsed: RelationTuple = serde_json::from_str(&json).unwrap();
assert_eq!(parsed, t);
}
// ── Direct check ────────────────────────────────────────────
#[tokio::test]
async fn test_direct_relation_check() {
let store = ZanzibarStore::default();
store
.write_tuple(RelationTuple::new("document:readme", "viewer", "user:alice"))
.await
.unwrap();
assert!(store.check("document:readme", "viewer", "user:alice").await.unwrap());
assert!(!store.check("document:readme", "viewer", "user:bob").await.unwrap());
assert!(!store.check("document:readme", "editor", "user:alice").await.unwrap());
}
// ── Union relation ──────────────────────────────────────────
#[tokio::test]
async fn test_union_relation() {
let store = ZanzibarStore::default();
// Configure: viewer includes editor (editors can also view)
store
.add_namespace(NamespaceConfig {
name: "document".to_string(),
relations: HashMap::from([
(
"editor".to_string(),
RelationDef {
direct: true,
union: vec![],
tuple_to_userset: vec![],
},
),
(
"viewer".to_string(),
RelationDef {
direct: true,
union: vec!["editor".to_string()],
tuple_to_userset: vec![],
},
),
]),
})
.await;
store
.write_tuple(RelationTuple::new("document:readme", "editor", "user:alice"))
.await
.unwrap();
// Alice is an editor, viewer includes editor via union
assert!(store.check("document:readme", "viewer", "user:alice").await.unwrap());
// Alice is also directly an editor
assert!(store.check("document:readme", "editor", "user:alice").await.unwrap());
// Bob has no relation
assert!(!store.check("document:readme", "viewer", "user:bob").await.unwrap());
}
// ── Tuple-to-userset (parent folder) ────────────────────────
#[tokio::test]
async fn test_tuple_to_userset() {
let store = ZanzibarStore::default();
// Configure: document.viewer includes folder(parent).viewer
store
.add_namespace(NamespaceConfig {
name: "document".to_string(),
relations: HashMap::from([
(
"parent".to_string(),
RelationDef {
direct: true,
union: vec![],
tuple_to_userset: vec![],
},
),
(
"viewer".to_string(),
RelationDef {
direct: true,
union: vec![],
tuple_to_userset: vec![TupleToUserset {
tupleset_relation: "parent".to_string(),
computed_userset_relation: "viewer".to_string(),
}],
},
),
]),
})
.await;
store
.add_namespace(NamespaceConfig {
name: "folder".to_string(),
relations: HashMap::from([(
"viewer".to_string(),
RelationDef {
direct: true,
union: vec![],
tuple_to_userset: vec![],
},
)]),
})
.await;
// folder:docs has viewer alice
store
.write_tuple(RelationTuple::new("folder:docs", "viewer", "user:alice"))
.await
.unwrap();
// document:readme has parent folder:docs
store
.write_tuple(RelationTuple::new("document:readme", "parent", "folder:docs"))
.await
.unwrap();
// Alice should be able to view document:readme via folder:docs
assert!(store.check("document:readme", "viewer", "user:alice").await.unwrap());
// Bob cannot
assert!(!store.check("document:readme", "viewer", "user:bob").await.unwrap());
}
// ── Group membership (userset reference) ────────────────────
#[tokio::test]
async fn test_group_membership_userset() {
let store = ZanzibarStore::default();
store
.add_namespace(NamespaceConfig {
name: "group".to_string(),
relations: HashMap::from([(
"member".to_string(),
RelationDef {
direct: true,
union: vec![],
tuple_to_userset: vec![],
},
)]),
})
.await;
store
.add_namespace(NamespaceConfig {
name: "document".to_string(),
relations: HashMap::from([(
"viewer".to_string(),
RelationDef {
direct: true,
union: vec![],
tuple_to_userset: vec![],
},
)]),
})
.await;
// Alice is a member of group:eng
store
.write_tuple(RelationTuple::new("group:eng", "member", "user:alice"))
.await
.unwrap();
// group:eng#member can view document:readme
store
.write_tuple(RelationTuple::new(
"document:readme",
"viewer",
"group:eng#member",
))
.await
.unwrap();
// Alice can view via group membership
assert!(store.check("document:readme", "viewer", "user:alice").await.unwrap());
// Bob cannot
assert!(!store.check("document:readme", "viewer", "user:bob").await.unwrap());
}
// ── Expand ──────────────────────────────────────────────────
#[tokio::test]
async fn test_expand() {
let store = ZanzibarStore::default();
store
.write_tuple(RelationTuple::new("document:readme", "viewer", "user:alice"))
.await
.unwrap();
store
.write_tuple(RelationTuple::new("document:readme", "viewer", "user:bob"))
.await
.unwrap();
store
.write_tuple(RelationTuple::new("document:readme", "editor", "user:carol"))
.await
.unwrap();
let viewers = store.expand("document:readme", "viewer").await.unwrap();
assert_eq!(viewers.len(), 2);
assert!(viewers.contains(&"user:alice".to_string()));
assert!(viewers.contains(&"user:bob".to_string()));
}
// ── Delete tuple ────────────────────────────────────────────
#[tokio::test]
async fn test_delete_tuple() {
let store = ZanzibarStore::default();
let tuple = RelationTuple::new("document:readme", "viewer", "user:alice");
store.write_tuple(tuple.clone()).await.unwrap();
assert!(store.check("document:readme", "viewer", "user:alice").await.unwrap());
assert!(store.delete_tuple(&tuple).await);
assert!(!store.check("document:readme", "viewer", "user:alice").await.unwrap());
}
#[tokio::test]
async fn test_delete_nonexistent_tuple() {
let store = ZanzibarStore::default();
let tuple = RelationTuple::new("document:readme", "viewer", "user:alice");
assert!(!store.delete_tuple(&tuple).await);
}
// ── Tuple count ─────────────────────────────────────────────
#[tokio::test]
async fn test_tuple_count() {
let store = ZanzibarStore::default();
assert_eq!(store.tuple_count().await, 0);
store
.write_tuple(RelationTuple::new("doc:1", "viewer", "user:a"))
.await
.unwrap();
store
.write_tuple(RelationTuple::new("doc:2", "viewer", "user:b"))
.await
.unwrap();
assert_eq!(store.tuple_count().await, 2);
}
// ── Namespace validation ────────────────────────────────────
#[tokio::test]
async fn test_invalid_relation_rejected() {
let store = ZanzibarStore::default();
store
.add_namespace(NamespaceConfig {
name: "document".to_string(),
relations: HashMap::from([(
"viewer".to_string(),
RelationDef {
direct: true,
union: vec![],
tuple_to_userset: vec![],
},
)]),
})
.await;
let result = store
.write_tuple(RelationTuple::new("document:readme", "admin", "user:alice"))
.await;
assert!(result.is_err());
}
// ── Cycle protection ────────────────────────────────────────
#[tokio::test]
async fn test_cycle_protection() {
let store = ZanzibarStore::new(5);
// Create a cycle: group:a member -> group:b#member, group:b member -> group:a#member
store
.write_tuple(RelationTuple::new("group:a", "member", "group:b#member"))
.await
.unwrap();
store
.write_tuple(RelationTuple::new("group:b", "member", "group:a#member"))
.await
.unwrap();
// Should not infinite-loop; returns false (or an error if depth exceeded)
let result = store.check("group:a", "member", "user:alice").await;
// Either false or depth error — both are acceptable
match result {
Ok(v) => assert!(!v),
Err(_) => {} // Depth exceeded is fine
}
}
// ── Read tuples ─────────────────────────────────────────────
#[tokio::test]
async fn test_read_tuples_with_filter() {
let store = ZanzibarStore::default();
store
.write_tuple(RelationTuple::new("doc:1", "viewer", "user:a"))
.await
.unwrap();
store
.write_tuple(RelationTuple::new("doc:1", "editor", "user:b"))
.await
.unwrap();
let viewers = store.read_tuples("doc:1", Some("viewer")).await;
assert_eq!(viewers.len(), 1);
assert_eq!(viewers[0].subject, "user:a");
let all = store.read_tuples("doc:1", None).await;
assert_eq!(all.len(), 2);
}
#[tokio::test]
async fn test_read_tuples_empty() {
let store = ZanzibarStore::default();
let result = store.read_tuples("doc:nonexistent", None).await;
assert!(result.is_empty());
}
}