muse_code_sdk/msp/view/tool_receipt.rs
1// @generated from the MSP stable schema and protocol sources; do not edit by hand.
2
3pub use code_mode::CodeModeReceipt;
4use serde::Deserialize;
5use serde::Deserializer;
6use serde::Serialize;
7use serde::Serializer;
8pub use vocabulary::ToolReceiptOperation;
9pub use vocabulary::ToolReceiptOutcome;
10
11pub(crate) mod code_mode;
12pub(crate) mod vocabulary;
13
14/// One row of the Cron `list` receipt's `jobs` array.
15///
16/// Server order, and **`active` + `firing` rows only** — the owner's `Serve
17/// active+firing only` directive. A small fixed-shape array of scalars, which
18/// is what keeps it inside the governing rules bounded-machine-fact rule.
19#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
20#[serde(rename_all = "camelCase")]
21#[non_exhaustive]
22pub struct CronReceiptJob {
23 /// The job's server-owned id.
24 pub job_id: String,
25 /// Absolute epoch-milliseconds of the job's next scheduled fire.
26 pub next_fire_at_ms: i64,
27}
28
29/// Cron's `details` payload.
30///
31/// Every field a family owns lives here, under the container's one `details`
32/// key, rather than flat beside the discriminators — the governing rules as
33/// amended. Each is a **bounded machine fact**: scalars, short identifiers and
34/// a small fixed-shape array of those.
35#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
36#[serde(rename_all = "camelCase")]
37#[non_exhaustive]
38pub struct CronReceipt {
39 /// Cron `create` and `delete`: the job id. On `delete`'s `missing` arm this
40 /// is the caller's own id echoed back under the governing rules declared
41 /// echo — that arm has no server-side subject to name.
42 ///
43 /// Also the field governing rules declaration names as conveying the hidden
44 /// fact on the declared `created` + `cancelled` pairing: a job row was
45 /// committed and will fire.
46 #[serde(default, skip_serializing_if = "Option::is_none")]
47 pub job_id: Option<String>,
48 /// Cron `create` (`created`, `duplicate`): absolute epoch-milliseconds of
49 /// the job's next scheduled fire.
50 #[serde(default, skip_serializing_if = "Option::is_none")]
51 pub next_fire_at_ms: Option<i64>,
52 /// Cron `list`: the served jobs in server order, `active` and `firing`
53 /// rows only.
54 #[serde(default, skip_serializing_if = "Option::is_none")]
55 pub jobs: Option<Vec<CronReceiptJob>>,
56}
57
58/// The family-selected body of a receipt: the `family` discriminator and the
59/// one `details` object whose shape that discriminator chooses.
60///
61/// Adjacently tagged in spirit — `tag = "family"`, `content = "details"` — but
62/// written by hand rather than derived, for one reason: serde's
63/// `#[serde(other)]` fallback admits only a **unit** variant, which would
64/// discard both the unrecognized family word and its `details`. the governing
65/// rules openness requires the opposite: an unknown value is preserved verbatim
66/// so a client falls back only for the part it does not recognize. The
67/// `Unknown` arm therefore keeps both.
68#[derive(Debug, Clone, PartialEq)]
69#[non_exhaustive]
70pub enum ToolReceiptBody {
71 /// `family: "cron"`.
72 Cron(CronReceipt),
73 /// `family: "codeMode"`.
74 CodeMode(CodeModeReceipt),
75 /// A family minted by a newer decision than this build knows. Both the
76 /// word and its `details` are kept exactly as served; nothing about the
77 /// payload is assumed, because only the owning family's decision defines
78 /// it.
79 ///
80 /// **Never construct this with a registered word.** A producer serving cron
81 /// builds [`Self::Cron`]; `Unknown { family: "cron",.. }` encodes to the
82 /// same wire bytes but does **not** survive a round-trip — decoding selects
83 /// the typed arm, so a payload outside `CronReceipt`'s members is silently
84 /// dropped. The arm exists only to carry words from newer builds, and
85 /// decoding never produces it for a word this build knows.
86 Unknown {
87 /// The family word, verbatim.
88 family: String,
89 /// Whatever that family put under `details`, if anything. A family
90 /// with no fields omits the key.
91 ///
92 /// A `Map`, not a `Value`: the schema's open arm types `details` as
93 /// an object, so accepting `[1]` or `null` here would let this type
94 /// re-encode a frame the published bundle calls invalid. A
95 /// non-object fails at decode instead.
96 details: Option<serde_json::Map<String, serde_json::Value>>,
97 },
98}
99
100/// The shared `toolReceipt` container.
101///
102/// `family`, `operation` and `outcome` are required together and sit at the
103/// top level; `body` is flattened, so `family` and `details` are emitted
104/// beside the other two rather than nested.
105#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
106#[serde(rename_all = "camelCase")]
107#[non_exhaustive]
108pub struct ToolReceipt {
109 /// Which operation it describes. Required with the other two.
110 pub operation: ToolReceiptOperation,
111 /// What the operation did. Required with the other two.
112 pub outcome: ToolReceiptOutcome,
113 /// The `family` discriminator and its `details` payload.
114 #[serde(flatten)]
115 pub body: ToolReceiptBody,
116}
117
118impl Serialize for ToolReceiptBody {
119 fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
120 use serde::ser::SerializeMap;
121 match self {
122 Self::Cron(cron) => {
123 let empty = cron == &CronReceipt::default();
124 let mut map = serializer.serialize_map(Some(1 + usize::from(!empty)))?;
125 map.serialize_entry("family", "cron")?;
126 if !empty {
127 map.serialize_entry("details", cron)?;
128 }
129 map.end()
130 }
131 Self::CodeMode(code_mode) => {
132 let empty = code_mode.serializes_empty();
133 let mut map = serializer.serialize_map(Some(1 + usize::from(!empty)))?;
134 map.serialize_entry("family", "codeMode")?;
135 if !empty {
136 map.serialize_entry("details", code_mode)?;
137 }
138 map.end()
139 }
140 Self::Unknown { family, details } => {
141 let mut map = serializer.serialize_map(Some(1 + usize::from(details.is_some())))?;
142 map.serialize_entry("family", family)?;
143 if let Some(details) = details {
144 map.serialize_entry("details", details)?;
145 }
146 map.end()
147 }
148 }
149 }
150}
151
152impl<'de> Deserialize<'de> for ToolReceiptBody {
153 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
154 struct BodyVisitor;
155
156 impl<'de> serde::de::Visitor<'de> for BodyVisitor {
157 type Value = ToolReceiptBody;
158
159 fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
160 f.write_str("a toolReceipt body with a `family` discriminator")
161 }
162
163 fn visit_map<M: serde::de::MapAccess<'de>>(
164 self,
165 mut map: M,
166 ) -> Result<Self::Value, M::Error> {
167 let mut family: Option<String> = None;
168 let mut details: Option<serde_json::Map<String, serde_json::Value>> = None;
169 while let Some(key) = map.next_key::<String>()? {
170 match key.as_str() {
171 "family" => {
172 if family.is_some() {
173 return Err(serde::de::Error::duplicate_field("family"));
174 }
175 family = Some(map.next_value()?);
176 }
177 "details" => {
178 if details.is_some() {
179 return Err(serde::de::Error::duplicate_field("details"));
180 }
181 details = Some(map.next_value()?);
182 }
183 _ => {
184 let _ = map.next_value::<serde::de::IgnoredAny>()?;
185 }
186 }
187 }
188 let family = family.ok_or_else(|| serde::de::Error::missing_field("family"))?;
189 if family == "cron" {
190 let cron = match details {
191 Some(value) => serde_json::from_value(serde_json::Value::Object(value))
192 .map_err(serde::de::Error::custom)?,
193 None => CronReceipt::default(),
194 };
195 return Ok(ToolReceiptBody::Cron(cron));
196 }
197 if family == "codeMode" {
198 let code_mode = match details {
199 Some(value) => serde_json::from_value(serde_json::Value::Object(value))
200 .map_err(serde::de::Error::custom)?,
201 None => CodeModeReceipt::default(),
202 };
203 return Ok(ToolReceiptBody::CodeMode(code_mode));
204 }
205 Ok(ToolReceiptBody::Unknown { family, details })
206 }
207 }
208
209 deserializer.deserialize_map(BodyVisitor)
210 }
211}