ssh_encoding/
error.rs

1//! Error types.
2
3use crate::LabelError;
4use core::fmt;
5
6/// Result type with `ssh-encoding` crate's [`Error`] as the error type.
7pub type Result<T> = core::result::Result<T, Error>;
8
9/// Error type.
10#[derive(Clone, Debug, Eq, PartialEq)]
11#[non_exhaustive]
12pub enum Error {
13    /// Base64-related errors.
14    #[cfg(feature = "base64")]
15    Base64(base64ct::Error),
16
17    /// Character encoding-related errors.
18    CharacterEncoding,
19
20    /// Invalid label.
21    Label(LabelError),
22
23    /// Invalid length.
24    Length,
25
26    /// `mpint` encoding errors.
27    #[cfg(feature = "alloc")]
28    MpintEncoding,
29
30    /// Overflow errors.
31    Overflow,
32
33    /// PEM encoding errors.
34    #[cfg(feature = "pem")]
35    Pem(pem_rfc7468::Error),
36
37    /// Unexpected trailing data at end of message.
38    TrailingData {
39        /// Number of bytes of remaining data at end of message.
40        remaining: usize,
41    },
42
43    /// Invalid discriminant value in message.
44    InvalidDiscriminant(u128),
45}
46
47impl core::error::Error for Error {
48    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
49        match self {
50            #[cfg(feature = "base64")]
51            Self::Base64(err) => Some(err),
52            #[cfg(feature = "pem")]
53            Self::Pem(err) => Some(err),
54            _ => None,
55        }
56    }
57}
58
59impl fmt::Display for Error {
60    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61        match self {
62            #[cfg(feature = "base64")]
63            Error::Base64(err) => write!(f, "Base64 encoding error: {err}"),
64            Error::CharacterEncoding => write!(f, "character encoding invalid"),
65            Error::Label(err) => write!(f, "{}", err),
66            Error::Length => write!(f, "length invalid"),
67            #[cfg(feature = "alloc")]
68            Error::MpintEncoding => write!(f, "`mpint` encoding invalid"),
69            Error::Overflow => write!(f, "internal overflow error"),
70            #[cfg(feature = "pem")]
71            Error::Pem(err) => write!(f, "{err}"),
72            Error::TrailingData { remaining } => write!(
73                f,
74                "unexpected trailing data at end of message ({remaining} bytes)",
75            ),
76            Error::InvalidDiscriminant(discriminant) => {
77                write!(f, "invalid discriminant value: {discriminant}")
78            }
79        }
80    }
81}
82
83impl From<LabelError> for Error {
84    fn from(err: LabelError) -> Error {
85        Error::Label(err)
86    }
87}
88
89impl From<core::num::TryFromIntError> for Error {
90    fn from(_: core::num::TryFromIntError) -> Error {
91        Error::Overflow
92    }
93}
94
95impl From<core::str::Utf8Error> for Error {
96    fn from(_: core::str::Utf8Error) -> Error {
97        Error::CharacterEncoding
98    }
99}
100
101#[cfg(feature = "alloc")]
102impl From<alloc::string::FromUtf8Error> for Error {
103    fn from(_: alloc::string::FromUtf8Error) -> Error {
104        Error::CharacterEncoding
105    }
106}
107
108#[cfg(feature = "base64")]
109impl From<base64ct::Error> for Error {
110    fn from(err: base64ct::Error) -> Error {
111        Error::Base64(err)
112    }
113}
114
115#[cfg(feature = "base64")]
116impl From<base64ct::InvalidLengthError> for Error {
117    fn from(_: base64ct::InvalidLengthError) -> Error {
118        Error::Length
119    }
120}
121
122#[cfg(feature = "bigint")]
123impl From<bigint::DecodeError> for Error {
124    fn from(_: bigint::DecodeError) -> Error {
125        Error::MpintEncoding
126    }
127}
128
129#[cfg(feature = "pem")]
130impl From<pem_rfc7468::Error> for Error {
131    fn from(err: pem_rfc7468::Error) -> Error {
132        Error::Pem(err)
133    }
134}