Skip to main content

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    /// Memory allocation or capacity reservation failed.
14    #[cfg(feature = "alloc")]
15    Alloc(alloc::collections::TryReserveError),
16
17    /// Base64-related errors.
18    #[cfg(feature = "base64")]
19    Base64(base64ct::Error),
20
21    /// Character encoding-related errors.
22    CharacterEncoding,
23
24    /// A previous read or nested decoding operation failed.
25    Failed,
26
27    /// Invalid label.
28    Label(LabelError),
29
30    /// Invalid length.
31    Length,
32
33    /// `mpint` encoding errors.
34    #[cfg(feature = "alloc")]
35    MpintEncoding,
36
37    /// Maximum decoding nesting depth exceeded.
38    NestingDepth,
39
40    /// Overflow errors.
41    Overflow,
42
43    /// PEM encoding errors.
44    #[cfg(feature = "pem")]
45    Pem(pem_rfc7468::Error),
46
47    /// Unexpected trailing data at end of message.
48    TrailingData {
49        /// Number of bytes of remaining data at end of message.
50        remaining: usize,
51    },
52
53    /// Invalid discriminant value in message.
54    InvalidDiscriminant(u128),
55}
56
57impl core::error::Error for Error {
58    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
59        match self {
60            #[cfg(feature = "alloc")]
61            Self::Alloc(err) => Some(err),
62            #[cfg(feature = "base64")]
63            Self::Base64(err) => Some(err),
64            #[cfg(feature = "pem")]
65            Self::Pem(err) => Some(err),
66            _ => None,
67        }
68    }
69}
70
71impl fmt::Display for Error {
72    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73        match self {
74            #[cfg(feature = "alloc")]
75            Error::Alloc(err) => write!(f, "allocation failed: {err}"),
76            #[cfg(feature = "base64")]
77            Error::Base64(err) => write!(f, "Base64 encoding error: {err}"),
78            Error::CharacterEncoding => write!(f, "character encoding invalid"),
79            Error::Failed => write!(f, "decoder previously failed"),
80            Error::Label(err) => write!(f, "{}", err),
81            Error::Length => write!(f, "length invalid"),
82            #[cfg(feature = "alloc")]
83            Error::MpintEncoding => write!(f, "`mpint` encoding invalid"),
84            Error::NestingDepth => write!(f, "maximum nesting depth exceeded"),
85            Error::Overflow => write!(f, "internal overflow error"),
86            #[cfg(feature = "pem")]
87            Error::Pem(err) => write!(f, "{err}"),
88            Error::TrailingData { remaining } => write!(
89                f,
90                "unexpected trailing data at end of message ({remaining} bytes)",
91            ),
92            Error::InvalidDiscriminant(discriminant) => {
93                write!(f, "invalid discriminant value: {discriminant}")
94            }
95        }
96    }
97}
98
99impl From<LabelError> for Error {
100    fn from(err: LabelError) -> Error {
101        Error::Label(err)
102    }
103}
104
105impl From<core::num::TryFromIntError> for Error {
106    fn from(_: core::num::TryFromIntError) -> Error {
107        Error::Overflow
108    }
109}
110
111impl From<core::str::Utf8Error> for Error {
112    fn from(_: core::str::Utf8Error) -> Error {
113        Error::CharacterEncoding
114    }
115}
116
117#[cfg(feature = "alloc")]
118impl From<alloc::collections::TryReserveError> for Error {
119    fn from(err: alloc::collections::TryReserveError) -> Error {
120        Error::Alloc(err)
121    }
122}
123
124#[cfg(feature = "alloc")]
125impl From<alloc::string::FromUtf8Error> for Error {
126    fn from(_: alloc::string::FromUtf8Error) -> Error {
127        Error::CharacterEncoding
128    }
129}
130
131#[cfg(feature = "base64")]
132impl From<base64ct::Error> for Error {
133    fn from(err: base64ct::Error) -> Error {
134        Error::Base64(err)
135    }
136}
137
138#[cfg(feature = "base64")]
139impl From<base64ct::InvalidLengthError> for Error {
140    fn from(_: base64ct::InvalidLengthError) -> Error {
141        Error::Length
142    }
143}
144
145#[cfg(feature = "bigint")]
146impl From<bigint::DecodeError> for Error {
147    fn from(_: bigint::DecodeError) -> Error {
148        Error::MpintEncoding
149    }
150}
151
152#[cfg(feature = "pem")]
153impl From<pem_rfc7468::Error> for Error {
154    fn from(err: pem_rfc7468::Error) -> Error {
155        Error::Pem(err)
156    }
157}