Skip to content

Commit

Permalink
refactor(proto)!: replace flex_error with thiserror
Browse files Browse the repository at this point in the history
  • Loading branch information
lklimek committed Apr 12, 2023
1 parent d61717a commit 45014e9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 30 deletions.
2 changes: 1 addition & 1 deletion proto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ time = { version = "0.3", default-features = false, features = [
"macros",
"parsing",
] }
flex-error = { version = "0.4.4", default-features = false }
thiserror = { version = "1.0.40" }
chrono = { version = "0.4.24", default-features = false }
derive_more = { version = "0.99.17" }

Expand Down
44 changes: 20 additions & 24 deletions proto/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
//! This module defines the various errors that be raised during Protobuf
//! conversions.
extern crate std;
use core::{convert::TryFrom, fmt::Display, num::TryFromIntError};

use flex_error::{define_error, DisplayOnly};
use prost::{DecodeError, EncodeError};

use crate::prelude::*;

define_error! {
Error {
TryFromProtobuf
{ reason: String }
| e | {
format!("error converting message type into domain type: {}",
e.reason)
},

EncodeMessage
[ DisplayOnly<EncodeError> ]
| _ | { "error encoding message into buffer" },

DecodeMessage
[ DisplayOnly<DecodeError> ]
| _ | { "error decoding buffer into message" },

ParseLength
[ DisplayOnly<TryFromIntError> ]
| _ | { "error parsing encoded length" },
}
#[derive(thiserror::Error, Debug)]
pub enum Error {
/// error converting message type into domain type
#[error("error converting message type into domain type: {0}")]
TryFromProtobuf(String),

/// error encoding message into buffer
#[error("error encoding message into buffer: {0:?}")]
EncodeMessage(EncodeError),
/// error decoding buffer into message
#[error("error decoding buffer into message: {0:?}")]
DecodeMessage(DecodeError),
/// error decoding buffer into message
#[error("error building canonical message: {0}")]
CreateCanonical(String),
/// error parsing encoded length
#[error("error parsing encoded length: {0:?}")]
ParseLength(TryFromIntError),
}

impl Error {
Expand All @@ -37,6 +33,6 @@ impl Error {
E: Display,
T: TryFrom<Raw, Error = E>,
{
Error::try_from_protobuf(format!("{e}"))
Error::TryFromProtobuf(format!("{e}"))
}
}
10 changes: 5 additions & 5 deletions proto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ where
fn encode<B: BufMut>(&self, buf: &mut B) -> Result<(), Error> {
T::from(self.clone())
.encode(buf)
.map_err(Error::encode_message)
.map_err(Error::EncodeMessage)
}

/// Encode with a length-delimiter to a buffer in Protobuf format.
Expand All @@ -145,7 +145,7 @@ where
fn encode_length_delimited<B: BufMut>(&self, buf: &mut B) -> Result<(), Error> {
T::from(self.clone())
.encode_length_delimited(buf)
.map_err(Error::encode_message)
.map_err(Error::EncodeMessage)
}

/// Constructor that attempts to decode an instance from a buffer.
Expand All @@ -157,7 +157,7 @@ where
///
/// [`prost::Message::decode`]: https://docs.rs/prost/*/prost/trait.Message.html#method.decode
fn decode<B: Buf>(buf: B) -> Result<Self, Error> {
let raw = T::decode(buf).map_err(Error::decode_message)?;
let raw = T::decode(buf).map_err(Error::DecodeMessage)?;

Self::try_from(raw).map_err(Error::try_from::<T, Self, _>)
}
Expand All @@ -172,7 +172,7 @@ where
///
/// [`prost::Message::decode_length_delimited`]: https://docs.rs/prost/*/prost/trait.Message.html#method.decode_length_delimited
fn decode_length_delimited<B: Buf>(buf: B) -> Result<Self, Error> {
let raw = T::decode_length_delimited(buf).map_err(Error::decode_message)?;
let raw = T::decode_length_delimited(buf).map_err(Error::DecodeMessage)?;

Self::try_from(raw).map_err(Error::try_from::<T, Self, _>)
}
Expand Down Expand Up @@ -202,7 +202,7 @@ where
/// Encode with a length-delimiter to a `Vec<u8>` Protobuf-encoded message.
fn encode_length_delimited_vec(&self) -> Result<Vec<u8>, Error> {
let len = self.encoded_len();
let lenu64 = len.try_into().map_err(Error::parse_length)?;
let lenu64 = len.try_into().map_err(Error::ParseLength)?;
let mut wire = Vec::with_capacity(len + encoded_len_varint(lenu64));
self.encode_length_delimited(&mut wire).map(|_| wire)
}
Expand Down

0 comments on commit 45014e9

Please sign in to comment.