Skip to main content

muse_code_sdk/msp/view/tool_receipt/
code_mode.rs

1// @generated from the MSP stable schema and protocol sources; do not edit by hand.
2
3use serde::Deserialize;
4use serde::Deserializer;
5use serde::Serialize;
6use serde::Serializer;
7
8/// The language a Code Mode cell ran in. Open: values are lowercase VS Code
9/// language identifiers, and only `javascript` (the V8 engine) is minted today.
10#[derive(Debug, Clone, PartialEq, Eq)]
11#[non_exhaustive]
12pub enum CodeModeLanguage {
13    /// The V8 engine's language.
14    Javascript,
15    /// A value minted by a newer decision than this build knows, preserved
16    /// verbatim. **Never construct this with a registered word.**
17    Unknown(String),
18}
19
20/// What a Code Mode output span's bytes are. Open: each engine mints its own
21/// values.
22#[derive(Debug, Clone, PartialEq, Eq)]
23#[non_exhaustive]
24pub enum CodeModeOutputStream {
25    /// The V8 engine's one interleaved, source-labelled output transcript.
26    Transcript,
27    /// A value minted by a newer decision than this build knows, preserved
28    /// verbatim. **Never construct this with a registered word.**
29    Unknown(String),
30}
31
32/// One public-output span inside the item's `visibleOutput`.
33///
34/// Offsets index the UTF-8 encoding of the **decoded** `visibleOutput`
35/// string, never the JSON-escaped wire text; both ends fall on code-point
36/// boundaries; named and measured like `item/readOutput`'s byte range.
37#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
38#[serde(rename_all = "camelCase")]
39#[non_exhaustive]
40pub struct CodeModeOutputSpan {
41    /// What the bytes are.
42    pub stream: CodeModeOutputStream,
43    /// Start of the span, in bytes.
44    pub offset_bytes: u64,
45    /// Length of the span, in bytes.
46    pub length_bytes: u64,
47}
48
49/// Code Mode's `details` payload.
50///
51/// Every member is absent when it does not apply, never `null`; an empty
52/// `outputSpans` list is never served.
53#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
54#[serde(rename_all = "camelCase")]
55#[non_exhaustive]
56pub struct CodeModeReceipt {
57    /// `execute`, and `wait` when the producer resolved the originating
58    /// execute: the language the cell ran in.
59    #[serde(default, skip_serializing_if = "Option::is_none")]
60    pub language: Option<CodeModeLanguage>,
61    /// `execute` only: the top-level key of the item's `args` object whose
62    /// value is the cell source. A pointer, never source bytes.
63    #[serde(default, skip_serializing_if = "Option::is_none")]
64    pub source_arg: Option<String>,
65    /// `wait` only, when the producer resolved it: the `itemId` of the
66    /// `execute` item that started the cell.
67    #[serde(default, skip_serializing_if = "Option::is_none")]
68    pub cell_item_id: Option<String>,
69    /// The public cell-output spans, ascending and non-overlapping; absent
70    /// when there is no cell body or a hook rewrote the served body.
71    #[serde(default, skip_serializing_if = "spans_absent")]
72    pub output_spans: Option<Vec<CodeModeOutputSpan>>,
73}
74
75fn spans_absent(spans: &Option<Vec<CodeModeOutputSpan>>) -> bool {
76    spans.as_ref().is_none_or(Vec::is_empty)
77}
78
79impl CodeModeReceipt {
80    /// True when no member would be serialized, so the receipt omits `details`
81    /// entirely.
82    #[must_use]
83    pub fn serializes_empty(&self) -> bool {
84        self.language.is_none()
85            && self.source_arg.is_none()
86            && self.cell_item_id.is_none()
87            && spans_absent(&self.output_spans)
88    }
89}
90
91impl CodeModeLanguage {
92    /// The word table `Deserialize` decodes through.
93    fn decode_word(word: &str) -> Self {
94        match word {
95            "javascript" => Self::Javascript,
96            _ => Self::Unknown(word.to_owned()),
97        }
98    }
99
100    /// The exact wire spelling, including a value this build does not know.
101    #[must_use]
102    pub fn as_str(&self) -> &str {
103        match self {
104            Self::Javascript => "javascript",
105            Self::Unknown(value) => value,
106        }
107    }
108}
109
110impl Serialize for CodeModeLanguage {
111    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
112        serializer.serialize_str(self.as_str())
113    }
114}
115
116impl<'de> Deserialize<'de> for CodeModeLanguage {
117    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
118        let value = String::deserialize(deserializer)?;
119        Ok(Self::decode_word(&value))
120    }
121}
122
123impl CodeModeOutputStream {
124    /// The word table `Deserialize` decodes through.
125    fn decode_word(word: &str) -> Self {
126        match word {
127            "transcript" => Self::Transcript,
128            _ => Self::Unknown(word.to_owned()),
129        }
130    }
131
132    /// The exact wire spelling, including a value this build does not know.
133    #[must_use]
134    pub fn as_str(&self) -> &str {
135        match self {
136            Self::Transcript => "transcript",
137            Self::Unknown(value) => value,
138        }
139    }
140}
141
142impl Serialize for CodeModeOutputStream {
143    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
144        serializer.serialize_str(self.as_str())
145    }
146}
147
148impl<'de> Deserialize<'de> for CodeModeOutputStream {
149    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
150        let value = String::deserialize(deserializer)?;
151        Ok(Self::decode_word(&value))
152    }
153}