Skip to main content

cp_ast_json/
error.rs

1use std::fmt;
2
3/// Errors during AST ↔ JSON conversion.
4#[derive(Debug, Clone)]
5pub enum ConversionError {
6    /// Invalid decimal ID string.
7    InvalidId(String),
8    /// Unknown enum variant tag.
9    UnknownVariant {
10        type_name: &'static str,
11        value: String,
12    },
13    /// JSON parse/stringify error.
14    Json(String),
15    /// Share state base64 decoding error.
16    Base64(String),
17    /// Share state gzip decoding error.
18    Gzip(String),
19    /// Schema version mismatch.
20    UnsupportedVersion(u32),
21    /// Arena slot ID doesn't match its index.
22    IdIndexMismatch { expected: u64, actual: u64 },
23}
24
25impl fmt::Display for ConversionError {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        match self {
28            Self::InvalidId(s) => write!(f, "invalid ID string: {s:?}"),
29            Self::UnknownVariant { type_name, value } => {
30                write!(f, "unknown {type_name} variant: {value:?}")
31            }
32            Self::Json(msg) => write!(f, "JSON error: {msg}"),
33            Self::Base64(msg) => write!(f, "base64 decode error: {msg}"),
34            Self::Gzip(msg) => write!(f, "gzip decode error: {msg}"),
35            Self::UnsupportedVersion(v) => write!(f, "unsupported schema version: {v}"),
36            Self::IdIndexMismatch { expected, actual } => {
37                write!(
38                    f,
39                    "arena slot ID mismatch: expected {expected}, got {actual}"
40                )
41            }
42        }
43    }
44}
45
46impl std::error::Error for ConversionError {}
47
48impl From<serde_json::Error> for ConversionError {
49    fn from(err: serde_json::Error) -> Self {
50        Self::Json(err.to_string())
51    }
52}