Skip to main content

tilo_actions/
framework.rs

1//! Changes the framework makes to a record, which no app action produces.
2//!
3//! Every change a device signs names what it does: an app's verb or field
4//! edit names itself in its history entry ([`crate::ActionContext`]), and a
5//! change the framework writes on its own names one of these in its commit
6//! message, so a receiver can decide it against the author's grants like
7//! any other.
8
9use serde::{Deserialize, Serialize};
10
11/// Prefix of a framework change's commit message.
12pub const FRAMEWORK_MESSAGE_PREFIX: &str = "tilo.framework/1 ";
13
14/// A change the framework writes rather than an app's action.
15#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
16#[serde(rename_all = "kebab-case")]
17pub enum FrameworkChange {
18    /// What a brand-new record starts with: its genesis and first content.
19    Genesis,
20    /// A storage migration to the model's current version. A record whose
21    /// model has a version to name writes the step as a history entry
22    /// ([`crate::HistoryOp::Migrate`]); one whose model has none names this
23    /// message.
24    Migrate,
25    /// An empty container or the initial text every member derives the
26    /// same way, made the first time the record needs it.
27    Structure,
28    /// The record leaving its workspace for another, or arriving in one.
29    Transfer,
30    /// A newer package of the record's app now decides its changes: the
31    /// record's identity names that package's surface.
32    Upgrade,
33    /// A marker recording one instruction that changed several records as
34    /// one run in the record's history. It carries no content and is
35    /// decided as a checkpoint.
36    Run,
37    /// A window's direct edit of the record that names no finer action
38    /// (widget input outside a bound action). It is decided as an edit of
39    /// the whole record.
40    Native,
41}
42
43impl FrameworkChange {
44    /// The commit message naming this change.
45    #[must_use]
46    pub fn change_message(self) -> String {
47        let name = match self {
48            Self::Genesis => "genesis",
49            Self::Migrate => "migrate",
50            Self::Structure => "structure",
51            Self::Transfer => "transfer",
52            Self::Run => "run",
53            Self::Upgrade => "upgrade",
54            Self::Native => "native",
55        };
56        format!("{FRAMEWORK_MESSAGE_PREFIX}{name}")
57    }
58
59    /// The framework change a commit message names, if it names one. Read through
60    /// [`crate::stamp::Stamp::read`] or [`crate::stamp::Label::of`], the one reader of labels.
61    #[must_use]
62    pub(crate) fn from_change_message(message: &str) -> Option<Self> {
63        Some(match message.strip_prefix(FRAMEWORK_MESSAGE_PREFIX)? {
64            "genesis" => Self::Genesis,
65            "migrate" => Self::Migrate,
66            "structure" => Self::Structure,
67            "transfer" => Self::Transfer,
68            "run" => Self::Run,
69            "upgrade" => Self::Upgrade,
70            "native" => Self::Native,
71            _ => return None,
72        })
73    }
74}
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79
80    #[test]
81    fn a_framework_change_reads_back_from_its_message() {
82        for change in [
83            FrameworkChange::Genesis,
84            FrameworkChange::Migrate,
85            FrameworkChange::Structure,
86            FrameworkChange::Transfer,
87            FrameworkChange::Upgrade,
88            FrameworkChange::Run,
89            FrameworkChange::Native,
90        ] {
91            assert_eq!(
92                FrameworkChange::from_change_message(&change.change_message()),
93                Some(change)
94            );
95        }
96        assert_eq!(
97            FrameworkChange::from_change_message("tilo.entry/1 {}"),
98            None
99        );
100        assert_eq!(
101            FrameworkChange::from_change_message("tilo.framework/1 other"),
102            None
103        );
104    }
105}