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
//! Changes the framework makes to a record, which no app action produces.
//!
//! Every change a device signs names what it does: an app's verb or field
//! edit names itself in its history entry ([`crate::ActionContext`]), and a
//! change the framework writes on its own names one of these in its commit
//! message, so a receiver can decide it against the author's grants like
//! any other.
use serde::{Deserialize, Serialize};
/// Prefix of a framework change's commit message.
pub const FRAMEWORK_MESSAGE_PREFIX: &str = "tilo.framework/1 ";
/// A change the framework writes rather than an app's action.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum FrameworkChange {
/// What a brand-new record starts with: its genesis and first content.
Genesis,
/// A storage migration to the model's current version. Written with a
/// history entry ([`crate::HistoryOp::Migrate`]), not this message.
Migrate,
/// An empty container or the initial text every member derives the
/// same way, made the first time the record needs it.
Structure,
/// The record leaving its workspace for another, or arriving in one.
Transfer,
/// A newer package of the record's app now decides its changes: the
/// record's identity names that package's surface.
Upgrade,
/// A marker recording one instruction that changed several records as
/// one run in the record's history. It carries no content and is
/// decided as a checkpoint.
Run,
/// A window's direct edit of the record that names no finer action
/// (widget input outside a bound action). It is decided as an edit of
/// the whole record.
Native,
}
impl FrameworkChange {
/// The commit message naming this change.
#[must_use]
pub fn change_message(self) -> String {
let name = match self {
Self::Genesis => "genesis",
Self::Migrate => "migrate",
Self::Structure => "structure",
Self::Transfer => "transfer",
Self::Run => "run",
Self::Upgrade => "upgrade",
Self::Native => "native",
};
format!("{FRAMEWORK_MESSAGE_PREFIX}{name}")
}
/// The framework change a commit message names, if it names one. Read through
/// [`crate::stamp::Stamp::read`] or [`crate::stamp::Label::of`], the one reader of labels.
#[must_use]
pub(crate) fn from_change_message(message: &str) -> Option<Self> {
Some(match message.strip_prefix(FRAMEWORK_MESSAGE_PREFIX)? {
"genesis" => Self::Genesis,
"migrate" => Self::Migrate,
"structure" => Self::Structure,
"transfer" => Self::Transfer,
"run" => Self::Run,
"upgrade" => Self::Upgrade,
"native" => Self::Native,
_ => return None,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_framework_change_reads_back_from_its_message() {
for change in [
FrameworkChange::Genesis,
FrameworkChange::Migrate,
FrameworkChange::Structure,
FrameworkChange::Transfer,
FrameworkChange::Upgrade,
FrameworkChange::Run,
FrameworkChange::Native,
] {
assert_eq!(
FrameworkChange::from_change_message(&change.change_message()),
Some(change)
);
}
assert_eq!(
FrameworkChange::from_change_message("tilo.entry/1 {}"),
None
);
assert_eq!(
FrameworkChange::from_change_message("tilo.framework/1 other"),
None
);
}
}