Skip to main content

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