Skip to main content

tilo_actions/remote/
changes.rs

1//! What changed in a workspace's records, as the provider that keeps them saw
2//! it: every change to every record, with who made it and in which run, for
3//! a caller following the whole workspace rather than one resource.
4//!
5//! A record's own `history` verb lists its changes to whoever reads that
6//! record. A caller watching the workspace (an agent's `tilo_watch` with no
7//! path, the API's event stream) needs every record's, including records it
8//! never named, so the provider that holds the records answers from what it
9//! holds: [`ChangeFeed`], offered through [`super::ActionProvider::change_feed`]
10//! as the roster is through [`super::ActionProvider::here`], and asked across the
11//! wire with [`super::RemoteActionMessage::ChangesRequest`].
12
13use serde::{Deserialize, Serialize};
14
15use super::{ActionCallError, Caller, Pending};
16
17/// What a provider without a change feed answers.
18pub const CHANGE_FEED_UNAVAILABLE: &str =
19    "this provider keeps no feed of its records' changes; watch one resource by its path instead";
20
21/// What a change did to the resource it is a change of: it made it, edited it, or removed it. The
22/// store reads it from the recorded change, and every watcher of the workspace is told it beside
23/// the change's id, so no app works it out from what it saw before. A change always names one:
24/// a change that says nothing is a fault, never an edit.
25#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
26#[serde(rename_all = "kebab-case")]
27pub enum What {
28    /// The resource's first recorded change, which made it.
29    Created,
30    /// A change to the resource itself.
31    Edited,
32    /// The change that removed it.
33    Removed,
34}
35
36impl What {
37    /// The word an event carries it as: `created`, `edited` or `removed`.
38    #[must_use]
39    pub const fn word(self) -> &'static str {
40        match self {
41            Self::Created => "created",
42            Self::Edited => "edited",
43            Self::Removed => "removed",
44        }
45    }
46
47    /// The [`Self::word`] `word` names.
48    #[must_use]
49    pub fn of_word(word: &str) -> Option<Self> {
50        match word {
51            "created" => Some(Self::Created),
52            "edited" => Some(Self::Edited),
53            "removed" => Some(Self::Removed),
54            _ => None,
55        }
56    }
57}
58
59/// One change to one record of the workspace.
60#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
61pub struct ResourceChanged {
62    /// The app whose record it is.
63    pub app: String,
64    /// The record's id there; empty for the app's record of the workspace.
65    pub record: String,
66    /// The change as the record's `history` lists it: its `change` id, its
67    /// `sentence`, `time_ms`, who made it (`actor`, `by`), the group it was a
68    /// step of (`title`, `group`, `group_kind`), the invocation that made
69    /// it (`invocation`), and what it did to the resource (`happened`, a [`What`]).
70    pub entry: serde_json::Value,
71}
72
73/// The changes after a cursor, oldest first, and the cursor after them.
74#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
75pub struct ChangePage {
76    pub changes: Vec<ResourceChanged>,
77    /// Opaque: handed back as it is to read on from here.
78    pub cursor: String,
79}
80
81/// A provider's feed of every change to the records of a caller's workspace.
82pub trait ChangeFeed: Send + Sync {
83    /// The changes `caller` may read made after `after`, a cursor an earlier
84    /// page answered; `None` starts from now, answering no changes and the
85    /// cursor to read on from. A cursor this provider cannot read on from (it
86    /// restarted, or the changes it named are no longer kept) answers from
87    /// now too, so a follower misses changes rather than reading old ones
88    /// twice.
89    ///
90    /// # Errors
91    /// The feed refuses `caller`, or the workspace cannot be read now.
92    fn since(
93        &self,
94        caller: &Caller,
95        after: Option<&str>,
96    ) -> Pending<Result<ChangePage, ActionCallError>>;
97}