Skip to main content

zaun_community_protocol/permissions/
resolved.rs

1//! The result of one resolution, with enough provenance to explain a denial.
2
3use crate::bits::Permissions;
4
5use super::denial::{Denial, DenialReason};
6use super::resolve::with_dependants;
7
8/// The masks each step 7 restriction removed, in application order.
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
10pub struct Steps {
11    /// 7a: everything but `TIMEOUT_KEEP` while timed out.
12    pub timeout: Permissions,
13    /// 7b: the newcomer mask during probation and the waiting-period mask.
14    pub probation: Permissions,
15    /// 7c: the active lockdown's mask for this member.
16    pub lockdown: Permissions,
17    /// 7d: `LOCK_MASK` when the channel flag applies.
18    pub lock: Permissions,
19}
20
21/// One resolved permission set (`cp` or `chp`).
22#[derive(Debug, Clone, Copy, PartialEq, Eq)]
23pub struct Resolved {
24    /// The effective permissions after step 7. Always a subset of `KNOWN`.
25    pub effective: Permissions,
26    /// The grants of steps 1–6 (after dependencies), before any restriction.
27    pub grants: Permissions,
28    /// The permissions after steps 7a–7b but before the lockdown (7c) and the `locked` flag
29    /// (7d). H3 for lockdown management compares against this, so a lockdown never prevents
30    /// its own management.
31    pub before_lockdown: Permissions,
32    /// What each restriction removed.
33    pub steps: Steps,
34    /// Whether step 7a applied.
35    pub timed_out: bool,
36    /// Whether the waiting period applied.
37    pub waiting: bool,
38}
39
40impl Resolved {
41    /// A result with no restriction (owner, or a member the state gate zeroes).
42    #[must_use]
43    pub fn unrestricted(p: Permissions) -> Self {
44        Self {
45            effective: p,
46            grants: p,
47            before_lockdown: p,
48            steps: Steps::default(),
49            timed_out: false,
50            waiting: false,
51        }
52    }
53
54    /// Whether every bit of `bits` is effective.
55    #[must_use]
56    pub const fn has(&self, bits: Permissions) -> bool {
57        self.effective.contains(bits)
58    }
59
60    /// `Ok` when every bit of `bits` is effective, else a denial naming the missing bits.
61    pub fn require(&self, bits: Permissions) -> Result<(), Denial> {
62        let missing = bits - self.effective;
63        if missing.is_empty() {
64            Ok(())
65        } else {
66            Err(self.explain(missing))
67        }
68    }
69
70    /// `Ok` when at least one bit of `bits` is effective (or `bits` is empty).
71    pub fn require_any(&self, bits: Permissions) -> Result<(), Denial> {
72        if bits.is_empty() || self.effective.intersects(bits) {
73            Ok(())
74        } else {
75            Err(self.explain(bits))
76        }
77    }
78
79    /// Attributes missing bits to a restriction. A reason is given only when the
80    /// restrictions are the whole cause (every missing bit was granted); then it names the
81    /// first step, in order 7a–7d, that removed one of them or its prerequisite.
82    #[must_use]
83    pub fn explain(&self, missing: Permissions) -> Denial {
84        if !self.grants.contains(missing) {
85            return Denial::missing(missing);
86        }
87        let s = &self.steps;
88        let order = [
89            (s.timeout, DenialReason::Timeout),
90            (s.probation, DenialReason::Probation),
91            (s.lockdown, DenialReason::Lockdown),
92            (s.lock, DenialReason::ChannelLocked),
93        ];
94        let reason = order
95            .into_iter()
96            .find(|(mask, _)| with_dependants(*mask).intersects(missing))
97            .map(|(_, r)| r);
98        Denial { missing, reason }
99    }
100}