Skip to main content

ledvar_core/
error.rs

1//! Error type for the core. Hand-rolled (no `thiserror`) to keep the dependency
2//! surface minimal — this crate is embedded by every higher layer.
3
4use std::fmt;
5
6/// Why a snapshot is not well-formed, or a version cannot be parsed.
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub enum Error {
9    /// `protocol_version` is not a parseable `MAJOR.MINOR.PATCH`.
10    BadVersion(String),
11    /// The snapshot's MAJOR differs from [`crate::SUPPORTED_PROTOCOL_MAJOR`].
12    UnsupportedMajor(u64),
13    /// The snapshot's MINOR differs from [`crate::SUPPORTED_PROTOCOL_MINOR`]. Only
14    /// enforced while MAJOR is 0, where a MINOR bump may move the canonical form (SPEC §10).
15    UnsupportedMinor(u64),
16    /// A node at the given index has an empty `path`.
17    EmptyPath(usize),
18    /// A node at the given index has an empty segment in its `path` (`[""]`).
19    EmptyPathSegment(usize),
20    /// A node at the given index has an empty attribute name (`{"":[…]}`).
21    EmptyAttrName(usize),
22    /// A node (index) has an attribute (name) that maps to an empty value set (`{"a":[]}`).
23    EmptyValueSet(usize, String),
24    /// Two nodes share the same identity (path) within one snapshot.
25    DuplicatePath(Vec<String>),
26}
27
28impl fmt::Display for Error {
29    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30        match self {
31            Error::BadVersion(v) => write!(f, "protocol_version is not MAJOR.MINOR.PATCH: {v:?}"),
32            Error::UnsupportedMajor(m) => write!(
33                f,
34                "unsupported protocol MAJOR {m} (this implementation supports {})",
35                crate::SUPPORTED_PROTOCOL_MAJOR
36            ),
37            Error::UnsupportedMinor(m) => write!(
38                f,
39                "unsupported protocol MINOR {m} while MAJOR is 0 (this implementation supports 0.{})",
40                crate::SUPPORTED_PROTOCOL_MINOR
41            ),
42            Error::EmptyPath(i) => write!(f, "node at index {i} has an empty path"),
43            Error::EmptyPathSegment(i) => write!(f, "node at index {i} has an empty path segment"),
44            Error::EmptyAttrName(i) => write!(f, "node at index {i} has an empty attribute name"),
45            Error::EmptyValueSet(i, name) => {
46                write!(f, "node at index {i}: attribute {name:?} has an empty value set")
47            }
48            Error::DuplicatePath(p) => write!(f, "duplicate node path within snapshot: {p:?}"),
49        }
50    }
51}
52
53impl std::error::Error for Error {}