Skip to main content

cbor_ld/
error.rs

1use std::fmt;
2
3/// A result type returned by this crate.
4pub type Result<T> = std::result::Result<T, Error>;
5
6/// Errors returned by the CBOR-LD processor.
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub enum Error {
9    /// cbor2 failed to encode, decode, or validate data.
10    Cbor(String),
11    /// The input is not a CBOR-LD 1.0 tagged value.
12    NotCborld(String),
13    /// A required registry type table was not provided.
14    NoTypeTable(u64),
15    /// The type table contains an unsupported literal type.
16    UnsupportedLiteralType(String),
17    /// A JSON-LD term definition is not valid for CBOR-LD processing.
18    InvalidTermDefinition(String),
19    /// A protected term was redefined.
20    ProtectedTermRedefinition(String),
21    /// A remote context was needed but could not be loaded.
22    DocumentLoader(String),
23    /// A compressed context could not be resolved.
24    UndefinedCompressedContext(u64),
25    /// A compressed term ID could not be resolved.
26    UnknownTermId(u64),
27    /// A compressed table value could not be resolved.
28    UnknownCompressedValue(u64),
29    /// A compressed value uses invalid bytes.
30    UnrecognizedBytes(String),
31    /// The input shape is invalid for the active CBOR-LD algorithm.
32    InvalidInput(String),
33    /// A value cannot be encoded by a required codec.
34    UnsupportedValue(String),
35}
36
37impl fmt::Display for Error {
38    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39        match self {
40            Self::Cbor(msg) => write!(f, "CBOR error: {msg}"),
41            Self::NotCborld(msg) => write!(f, "not CBOR-LD: {msg}"),
42            Self::NoTypeTable(id) => write!(f, "type table not found for registry entry {id}"),
43            Self::UnsupportedLiteralType(t) => {
44                write!(f, "unsupported literal type in type table: {t}")
45            }
46            Self::InvalidTermDefinition(msg) => write!(f, "invalid term definition: {msg}"),
47            Self::ProtectedTermRedefinition(term) => {
48                write!(f, "unexpected redefinition of protected term {term:?}")
49            }
50            Self::DocumentLoader(msg) => write!(f, "document loader error: {msg}"),
51            Self::UndefinedCompressedContext(id) => {
52                write!(f, "undefined compressed context {id}")
53            }
54            Self::UnknownTermId(id) => write!(f, "unknown CBOR-LD term ID {id}"),
55            Self::UnknownCompressedValue(id) => write!(f, "unknown compressed value {id}"),
56            Self::UnrecognizedBytes(msg) => write!(f, "unrecognized bytes: {msg}"),
57            Self::InvalidInput(msg) => write!(f, "invalid input: {msg}"),
58            Self::UnsupportedValue(msg) => write!(f, "unsupported value: {msg}"),
59        }
60    }
61}
62
63impl std::error::Error for Error {}