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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// @generated from the MSP stable schema and protocol sources; do not edit by hand.
pub use code_mode::CodeModeReceipt;
use serde::Deserialize;
use serde::Deserializer;
use serde::Serialize;
use serde::Serializer;
pub use vocabulary::ToolReceiptOperation;
pub use vocabulary::ToolReceiptOutcome;
pub(crate) mod code_mode;
pub(crate) mod vocabulary;
/// One row of the Cron `list` receipt's `jobs` array.
///
/// Server order, and **`active` + `firing` rows only**. A small fixed-shape
/// array of scalars, which is what keeps it a bounded machine fact.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct CronReceiptJob {
/// The job's server-owned id.
pub job_id: String,
/// Absolute epoch-milliseconds of the job's next scheduled fire.
pub next_fire_at_ms: i64,
}
/// Cron's `details` payload.
///
/// Every field a family owns lives here, under the container's one `details`
/// key, rather than flat beside the discriminators. Each is a **bounded machine
/// fact**: scalars, short identifiers and a small fixed-shape array of those.
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct CronReceipt {
/// Cron `create` and `delete`: the job id. On `delete`'s `missing` arm this
/// is the caller's own id echoed back — that arm has no server-side subject
/// to name.
///
/// On the `created` + `cancelled` pairing, this field also conveys the
/// hidden fact: a job row was committed and will fire.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub job_id: Option<String>,
/// Cron `create` (`created`, `duplicate`): absolute epoch-milliseconds of
/// the job's next scheduled fire.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub next_fire_at_ms: Option<i64>,
/// Cron `list`: the served jobs in server order, `active` and `firing`
/// rows only.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub jobs: Option<Vec<CronReceiptJob>>,
}
/// The family-selected body of a receipt: the `family` discriminator and the
/// one `details` object whose shape that discriminator chooses.
///
/// Adjacently tagged in spirit — `tag = "family"`, `content = "details"` —
/// but written by hand rather than derived, for one reason: serde's
/// `#[serde(other)]` fallback admits only a **unit** variant, which would
/// discard both the unrecognized family word and its `details`. The open
/// vocabulary requires the opposite: an unknown value is preserved verbatim so
/// a client falls back only for the part it does not recognize. The `Unknown`
/// arm therefore keeps both.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum ToolReceiptBody {
/// `family: "cron"`.
Cron(CronReceipt),
/// `family: "codeMode"`.
CodeMode(CodeModeReceipt),
/// A family minted by a newer decision than this build knows. Both the
/// word and its `details` are kept exactly as served; nothing about the
/// payload is assumed, because only the owning family's decision defines
/// it.
///
/// **Never construct this with a registered word.** A producer serving cron
/// builds [`Self::Cron`]; `Unknown { family: "cron",.. }` encodes to the
/// same wire bytes but does **not** survive a round-trip — decoding selects
/// the typed arm, so a payload outside `CronReceipt`'s members is silently
/// dropped. The arm exists only to carry words from newer builds, and
/// decoding never produces it for a word this build knows.
Unknown {
/// The family word, verbatim.
family: String,
/// Whatever that family put under `details`, if anything. A family
/// with no fields omits the key.
///
/// A `Map`, not a `Value`: the schema's open arm types `details` as
/// an object, so accepting `[1]` or `null` here would let this type
/// re-encode a frame the published bundle calls invalid. A
/// non-object fails at decode instead.
details: Option<serde_json::Map<String, serde_json::Value>>,
},
}
/// The shared `toolReceipt` container.
///
/// `family`, `operation` and `outcome` are required together and sit at the
/// top level; `body` is flattened, so `family` and `details` are emitted
/// beside the other two rather than nested.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct ToolReceipt {
/// Which operation it describes. Required with the other two.
pub operation: ToolReceiptOperation,
/// What the operation did. Required with the other two.
pub outcome: ToolReceiptOutcome,
/// The `family` discriminator and its `details` payload.
#[serde(flatten)]
pub body: ToolReceiptBody,
}
impl Serialize for ToolReceiptBody {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
use serde::ser::SerializeMap;
match self {
Self::Cron(cron) => {
let empty = cron == &CronReceipt::default();
let mut map = serializer.serialize_map(Some(1 + usize::from(!empty)))?;
map.serialize_entry("family", "cron")?;
if !empty {
map.serialize_entry("details", cron)?;
}
map.end()
}
Self::CodeMode(code_mode) => {
let empty = code_mode.serializes_empty();
let mut map = serializer.serialize_map(Some(1 + usize::from(!empty)))?;
map.serialize_entry("family", "codeMode")?;
if !empty {
map.serialize_entry("details", code_mode)?;
}
map.end()
}
Self::Unknown { family, details } => {
let mut map = serializer.serialize_map(Some(1 + usize::from(details.is_some())))?;
map.serialize_entry("family", family)?;
if let Some(details) = details {
map.serialize_entry("details", details)?;
}
map.end()
}
}
}
}
impl<'de> Deserialize<'de> for ToolReceiptBody {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
struct BodyVisitor;
impl<'de> serde::de::Visitor<'de> for BodyVisitor {
type Value = ToolReceiptBody;
fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.write_str("a toolReceipt body with a `family` discriminator")
}
fn visit_map<M: serde::de::MapAccess<'de>>(
self,
mut map: M,
) -> Result<Self::Value, M::Error> {
let mut family: Option<String> = None;
let mut details: Option<serde_json::Map<String, serde_json::Value>> = None;
while let Some(key) = map.next_key::<String>()? {
match key.as_str() {
"family" => {
if family.is_some() {
return Err(serde::de::Error::duplicate_field("family"));
}
family = Some(map.next_value()?);
}
"details" => {
if details.is_some() {
return Err(serde::de::Error::duplicate_field("details"));
}
details = Some(map.next_value()?);
}
_ => {
let _ = map.next_value::<serde::de::IgnoredAny>()?;
}
}
}
let family = family.ok_or_else(|| serde::de::Error::missing_field("family"))?;
if family == "cron" {
let cron = match details {
Some(value) => serde_json::from_value(serde_json::Value::Object(value))
.map_err(serde::de::Error::custom)?,
None => CronReceipt::default(),
};
return Ok(ToolReceiptBody::Cron(cron));
}
if family == "codeMode" {
let code_mode = match details {
Some(value) => serde_json::from_value(serde_json::Value::Object(value))
.map_err(serde::de::Error::custom)?,
None => CodeModeReceipt::default(),
};
return Ok(ToolReceiptBody::CodeMode(code_mode));
}
Ok(ToolReceiptBody::Unknown { family, details })
}
}
deserializer.deserialize_map(BodyVisitor)
}
}