Skip to main content

tilo_action_policy/
readers.rs

1//! Who reads a record of a kind, when not every member of the workspace does.
2
3use serde::{Deserialize, Serialize};
4
5/// Who reads a record of a kind, when it is not every member: what a tilolang type says with
6/// `(readers: ...)` and a Rust resource with `#[resource(readers = "...")]`, carried in the
7/// kind's published surface. The store works out the reader set from it, the record's author and
8/// its fields, and places the record in a document of that set alone, sealed to its key group; a
9/// record whose set changes moves to the new set's group, sealed afresh. Whoever left the set
10/// keeps what they already held of it, its ciphertext and the keys they had, as after any
11/// revocation, and nothing written after.
12#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
13#[serde(rename_all = "snake_case")]
14pub enum ReaderRule {
15    /// `author`, `author until sent`: the person who made the record, on each of their devices;
16    /// every member once the `Bool` field `until` names holds, which it never stops holding (a
17    /// draft until it is sent).
18    Author {
19        #[serde(default, skip_serializing_if = "Option::is_none")]
20        until: Option<String>,
21    },
22    /// A field's name: the people its `Set<Actor>` names.
23    Field { field: String },
24    /// `author and moderator`: the person who made the record, and every member whose role
25    /// includes `role` ([`crate::Role::includes`]) whenever it is read. Who holds the role is
26    /// the workspace's governance's to say at each moment, never written into the record: a
27    /// member given the role reads what was written before, and one who loses it or leaves
28    /// reads nothing written after.
29    AuthorAnd { role: crate::Role },
30}
31
32impl ReaderRule {
33    /// The rule a type's options write as `words` (`author`, `author until sent`,
34    /// `author and moderator`, or a field's name); `None` when they name no rule.
35    #[must_use]
36    pub fn parse(words: &str) -> Option<Self> {
37        let field = |word: &str| {
38            !word.is_empty() && word.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
39        };
40        match words.split_whitespace().collect::<Vec<_>>().as_slice() {
41            ["author"] => Some(Self::Author { until: None }),
42            ["author", "until", until] if field(until) => Some(Self::Author {
43                until: Some((*until).to_owned()),
44            }),
45            ["author", "and", role] => {
46                crate::Role::parse(role).map(|role| Self::AuthorAnd { role })
47            }
48            [name] if field(name) && *name != "author" => Some(Self::Field {
49                field: (*name).to_owned(),
50            }),
51            _ => None,
52        }
53    }
54}
55
56impl std::fmt::Display for ReaderRule {
57    /// The rule as a type's options write it.
58    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59        match self {
60            Self::Author { until: None } => f.write_str("author"),
61            Self::Author { until: Some(field) } => write!(f, "author until {field}"),
62            Self::Field { field } => f.write_str(field),
63            Self::AuthorAnd { role } => write!(f, "author and {}", role.key()),
64        }
65    }
66}