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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//! The [`ActionRegistry`]: `type_id` → decoder, so a replaying peer can
//! turn an [`EncodedAction`] back into a live `Box<dyn Action>` without
//! knowing the concrete type at the call site. An app registers each
//! action type once; the registry is the same boundary the OS-wide
//! global-search backend plugs into to enumerate action *types* across apps.
use std::collections::HashMap;
use serde::de::DeserializeOwned;
use crate::action::{Action, ActionError, EncodedAction};
type Decoder = Box<dyn Fn(&[u8]) -> Result<Box<dyn Action>, ActionError> + Send + Sync>;
/// Decodes one action type's payload ([`ActionRegistry::register_decoder`]).
pub type DecodePayload = fn(&[u8]) -> Result<Box<dyn Action>, serde_json::Error>;
/// Maps a stable `type_id` to the decoder that reconstructs its action
/// from a payload — cheap to share behind an `Arc`/`&` at dispatch time.
#[derive(Default)]
pub struct ActionRegistry {
decoders: HashMap<String, Decoder>,
/// Where the large parts of a recorded action are read from, by
/// reference ([`crate::content`]): the content store of the record whose
/// change names them, which asks the record's route for what this device
/// lacks.
content: Option<tilo_content::ContentStore>,
}
impl ActionRegistry {
pub fn new() -> Self {
Self::default()
}
/// A registry holding what `register` adds, such as an app's action set.
#[must_use]
pub fn with(register: impl FnOnce(&mut Self)) -> Self {
let mut registry = Self::new();
register(&mut registry);
registry
}
/// Register `A` under `type_id`; a later [`decode`](Self::decode) of an
/// [`EncodedAction`] carrying this `type_id` deserializes the payload
/// into an `A` and boxes it as `dyn Action`.
pub fn register<A>(&mut self, type_id: impl Into<String>)
where
A: Action + DeserializeOwned + 'static,
{
let type_id = type_id.into();
let tid = type_id.clone();
self.decoders.insert(
type_id,
Box::new(move |bytes: &[u8]| {
let action: A = decode_json(bytes).map_err(|e| ActionError::Decode {
type_id: tid.clone(),
source: e,
})?;
Ok(Box::new(action) as Box<dyn Action>)
}),
);
}
/// Register `decode` under `type_id`, for an action type that decodes
/// its payload itself, such as one generic type whose payload one
/// non-generic function decodes for every instance.
pub fn register_decoder(&mut self, type_id: impl Into<String>, decode: DecodePayload) {
let type_id = type_id.into();
let tid = type_id.clone();
self.decoders.insert(
type_id,
Box::new(move |bytes: &[u8]| {
decode(bytes).map_err(|e| ActionError::Decode {
type_id: tid.clone(),
source: e,
})
}),
);
}
/// Reconstruct a live action from its encoded form.
///
/// # Errors
///
/// Returns [`ActionError::UnknownType`] if no decoder is registered
/// for `encoded.type_id`, or [`ActionError::Decode`] if the payload
/// fails to deserialize into the registered type.
pub fn decode(&self, encoded: &EncodedAction) -> Result<Box<dyn Action>, ActionError> {
let decoder = self
.decoders
.get(&encoded.type_id)
.ok_or_else(|| ActionError::UnknownType(encoded.type_id.clone()))?;
decoder(&crate::group_form::readable(
&encoded.type_id,
&encoded.payload,
))
}
/// Read the large parts of recorded actions from `store` — the content
/// store of the record they were made on, which keeps them sealed and
/// asks the record's route for the ones this device lacks — so a change
/// whose action names content decodes here, on whatever thread the
/// registry is used. The host connects it where it builds the registry
/// ([`crate::content::register`] does the same for code the window runs
/// in place).
pub fn connect_content(&mut self, store: tilo_content::ContentStore) {
self.content = Some(store);
}
/// The content store recorded actions read their parts from.
#[must_use]
pub fn content(&self) -> Option<&tilo_content::ContentStore> {
self.content.as_ref()
}
/// The recorded action `recorded` as it ran, its content parts read
/// from this registry's content store ([`Self::connect_content`]), or
/// from the running window's store when none is connected.
///
/// # Errors
/// [`MissingContent`](crate::MissingContent) for content not here yet
/// (inside `Ok`), or a payload that is not JSON.
pub fn resolve(
&self,
recorded: &EncodedAction,
) -> Result<Result<EncodedAction, crate::MissingContent>, ActionError> {
if recorded.content_parts().is_empty() {
return Ok(Ok(recorded.clone()));
}
match self
.content
.clone()
.or_else(crate::content::recording_store)
{
Some(store) => recorded.resolve_content(&|reference| store.read(reference)),
None => recorded.resolve_content(&|_| None),
}
}
/// Whether a decoder is registered for `type_id`.
pub fn contains(&self, type_id: &str) -> bool {
self.decoders.contains_key(type_id)
}
/// Every registered action type id (the general-scope enumeration boundary).
pub fn type_ids(&self) -> Vec<&str> {
self.decoders.keys().map(String::as_str).collect()
}
}
/// A `T` from its JSON bytes, through a JSON value: one byte parser for
/// every action type, compiled here, rather than one per type and crate.
///
/// # Errors
/// Why the bytes are not a `T`.
#[inline]
pub fn decode_json<T: DeserializeOwned>(bytes: &[u8]) -> Result<T, serde_json::Error> {
decode_payload("", bytes)
}
/// A `T` from the payload of an action of type `type_id`,
/// like [`decode_json`]: a group written with what its members share once
/// ([`crate::group_form`]) reads as the list of them.
pub fn decode_payload<T: DeserializeOwned>(
type_id: &str,
payload: &[u8],
) -> Result<T, serde_json::Error> {
T::deserialize(payload_value(type_id, payload)?)
}
/// The JSON value of the payload of an action of type `type_id`, a group's
/// as the list of its members; [`decode_json`] passes no type, so its bytes
/// read as they are. One function for both keeps every app to one import.
#[inline(never)]
fn payload_value(type_id: &str, payload: &[u8]) -> Result<serde_json::Value, serde_json::Error> {
match serde_json::from_slice(payload)? {
serde_json::Value::Object(shared) if crate::group_form::is_group(type_id) => {
crate::group_form::members_of(shared)
.map(serde_json::Value::Array)
.map_err(<serde_json::Error as serde::de::Error>::custom)
}
value => Ok(value),
}
}