1use serde::{
4 de::{self, Expected, Unexpected},
5 ser,
6};
7use std::{char::CharTryFromError, fmt::Display, io, num::TryFromIntError};
8use thiserror::Error;
9
10#[derive(Debug, Error)]
12pub enum Error {
13 #[error(transparent)]
15 Io(#[from] io::Error),
16
17 #[error("invalid MAC")]
19 InvalidMac,
20 #[error("key exchange failure")]
22 FailedKeyExchange,
23 #[error("RNG failure")]
25 FailedRng,
26 #[error("unexpected trailing bytes")]
28 TrailingBytes,
29 #[error("encountered wrong data type")]
31 WrongType, #[error("invalid data type")]
34 InvalidType,
35 #[error("invalid value")]
37 InvalidValue,
38 #[error("invalid length")]
40 InvalidLength,
41 #[error("invalid length of length")]
43 InvalidLengthOfLength,
44 #[error("older version found")]
46 OlderVersion,
47 #[error("beta version found")]
49 BetaVersion,
50 #[error("newer version found")]
52 NewerVersion,
53 #[error("invalid record")]
55 InvalidRecord,
56 #[error("invalid key found")]
58 InvalidKey,
59 #[error("invalid proof of ownership found")]
61 InvalidProof,
62 #[error("empty payload")]
64 EmptyPayload,
65 #[error("invalid commitment")]
67 InvalidCommitment,
68 #[error("invalid transport")]
70 InvalidTransport,
71 #[error("invalid confirmation")]
73 InvalidConfirmation,
74 #[error("past time period requested")]
76 TimePeriodIsPast,
77 #[error("time has ran out")]
79 Timeout,
80
81 #[doc(hidden)]
82 #[error("{0:}")]
83 Other(String),
84}
85
86impl Error {
87 pub fn eof() -> Error {
89 Self::from(io::Error::new(
90 io::ErrorKind::UnexpectedEof,
91 "unexpected end of input",
92 ))
93 }
94
95 pub fn is_eof(&self) -> bool {
97 matches!(self, Error::Io(e) if e.kind() == io::ErrorKind::UnexpectedEof)
98 }
99}
100
101impl From<TryFromIntError> for Error {
102 fn from(_: TryFromIntError) -> Self {
103 Self::InvalidValue
104 }
105}
106
107impl From<CharTryFromError> for Error {
108 fn from(_: CharTryFromError) -> Self {
109 Self::InvalidValue
110 }
111}
112
113impl From<rand::Error> for Error {
114 fn from(_: rand::Error) -> Self {
115 Self::FailedRng
116 }
117}
118
119impl From<xsalsa20poly1305::aead::Error> for Error {
120 fn from(_: xsalsa20poly1305::aead::Error) -> Self {
121 Self::InvalidMac
122 }
123}
124
125impl ser::Error for Error {
126 fn custom<T: Display>(msg: T) -> Self {
127 Self::Other(msg.to_string())
128 }
129}
130
131impl de::Error for Error {
132 fn custom<T: Display>(msg: T) -> Self {
133 Self::Other(msg.to_string())
134 }
135
136 fn invalid_type(_unexp: Unexpected, _exp: &dyn Expected) -> Self {
137 Self::WrongType
138 }
139
140 fn invalid_value(_unexp: Unexpected, _exp: &dyn Expected) -> Self {
141 Self::InvalidValue
142 }
143
144 fn invalid_length(_len: usize, _exp: &dyn Expected) -> Self {
145 Self::InvalidLength
146 }
147}
148
149pub type Result<T> = std::result::Result<T, Error>;