tor-bytes: Implement conversion from EncodeError to Bug

This commit is contained in:
Ian Jackson 2023-01-24 13:48:39 +00:00
parent fd8915155e
commit a4a4b19f21
2 changed files with 17 additions and 1 deletions

View File

@ -0,0 +1 @@
ADDED: EncodeError::always_bug method to turn an EcodeError into a Bug

View File

@ -1,6 +1,7 @@
//! Internal: Declare an Error type for tor-bytes
use thiserror::Error;
use tor_error::{into_internal, Bug};
/// Error type for decoding Tor objects from bytes.
//
@ -62,5 +63,19 @@ pub enum EncodeError {
/// We use this variant instead of calling assert() and expect() and
/// unwrap() from within encoding implementations.
#[error("Internal error")]
Bug(#[from] tor_error::Bug),
Bug(#[from] Bug),
}
impl EncodeError {
/// Converts this error into a [`Bug`]
///
/// Use when any encoding error is a bug.
//
// TODO: should this be a `From` impl or would that be too error-prone?
pub fn always_bug(self) -> Bug {
match self {
EncodeError::Bug(bug) => bug,
EncodeError::BadLengthValue => into_internal!("EncodingError")(self),
}
}
}