Skip to main content

xdr_common/
error.rs

1#![allow(unused_imports)]
2#![allow(dead_code)]
3
4pub use crate::cfg::*;
5
6use core::error;
7
8/// Errors that can occur when working with XDR types.
9///
10/// This error type can be compared via `PartialEq`.
11#[derive(Debug)]
12pub enum Error {
13    /// The XDR value is invalid or malformed.
14    Invalid,
15    /// The XDR value or operation is not supported.
16    Unsupported,
17    /// The length of the value exceeds the maximum allowed length.
18    ///
19    /// This occurs when trying to create a constrained type (e.g., `VecM<T, MAX>`)
20    /// from data that exceeds the `MAX` length parameter.
21    LengthExceedsMax,
22    /// The length of the value does not match the expected length.
23    LengthMismatch,
24    /// XDR padding contains non-zero bytes (XDR requires zero padding).
25    NonZeroPadding,
26    /// The hex string is invalid.
27    ///
28    /// This error occurs when parsing hex-encoded byte strings.
29    #[cfg(feature = "alloc")]
30    InvalidHex,
31    /// The byte sequence is not valid UTF-8.
32    ///
33    /// This occurs when attempting to convert a `StringM` to a UTF-8 `String`.
34    InvalidUTF8,
35    /// The depth limit was exceeded during processing.
36    DepthLimitExceeded,
37    /// The length limit was exceeded during processing.
38    LengthLimitExceeded,
39}
40
41impl PartialEq for Error {
42    fn eq(&self, other: &Self) -> bool {
43        core::mem::discriminant(self) == core::mem::discriminant(other)
44    }
45}
46
47impl error::Error for Error {}
48
49use core::array::TryFromSliceError;
50use core::fmt;
51
52impl fmt::Display for Error {
53    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
54        match self {
55            Error::Invalid => write!(f, "xdr value invalid"),
56            Error::Unsupported => write!(f, "xdr value unsupported"),
57            Error::LengthExceedsMax => {
58                write!(f, "xdr value max length exceeded")
59            }
60            Error::LengthMismatch => {
61                write!(f, "xdr value length does not match")
62            }
63            Error::NonZeroPadding => {
64                write!(f, "xdr padding contains non-zero bytes")
65            }
66            #[cfg(feature = "alloc")]
67            Error::InvalidHex => write!(f, "hex invalid"),
68            Error::InvalidUTF8 => write!(f, "utf8 invalid"),
69            Error::DepthLimitExceeded => write!(f, "depth limit exceeded"),
70            Error::LengthLimitExceeded => write!(f, "length limit exceeded"),
71        }
72    }
73}
74
75impl From<TryFromSliceError> for Error {
76    fn from(_: TryFromSliceError) -> Error {
77        Error::LengthMismatch
78    }
79}
80
81impl From<core::str::Utf8Error> for Error {
82    fn from(_: core::str::Utf8Error) -> Self {
83        Error::InvalidUTF8
84    }
85}
86
87#[cfg(feature = "alloc")]
88impl From<FromUtf8Error> for Error {
89    fn from(_: FromUtf8Error) -> Self {
90        Error::InvalidUTF8
91    }
92}