Skip to main content

ostrya_gvariant/
error.rs

1use std::fmt;
2
3/// An error of the GVariant codec.
4///
5/// The functions that encode and decode GVariant bytes return it.
6/// [`Type::parse`], [`to_text`], and [`to_text_unannotated`] also return it.
7/// [`from_text`] returns a [`TextError`].
8///
9/// [`Type::parse`]: crate::Type::parse
10/// [`to_text`]: crate::to_text
11/// [`to_text_unannotated`]: crate::to_text_unannotated
12/// [`from_text`]: crate::from_text
13/// [`TextError`]: crate::TextError
14#[derive(Debug, Clone, PartialEq, Eq)]
15pub enum Error {
16    /// A type signature string that [`Type::parse`] refuses.
17    ///
18    /// [`Type::parse`]: crate::Type::parse
19    InvalidTypeString {
20        /// The signature string that the caller gave.
21        signature: String,
22        /// The byte offset in `signature` at which the parse fails.
23        offset: usize,
24        /// The reason for the failure.
25        ///
26        /// [`Type::parse`] lists each reason.
27        ///
28        /// [`Type::parse`]: crate::Type::parse
29        reason: &'static str,
30    },
31    /// The value does not match its type.
32    ///
33    /// [`to_bytes`], [`to_text`], and [`to_text_unannotated`] return it.
34    ///
35    /// [`to_bytes`]: crate::to_bytes
36    /// [`to_text`]: crate::to_text
37    /// [`to_text_unannotated`]: crate::to_text_unannotated
38    TypeMismatch {
39        /// The signature of the type that the caller paired with the value.
40        expected: String,
41        /// The kind of the value, for example `array` or `tuple of a different
42        /// arity`.
43        found: &'static str,
44    },
45    /// GVariant cannot represent the value.
46    ///
47    /// This crate returns it for a string that holds an interior NUL byte.
48    /// The field holds the reason.
49    InvalidValue(&'static str),
50    /// The serialized bytes are not normal-form GVariant of the expected type.
51    ///
52    /// The field names the deviation.
53    NotNormal(&'static str),
54    /// The container nesting exceeds the depth limit of 128 levels.
55    ///
56    /// Each variant, array, maybe, tuple, and dict entry adds one level.
57    /// [`from_bytes`], [`validate`], [`tuple_field_from_bytes`], and
58    /// [`to_bytes`] return it. The decode of a [`Variant`] returns it through
59    /// [`from_bytes`]. [`Type::parse`] applies the same limit to a signature
60    /// and returns [`Error::InvalidTypeString`].
61    ///
62    /// [`Variant`]: crate::Variant
63    /// [`from_bytes`]: crate::from_bytes
64    /// [`validate`]: crate::validate
65    /// [`tuple_field_from_bytes`]: crate::tuple_field_from_bytes
66    /// [`to_bytes`]: crate::to_bytes
67    /// [`Type::parse`]: crate::Type::parse
68    DepthExceeded,
69}
70
71impl fmt::Display for Error {
72    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73        match self {
74            Error::InvalidTypeString {
75                signature,
76                offset,
77                reason,
78            } => write!(
79                f,
80                "invalid type signature {signature:?} at offset {offset}: {reason}"
81            ),
82            Error::TypeMismatch { expected, found } => {
83                write!(f, "value of kind {found} does not match type {expected:?}")
84            }
85            Error::InvalidValue(reason) => write!(f, "unrepresentable value: {reason}"),
86            Error::NotNormal(reason) => write!(f, "data is not normal-form GVariant: {reason}"),
87            Error::DepthExceeded => write!(f, "container nesting exceeds the supported depth"),
88        }
89    }
90}
91
92impl std::error::Error for Error {}
93
94/// The result type of the codec.
95pub type Result<T> = std::result::Result<T, Error>;