strig 0.7.0

Extensible authorization engine: permission catalog, RBAC, ABAC and ReBAC in one decision, with governance and auditing of changes.
Documentation
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
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
//! The Actors syntax: policy grouped by who acts. A heading names a role, user, principal, relation
//! or `anyone`; the lines indented under it say what that actor may do. Reference material
//! (catalog, relations, tuples, engine, governance) follows as sections of plain lines.

use std::collections::{BTreeMap, HashMap};

use crate::dsl::blocks::{duplicate, scoped};
use crate::dsl::blocks_print::{self as bp, Printer};
use crate::dsl::build::Builder;
use crate::dsl::error::{PResult, PrintError, Raw, Span};
use crate::dsl::grammar::{Cursor, anchor, assignment, closest, condition, describe, matcher, pairs, suggest, target};
use crate::dsl::lex::Tok;
use crate::dsl::policy::{Policy, ids};
use crate::dsl::rules;
use crate::{Anchor, Effect, Grant, Subject, SubjectMatcher};

type Printed<T> = Result<T, PrintError>;

const HEADINGS: [&str; 11] = [
    "catalog",
    "relations",
    "tuples",
    "engine",
    "governance",
    "role",
    "user",
    "subject",
    "principal",
    "relation",
    "anyone",
];
/// The element id a grant has while its line is read: the id comes last on the line.
const PENDING: &str = "grant:?";
const FAILED: &str = "grant:?failed";

#[derive(Clone, Copy, PartialEq)]
enum Section {
    Catalog,
    Relations,
    Tuples,
    Engine,
    Governance,
}

/// The heading whose lines are being read.
struct Actor {
    matcher: Option<SubjectMatcher>,
    heading: Span,
    /// Where the subject this heading declares sits in the policy, and its id.
    subject: Option<(String, Option<usize>)>,
    /// A `user` heading declares its subject on the first `role`, `principal` or `attr` line, wherever
    /// that line sits under the heading; this is the user id until then.
    lazy_user: Option<String>,
    level: Option<u32>,
    scope: Option<Anchor>,
    /// The `in` line whose scope no rule has used yet.
    empty_in: Option<Span>,
}

enum Ctx {
    None,
    /// The heading failed: its lines are not read again, so one mistake is one error.
    Skip,
    Section(Section),
    Actor(Actor),
}

/// What follows a heading: whether there is a line under it, and the word it starts with.
struct Below {
    child: Option<String>,
}

/// Reads every line up to the end of the file. Errors are reported on the builder and the reading
/// goes on from the next line.
pub(crate) fn parse(cur: &mut Cursor, b: &mut Builder) {
    if !cur.at_eof() && !cur.current().bol {
        b.report(Raw::new(cur.span(), "the language declaration takes its own line").hint("start a new line"));
    }
    let from = cur.index();
    let base = cur.current().span.col;
    let starts: Vec<usize> = cur.statements(None).into_iter().filter(|at| *at >= from).collect();
    let end = cur.end();
    let mut ctx = Ctx::None;
    for (n, at) in starts.iter().enumerate() {
        let fence = starts.get(n + 1).copied().unwrap_or(end);
        let below = look_below(cur, &starts, n, base, end);
        cur.goto(*at, fence);
        let col = cur.current().span.col;
        let result = if col < base {
            Err(Raw::new(cur.span(), "this line starts left of the first one").hint("headings share one column"))
        } else if col == base {
            flush_in(&ctx, b);
            heading(cur, b, &below).map(|next| ctx = next)
        } else {
            child(cur, b, &mut ctx, col)
        };
        match result {
            Ok(()) if cur.at_eof() => {}
            Ok(()) => {
                let token = cur.current();
                b.report(Raw::new(
                    token.span,
                    format!("unexpected {} at the end of the line", describe(&token.tok)),
                ));
            }
            Err(raw) => {
                if col <= base {
                    ctx = Ctx::Skip;
                }
                b.report(raw);
            }
        }
        cur.reset_depth();
    }
    flush_in(&ctx, b);
}

/// An `in` line that no rule sits under is a mistake: the scope would silently apply to nothing.
fn flush_in(ctx: &Ctx, b: &mut Builder) {
    if let Ctx::Actor(actor) = ctx
        && let Some(at) = actor.empty_in
    {
        b.report(
            Raw::new(at, "an `in` line has no rule under it").hint("indent the rules that take the scope under it"),
        );
    }
}

fn look_below(cur: &mut Cursor, starts: &[usize], n: usize, base: u32, end: usize) -> Below {
    let Some(next) = starts.get(n + 1).copied() else {
        return Below { child: None };
    };
    if cur.column_at(next) <= base {
        return Below { child: None };
    }
    cur.goto(next, (next + 1).min(end));
    let word = match cur.peek(0) {
        Tok::Ident(word) => word,
        _ => String::new(),
    };
    Below { child: Some(word) }
}

fn heading(cur: &mut Cursor, b: &mut Builder, below: &Below) -> PResult<Ctx> {
    let head = cur.current();
    let Tok::Ident(word) = head.tok.clone() else {
        return Err(Raw::new(
            head.span,
            format!("a heading starts with a word, found {}", describe(&head.tok)),
        ));
    };
    let ends = |cur: &Cursor| cur.peek(1) == Tok::Eof;
    match word.as_str() {
        "catalog" | "relations" | "tuples" | "engine" | "governance" => {
            if !ends(cur) {
                cur.advance();
                let token = cur.current();
                return Err(Raw::new(token.span, format!("`{word}` stands alone on its line")));
            }
            cur.advance();
            let section = match word.as_str() {
                "catalog" => Section::Catalog,
                "relations" => Section::Relations,
                "tuples" => Section::Tuples,
                "engine" => Section::Engine,
                _ => Section::Governance,
            };
            if section == Section::Relations && below.child.is_none() {
                empty_relations(b, head.span)?;
            }
            Ok(Ctx::Section(section))
        }
        "role" => role_heading(cur, b, head.span),
        "subject" => {
            cur.advance();
            let at = anchor(cur)?;
            let Some(sid) = at.anchor.id.clone() else {
                return Err(Raw::new(at.span, "a subject needs an id").hint("write: subject user:ana"));
            };
            let subject = declare_subject(cur, b, &at.anchor.kind, &sid, head.span);
            Ok(Ctx::Actor(Actor {
                matcher: None,
                heading: head.span,
                subject: Some(subject),
                lazy_user: None,
                level: None,
                scope: None,
                empty_in: None,
            }))
        }
        "user" | "principal" | "relation" | "anyone" | "any" => {
            let found = matcher(cur)?;
            let lazy_user = match &found {
                SubjectMatcher::User { id } => Some(id.clone()),
                _ => None,
            };
            Ok(Ctx::Actor(Actor {
                matcher: Some(found),
                heading: head.span,
                subject: None,
                lazy_user,
                level: None,
                scope: None,
                empty_in: None,
            }))
        }
        other => Err(Raw::new(head.span, format!("unknown heading `{other}`")).hint(suggest(other, &HEADINGS))),
    }
}

/// `relations` with nothing under it: the policy declares that it has no relations.
fn empty_relations(b: &mut Builder, at: Span) -> PResult<()> {
    let repeated = duplicate(b, ids::RELATIONS, at, "`relations`");
    b.open(ids::RELATIONS, at);
    b.close(at);
    if repeated {
        return Ok(());
    }
    if b.policy.relations.is_some() {
        let raw = Raw::new(at, "an empty `relations` comes after a relation")
            .hint("declare it before any relation, or drop it");
        b.conflict(raw, None);
        return Ok(());
    }
    b.policy.relations = Some(Vec::new());
    Ok(())
}

/// `role r [inherits a, b]` declares the role the first time; later headings of it only name it.
fn role_heading(cur: &mut Cursor, b: &mut Builder, at: Span) -> PResult<Ctx> {
    cur.advance();
    let Tok::Ident(name) = cur.peek(0) else {
        let token = cur.current();
        return Err(Raw::new(
            token.span,
            format!("expected a role name, found {}", describe(&token.tok)),
        ));
    };
    let inherits = matches!(cur.peek(1), Tok::Ident(w) if w == "inherits");
    if b.declared(&ids::role(&name)).is_some() && !inherits {
        cur.advance();
    } else {
        if !inherits {
            near_miss(b, &name, cur.span())?;
        }
        rules::role(cur, b, true)?;
    }
    Ok(Ctx::Actor(Actor {
        matcher: Some(SubjectMatcher::Role { id: name }),
        heading: at,
        subject: None,
        lazy_user: None,
        level: None,
        scope: None,
        empty_in: None,
    }))
}

/// The name of a declared role that `name` is a slip away from, if there is one.
fn close_role<'a>(known: &[&'a str], name: &str) -> Option<&'a str> {
    closest(name, known)
}

/// A heading that would declare a role whose name is close to a declared one is read as a typo,
/// not as a new role. Writing `role <name> inherits` with nothing after it declares it anyway.
fn near_miss(b: &Builder, name: &str, at: Span) -> PResult<()> {
    let known: Vec<&str> = b.policy.roles.iter().map(|r| r.role.as_str()).collect();
    let Some(close) = close_role(&known, name) else {
        return Ok(());
    };
    Err(Raw::new(at, format!("role `{name}` is not declared")).hint(format!(
        "did you mean `{close}`? if `{name}` is a new role, write `role {name} inherits` with nothing after it"
    )))
}

/// The text that declares the role at `at`: a name close to an earlier role gets the explicit
/// empty `inherits`, so that it reads back as new.
fn role_declaration(policy: &Policy, at: usize) -> Printed<(String, String)> {
    let Some(role) = policy.roles.get(at) else {
        return Err(PrintError::Unrepresentable {
            what: "a role".to_string(),
            reason: "the role is missing".to_string(),
        });
    };
    let (id, mut text) = rules::role_text(role)?;
    let before: Vec<&str> = policy.roles.iter().take(at).map(|r| r.role.as_str()).collect();
    if role.inherits.is_empty() && close_role(&before, &role.role).is_some() {
        text.push_str(" inherits");
    }
    Ok((id, text))
}

fn declare_subject(cur: &Cursor, b: &mut Builder, kind: &str, sid: &str, at: Span) -> (String, Option<usize>) {
    let id = ids::subject(kind, sid);
    let repeated = duplicate(b, &id, at, &format!("subject `{kind}:{sid}`"));
    b.open(&id, at);
    b.close(cur.previous_span());
    if repeated {
        return (id, None);
    }
    b.policy
        .subjects
        .push(Subject::of_kind(kind.to_string(), sid.to_string()));
    let at = b.policy.subjects.len() - 1;
    (id, Some(at))
}

fn child(cur: &mut Cursor, b: &mut Builder, ctx: &mut Ctx, col: u32) -> PResult<()> {
    match ctx {
        Ctx::None => {
            Err(Raw::new(cur.span(), "unexpected indentation").hint("a line under a heading, or a heading at column 1"))
        }
        Ctx::Skip => {
            while !cur.at_eof() {
                cur.advance();
            }
            Ok(())
        }
        Ctx::Section(section) => section_line(cur, b, *section),
        Ctx::Actor(actor) => {
            let level = *actor.level.get_or_insert(col);
            if col < level {
                return Err(
                    Raw::new(cur.span(), "inconsistent indentation").hint("lines under one heading share a column")
                );
            }
            if col > level {
                let Some(scope) = actor.scope.clone() else {
                    return Err(Raw::new(cur.span(), "unexpected indentation")
                        .hint("only an `in <scope>` line can have lines nested under it"));
                };
                let found = cur.current();
                return match &found.tok {
                    Tok::Ident(w) if w == "allow" || w == "deny" => {
                        actor.empty_in = None;
                        grant(cur, b, actor, Some(&scope))
                    }
                    _ => Err(Raw::new(found.span, "only rules go under an `in` line").hint("`in` blocks do not nest")),
                };
            }
            if let Some(at) = actor.empty_in.take() {
                b.report(
                    Raw::new(at, "an `in` line has no rule under it")
                        .hint("indent the rules that take the scope under it"),
                );
            }
            actor.scope = None;
            actor_line(cur, b, actor)
        }
    }
}

fn actor_line(cur: &mut Cursor, b: &mut Builder, actor: &mut Actor) -> PResult<()> {
    let head = cur.current();
    let Tok::Ident(word) = head.tok.clone() else {
        return Err(Raw::new(
            head.span,
            format!("a line starts with a word, found {}", describe(&head.tok)),
        ));
    };
    match word.as_str() {
        "in" => {
            let at = cur.span();
            cur.advance();
            actor.scope = Some(anchor(cur)?.anchor);
            actor.empty_in = Some(at);
            Ok(())
        }
        "allow" | "deny" => grant(cur, b, actor, None),
        "role" | "principal" | "attr" => {
            if actor.subject.is_none()
                && let Some(user) = actor.lazy_user.take()
            {
                actor.subject = Some(declare_subject(cur, b, "user", &user, actor.heading));
            }
            let Some((id, at)) = actor.subject.clone() else {
                return Err(Raw::new(
                    head.span,
                    format!("`{word}` lines belong under a user or subject heading"),
                )
                .hint("a user, subject or role heading names who the line is about; use `role <name>` to give a role its rules"));
            };
            cur.advance();
            subject_line(cur, b, &word, &id, at, head.span)
        }
        other => Err(Raw::new(head.span, format!("unknown line `{other}`"))
            .hint(suggest(other, &["allow", "deny", "in", "role", "principal", "attr"]))),
    }
}

fn subject_line(cur: &mut Cursor, b: &mut Builder, word: &str, id: &str, at: Option<usize>, head: Span) -> PResult<()> {
    match word {
        "role" => {
            let role = cur.word("a role name")?;
            let assign_id = ids::assign(id, &role.v);
            let repeated = duplicate(b, &assign_id, role.span, &format!("the assignment of `{}`", role.v));
            let read = scoped(cur, b, &assign_id, head, |cur, _| assignment(cur, role.v.clone()))?;
            if !repeated {
                with_subject(b, at, |s| s.roles.push(read));
            }
        }
        "principal" => {
            let name = cur.word("a principal name")?;
            with_subject(b, at, |s| s.principals.push(name.v));
        }
        _ => {
            let key_at = cur.span();
            let read = pairs(cur)?;
            let mut clash = None;
            with_subject(b, at, |s| {
                for (key, item) in read {
                    if s.attrs.insert(key.clone(), item).is_some() {
                        clash = Some(key);
                    }
                }
            });
            if let Some(key) = clash {
                return Err(Raw::new(key_at, format!("key `{key}` is given twice")));
            }
        }
    }
    Ok(())
}

fn with_subject(b: &mut Builder, at: Option<usize>, apply: impl FnOnce(&mut Subject)) {
    if let Some(at) = at
        && let Some(subject) = b.policy.subjects.get_mut(at)
    {
        apply(subject);
    }
}

/// `allow|deny <permission> on <target> [when <c>] [from <d>] [until <d>] as <id>`
fn grant(cur: &mut Cursor, b: &mut Builder, actor: &Actor, scope: Option<&Anchor>) -> PResult<()> {
    let Some(subject) = actor.matcher.clone() else {
        return Err(
            Raw::new(cur.span(), "this heading cannot own rules").hint("use role, user, principal, relation or anyone")
        );
    };
    let effect = cur.word("allow or deny")?;
    let scope = scope.or(actor.scope.as_ref()).cloned();
    b.open(PENDING, effect.span);
    let result = grant_body(cur, b, actor, &effect.v, subject, scope);
    b.close(cur.previous_span());
    match result {
        Ok(Some(built)) => {
            b.policy.grants.push(built);
            Ok(())
        }
        Ok(None) => {
            b.rename(PENDING, FAILED);
            Ok(())
        }
        Err(raw) => {
            b.rename(PENDING, FAILED);
            Err(raw)
        }
    }
}

fn grant_body(
    cur: &mut Cursor,
    b: &mut Builder,
    actor: &Actor,
    effect: &str,
    subject: SubjectMatcher,
    scope: Option<Anchor>,
) -> PResult<Option<Grant>> {
    let permission = cur.word("a permission")?;
    if !cur.eat_word("on") {
        let token = cur.current();
        return Err(Raw::new(
            token.span,
            format!("expected `on` before the target, found {}", describe(&token.tok)),
        ));
    }
    let target_at = cur.span();
    let target = target(cur)?;
    let (mut when, mut from, mut until, mut name) = (None, None, None, None);
    let (mut when_at, mut until_at) = (None, None);
    while !cur.at_eof() {
        let clause = cur.word("when, from, until or as")?;
        let twice = || Raw::new(clause.span, format!("`{}` is given twice", clause.v));
        match clause.v.as_str() {
            "when" => {
                when_at = Some(cur.span());
                if when.replace(condition(cur)?).is_some() {
                    return Err(twice());
                }
            }
            "from" => {
                if from.replace(cur.date()?.v).is_some() {
                    return Err(twice());
                }
            }
            "until" => {
                until_at = Some(cur.span());
                if until.replace(cur.date()?.v).is_some() {
                    return Err(twice());
                }
            }
            "as" => {
                if name.replace(cur.word("a grant id")?).is_some() {
                    return Err(twice());
                }
            }
            "in" => {
                return Err(
                    Raw::new(clause.span, "a rule takes its scope from the enclosing `in` line")
                        .hint("put `in <scope>` on its own line above the rule"),
                );
            }
            other => {
                return Err(Raw::new(clause.span, format!("unexpected `{other}`"))
                    .hint("a rule takes: when <condition> from <date> until <date> as <id>"));
            }
        }
    }
    let Some(name) = name else {
        return Err(Raw::new(cur.span(), "a rule needs an id: end the line with `as <id>`"));
    };
    let id = ids::grant(&name.v);
    if duplicate(b, &id, name.span, &format!("grant `{}`", name.v)) {
        return Ok(None);
    }
    b.rename(PENDING, &id);
    let mark = |b: &mut Builder, field: &str, span: Span| b.mark(&ids::field(&id, field), span);
    mark(b, "subject", actor.heading);
    mark(b, "permission", permission.span);
    mark(b, "target", target_at);
    if let Some(at) = when_at {
        mark(b, "when", at);
    }
    if let Some(at) = until_at {
        mark(b, "valid_until", at);
    }
    let effect = if effect == "allow" { Effect::Allow } else { Effect::Deny };
    Ok(Some(Grant {
        id: name.v,
        subject,
        permission: permission.v,
        target,
        effect,
        scope,
        condition: when,
        valid_from: from,
        valid_until: until,
    }))
}

fn section_line(cur: &mut Cursor, b: &mut Builder, section: Section) -> PResult<()> {
    let head = cur.current();
    let word = match &head.tok {
        Tok::Ident(word) => word.clone(),
        other => {
            return Err(Raw::new(
                head.span,
                format!("a line starts with a word, found {}", describe(other)),
            ));
        }
    };
    match section {
        Section::Catalog => match word.as_str() {
            "kinds" | "broad" => {
                cur.advance();
                rules::kinds(cur, b, &head.span, word == "kinds")
            }
            _ => rules::permission(cur, b),
        },
        Section::Relations => rules::relation(cur, b),
        Section::Tuples => rules::tuple_line(cur, b),
        Section::Engine => {
            cur.advance();
            match word.as_str() {
                "strategy" => rules::strategy(cur, b, head.span),
                "depth" => rules::depth(cur, b, head.span),
                "override" => rules::override_line(cur, b),
                other => Err(Raw::new(head.span, format!("unknown engine line `{other}`"))
                    .hint(suggest(other, &["strategy", "depth", "override"]))),
            }
        }
        Section::Governance => rules::always(cur, b),
    }
}

// ---- printer --------------------------------------------------------------------------------

/// Writes the policy as an Actors source.
pub(crate) fn print(policy: &Policy) -> Printed<String> {
    let mut printer = Printer::start(policy, false)?;
    printer.margin_step = 2;
    printer.leaf(0, ids::LANGUAGE, "language actors", false);
    actors(&mut printer, policy)?;
    sections(&mut printer, policy)?;
    while printer.out.ends_with("\n\n") {
        printer.out.pop();
    }
    printer.complete(policy)
}

/// How far the roles and subjects of the policy have been written: they come out in the order the
/// policy keeps them, so the reading puts them back in that order.
struct Walk<'a> {
    policy: &'a Policy,
    roles: HashMap<&'a str, usize>,
    users: HashMap<&'a str, usize>,
    next_role: usize,
    next_subject: usize,
}

fn actors(p: &mut Printer, policy: &Policy) -> Printed<()> {
    let mut walk = Walk {
        policy,
        roles: HashMap::new(),
        users: HashMap::new(),
        next_role: 0,
        next_subject: 0,
    };
    for (at, role) in policy.roles.iter().enumerate() {
        walk.roles.entry(role.role.as_str()).or_insert(at);
    }
    for (at, subject) in policy.subjects.iter().enumerate() {
        if let (true, Some(id)) = (subject.kind == "user", &subject.id) {
            walk.users.entry(id.as_str()).or_insert(at);
        }
    }
    let grants = &policy.grants;
    let mut i = 0;
    while let Some(first) = grants.get(i) {
        let mut j = i;
        while grants.get(j).is_some_and(|g| g.subject == first.subject) {
            j += 1;
        }
        p.blank();
        let decl = group_heading(p, &mut walk, &first.subject)?;
        if let Some(subject) = decl {
            decl_lines(p, subject)?;
        }
        rule_lines(p, grants.get(i..j).unwrap_or_default())?;
        i = j;
    }
    while walk.next_role < policy.roles.len() {
        lone_role(p, &mut walk)?;
    }
    while walk.next_subject < policy.subjects.len() {
        lone_subject(p, &mut walk)?;
    }
    Ok(())
}

fn lone_role(p: &mut Printer, walk: &mut Walk) -> Printed<()> {
    if walk.next_role < walk.policy.roles.len() {
        let (id, text) = role_declaration(walk.policy, walk.next_role)?;
        p.blank();
        p.leaf(0, &id, &text, false);
    }
    walk.next_role += 1;
    Ok(())
}

fn lone_subject(p: &mut Printer, walk: &mut Walk) -> Printed<()> {
    if let Some(subject) = walk.policy.subjects.get(walk.next_subject) {
        p.blank();
        let (id, head) = subject_head(subject)?;
        p.leaf(0, &id, &head, false);
        decl_lines(p, subject)?;
    }
    walk.next_subject += 1;
    Ok(())
}

fn subject_head(s: &Subject) -> Printed<(String, String)> {
    let Some(sid) = &s.id else {
        return Err(PrintError::Unrepresentable {
            what: "a subject".to_string(),
            reason: "a subject needs an id".to_string(),
        });
    };
    let head = format!("subject {}:{}", bp::word(&s.kind, "kind")?, bp::id_part(sid, "id")?);
    Ok((ids::subject(&s.kind, sid), head))
}

fn has_decl(s: &Subject) -> bool {
    !(s.roles.is_empty() && s.principals.is_empty() && s.attrs.is_empty())
}

/// Writes the heading of a run of grants and says which subject it declares, if any.
fn group_heading<'a>(p: &mut Printer, walk: &mut Walk<'a>, m: &SubjectMatcher) -> Printed<Option<&'a Subject>> {
    match m {
        SubjectMatcher::Role { id } => {
            let at = walk.roles.get(id.as_str()).copied().filter(|at| *at >= walk.next_role);
            let Some(at) = at else {
                p.push(0, &format!("role {}", bp::word(id, "role")?));
                return Ok(None);
            };
            while walk.next_role < at {
                lone_role(p, walk)?;
                p.blank();
            }
            walk.next_role = at + 1;
            if at < walk.policy.roles.len() {
                let (rid, text) = role_declaration(walk.policy, at)?;
                p.leaf(0, &rid, &text, false);
            }
            Ok(None)
        }
        SubjectMatcher::User { id } => {
            let text = bp::matcher(m)?;
            let at = walk
                .users
                .get(id.as_str())
                .copied()
                .filter(|at| *at >= walk.next_subject);
            let Some(at) = at else {
                p.push(0, &text);
                return Ok(None);
            };
            while walk.next_subject < at {
                lone_subject(p, walk)?;
                p.blank();
            }
            walk.next_subject = at + 1;
            let Some(subject) = walk.policy.subjects.get(at) else {
                p.push(0, &text);
                return Ok(None);
            };
            if has_decl(subject) {
                p.leaf(0, &ids::subject("user", id), &text, false);
                return Ok(Some(subject));
            }
            let (sid, head) = subject_head(subject)?;
            p.leaf(0, &sid, &head, false);
            p.blank();
            p.push(0, &text);
            Ok(None)
        }
        other => {
            p.push(0, &bp::matcher(other)?);
            Ok(None)
        }
    }
}

fn decl_lines(p: &mut Printer, s: &Subject) -> Printed<()> {
    let Some(sid) = &s.id else {
        return Ok(());
    };
    let id = ids::subject(&s.kind, sid);
    for assignment in &s.roles {
        let role = bp::word(&assignment.role, "role")?;
        let text = format!("role {role}{}", bp::assignment_clauses(assignment)?);
        p.leaf(1, &ids::assign(&id, role), &text, false);
    }
    for principal in &s.principals {
        p.push(1, &format!("principal {}", bp::word(principal, "principal")?));
    }
    for (key, item) in &s.attrs {
        p.push(
            1,
            &format!("attr {} = {}", bp::word(key, "attribute")?, bp::value(item)?),
        );
    }
    Ok(())
}

fn rule_lines(p: &mut Printer, run: &[Grant]) -> Printed<()> {
    let mut current: Option<&Anchor> = None;
    for g in run {
        if g.scope.as_ref() != current {
            current = g.scope.as_ref();
            if let Some(scope) = current {
                p.push(1, &format!("in {}", bp::anchor(scope)?));
            }
        }
        let effect = match g.effect {
            Effect::Allow => "allow",
            Effect::Deny => "deny",
            #[allow(unreachable_patterns, reason = "Effect may grow")]
            _ => {
                return Err(PrintError::Unrepresentable {
                    what: "a grant".to_string(),
                    reason: "the syntax does not know its effect".to_string(),
                });
            }
        };
        let name = bp::word(&g.id, "grant id")?;
        let target = bp::anchor(&Anchor {
            kind: g.target.kind.clone(),
            id: g.target.id.clone(),
        })?;
        let mut text = format!("{effect} {} on {target}", bp::word(&g.permission, "permission")?);
        if let Some(when) = &g.condition {
            text.push_str(&format!(" when {}", bp::condition(when, 0)?));
        }
        if let Some(from) = g.valid_from {
            text.push_str(&format!(" from {}", bp::date(from)?));
        }
        if let Some(until) = g.valid_until {
            text.push_str(&format!(" until {}", bp::date(until)?));
        }
        text.push_str(&format!(" as {name}"));
        p.leaf(if g.scope.is_some() { 2 } else { 1 }, &ids::grant(name), &text, false);
    }
    Ok(())
}

/// A section heading with its lines, each `(element id, text)`. A section with no line is left out.
fn block(p: &mut Printer, name: &str, lines: Vec<(String, String)>) {
    if lines.is_empty() {
        return;
    }
    p.blank();
    p.push(0, name);
    for (id, text) in lines {
        p.leaf(1, &id, &text, false);
    }
}

fn sections(p: &mut Printer, policy: &Policy) -> Printed<()> {
    let items = rules::header_items(p, policy)?;
    let (catalog, engine): (Vec<_>, Vec<_>) = items
        .into_iter()
        .partition(|(id, _)| *id == ids::TARGET_KINDS || *id == ids::BROAD_TARGETS);
    let mut lines: Vec<(String, String)> = catalog.into_iter().map(|(id, text)| (id.to_string(), text)).collect();
    for def in &policy.catalog.permissions {
        lines.push(rules::permission_text(def)?);
    }
    block(p, "catalog", lines);

    match &policy.relations {
        Some(relations) if relations.is_empty() => {
            p.blank();
            p.leaf(0, ids::RELATIONS, "relations", false);
        }
        Some(relations) => {
            let lines: Printed<Vec<_>> = relations.iter().map(rules::relation_text).collect();
            block(p, "relations", lines?);
        }
        None => {}
    }
    let tuples: Printed<Vec<_>> = policy.tuples.iter().map(rules::tuple_text).collect();
    block(p, "tuples", tuples?);

    let mut lines: Vec<(String, String)> = engine.into_iter().map(|(id, text)| (id.to_string(), text)).collect();
    for spec in &policy.overrides {
        lines.push(rules::override_text(spec)?);
    }
    block(p, "engine", lines);

    let mut ordinals = BTreeMap::new();
    let mut lines = Vec::new();
    for spec in &policy.invariants {
        lines.push(rules::invariant_text(spec, &mut ordinals)?);
    }
    block(p, "governance", lines);
    Ok(())
}