Skip to main content

lex_core/lex/wire/
error.rs

1//! Error types for the wire codec's reverse direction.
2
3/// Errors the reverse codec can surface when converting a `WireNode` back
4/// to a lex-core [`crate::lex::ast::ContentItem`].
5///
6/// Forward conversion (`to_wire_*`) is total — every lex-core AST shape
7/// has a defined mapping to a wire form. Reverse conversion is fallible
8/// because the wire input may be malformed or carry an unknown variant.
9#[derive(Debug, Clone, PartialEq)]
10pub enum FromWireError {
11    /// The wire node carried an unknown structural placeholder (an
12    /// `Unknown` variant added in a future wire version, or a kind the
13    /// host's `WIRE_VERSION` does not recognise).
14    UnsupportedKind { kind: String },
15    /// A required field was missing or had the wrong shape — for
16    /// example, a `WireNode::Annotation` whose `params` was not a
17    /// JSON object.
18    MalformedField { field: &'static str, detail: String },
19    /// A nested `serde_json::from_value` conversion failed when
20    /// destructuring an opaque field (e.g., `params` blob).
21    DeserialisationFailed(String),
22}
23
24impl std::fmt::Display for FromWireError {
25    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26        match self {
27            FromWireError::UnsupportedKind { kind } => {
28                write!(f, "wire AST: unsupported kind `{kind}`")
29            }
30            FromWireError::MalformedField { field, detail } => {
31                write!(f, "wire AST: malformed field `{field}`: {detail}")
32            }
33            FromWireError::DeserialisationFailed(msg) => {
34                write!(f, "wire AST: deserialisation failed: {msg}")
35            }
36        }
37    }
38}
39
40impl std::error::Error for FromWireError {}