From a4a4b19f2129b48970b2b61a0aa85d529d39f408 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Tue, 24 Jan 2023 13:48:39 +0000 Subject: [PATCH] tor-bytes: Implement conversion from EncodeError to Bug --- crates/tor-bytes/semver.md | 1 + crates/tor-bytes/src/err.rs | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 crates/tor-bytes/semver.md diff --git a/crates/tor-bytes/semver.md b/crates/tor-bytes/semver.md new file mode 100644 index 000000000..a9f4f688d --- /dev/null +++ b/crates/tor-bytes/semver.md @@ -0,0 +1 @@ +ADDED: EncodeError::always_bug method to turn an EcodeError into a Bug diff --git a/crates/tor-bytes/src/err.rs b/crates/tor-bytes/src/err.rs index 899b0fab6..e35097a48 100644 --- a/crates/tor-bytes/src/err.rs +++ b/crates/tor-bytes/src/err.rs @@ -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), + } + } }