tilo-actions 0.5.2

The Tilo action model: resources, the actions on them, and their registry, with the plain resource vocabulary that needs no store.
Documentation
//! 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. A record whose
    /// model has a version to name writes the step as a history entry
    /// ([`crate::HistoryOp::Migrate`]); one whose model has none names 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
        );
    }
}