Skip to main content

zaun_community_protocol/permissions/
table.rs

1//! The command table (permissions §6, specification §8): which permissions each command
2//! needs. Further hierarchy checks live in the management modules.
3
4mod community;
5mod content;
6
7use super::requirement::Requirement;
8
9/// Every command of the table with its requirement, in specification order.
10pub const TABLE: &[(&str, Requirement)] = &concat::<LEN>(community::ROWS, content::ROWS);
11
12const LEN: usize = community::ROWS.len() + content::ROWS.len();
13
14const fn concat<const N: usize>(
15    a: &[(&'static str, Requirement)],
16    b: &[(&'static str, Requirement)],
17) -> [(&'static str, Requirement); N] {
18    let mut out = [a[0]; N];
19    let mut i = 0;
20    while i < a.len() {
21        out[i] = a[i];
22        i += 1;
23    }
24    while i < N {
25        out[i] = b[i - a.len()];
26        i += 1;
27    }
28    out
29}
30
31/// The requirement of a command by wire name; `None` for a name the table does not know
32/// (the engine answers `UNSUPPORTED_COMMAND`).
33#[must_use]
34pub fn required(command: &str) -> Option<Requirement> {
35    TABLE
36        .iter()
37        .find(|(name, _)| *name == command)
38        .map(|(_, r)| *r)
39}
40
41/// Every command name of the table.
42pub fn commands() -> impl Iterator<Item = &'static str> {
43    TABLE.iter().map(|(name, _)| *name)
44}