use std::borrow::Cow;
use std::num::NonZeroUsize;
use derive_deftly::{Deftly, define_derive_deftly};
use safelog::Sensitive;
use thiserror::Error;
use tor_error::{Bug, into_internal};
define_derive_deftly! {
PartialEqForError expect items:
impl PartialEq for $ttype {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
$(
${when not(vmeta(never_eq))}
#[allow(deprecated)]
(${vpat fprefix=a_}, ${vpat fprefix=b_}) => {
$(
if $<a_ $fname> != $<b_ $fname> { return false; }
)
return true;
},
)
(_, _) => false,
}
}
}
}
#[derive(Error, Debug, Clone, Deftly)]
#[derive_deftly(PartialEqForError)]
#[non_exhaustive]
pub enum Error {
#[deprecated(since = "0.22.0", note = "Use Reader::incomplete_error instead.")]
#[error("something was truncated (maybe inner structure, maybe outer message)")]
Truncated,
#[error("Object truncated (or not fully present), at least {deficit} more bytes needed")]
Incomplete {
deficit: Sensitive<NonZeroUsize>,
},
#[error("Extra bytes at end of object")]
ExtraneousBytes,
#[error("Object length too large to represent as usize")]
BadLengthValue,
#[deprecated(since = "0.6.2", note = "Use InvalidMessage instead.")]
#[error("Bad object: {0}")]
BadMessage(&'static str),
#[error("Bad object: {0}")]
InvalidMessage(Cow<'static, str>),
#[error("message (or inner portion) too short")]
MissingData,
#[error("Internal error")]
#[deftly(never_eq)] Bug(#[from] tor_error::Bug),
}
impl Error {
pub fn new_incomplete_for_test(deficit: usize) -> Self {
let deficit = NonZeroUsize::new(deficit)
.expect("zero deficit in assert!")
.into();
Error::Incomplete { deficit }
}
}
#[derive(Error, Debug, Clone)]
#[non_exhaustive]
pub enum EncodeError {
#[error("Object length too large to encode")]
BadLengthValue,
#[error("Internal error")]
Bug(#[from] Bug),
}
impl EncodeError {
#[deprecated(note = "please use the `From<EncodeError>` trait for `Bug` instead")]
pub fn always_bug(self) -> Bug {
match self {
EncodeError::Bug(bug) => bug,
EncodeError::BadLengthValue => into_internal!("EncodingError")(self),
}
}
}
impl From<EncodeError> for Bug {
fn from(error: EncodeError) -> Bug {
match error {
EncodeError::Bug(bug) => bug,
EncodeError::BadLengthValue => into_internal!("EncodingError")(error),
}
}
}