Skip to content

Commit

Permalink
Implement Error trait for ZBytes error types (#1192)
Browse files Browse the repository at this point in the history
* Implement Error trait for ZBytes error types

* Improve error display message
  • Loading branch information
Mallets authored Jun 26, 2024
1 parent e1beef1 commit fc18f90
Showing 1 changed file with 153 additions and 0 deletions.
153 changes: 153 additions & 0 deletions zenoh/src/api/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,28 @@ where
Deserialize(<T as TryFrom<ZBytes>>::Error),
}

impl<T> std::fmt::Display for ZReadOrDeserializeError<T>
where
T: Debug,
T: TryFrom<ZBytes>,
<T as TryFrom<ZBytes>>::Error: Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ZReadOrDeserializeError::Read(_) => f.write_str("Read error"),
ZReadOrDeserializeError::Deserialize(e) => f.write_fmt(format_args!("{:?}", e)),
}
}
}

impl<T> std::error::Error for ZReadOrDeserializeError<T>
where
T: Debug,
T: TryFrom<ZBytes>,
<T as TryFrom<ZBytes>>::Error: Debug,
{
}

impl ZBytesReader<'_> {
/// Returns the number of bytes that can still be read
pub fn remaining(&self) -> usize {
Expand Down Expand Up @@ -480,6 +502,14 @@ pub struct ZSerde;
#[derive(Debug, Clone, Copy)]
pub struct ZDeserializeError;

impl std::fmt::Display for ZDeserializeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("Deserialize error")
}
}

impl std::error::Error for ZDeserializeError {}

// ZBytes
impl Serialize<ZBytes> for ZSerde {
type Output = ZBytes;
Expand Down Expand Up @@ -2255,6 +2285,38 @@ where
Two(ZReadOrDeserializeError<B>),
}

impl<A, B> std::fmt::Display for ZReadOrDeserializeErrorTuple2<A, B>
where
A: Debug,
A: TryFrom<ZBytes>,
<A as TryFrom<ZBytes>>::Error: Debug,
B: Debug,
B: TryFrom<ZBytes>,
<B as TryFrom<ZBytes>>::Error: Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ZReadOrDeserializeErrorTuple2::One(e) => {
f.write_fmt(format_args!("1st tuple element: {}", e))
}
ZReadOrDeserializeErrorTuple2::Two(e) => {
f.write_fmt(format_args!("2nd tuple element: {}", e))
}
}
}
}

impl<A, B> std::error::Error for ZReadOrDeserializeErrorTuple2<A, B>
where
A: Debug,
A: TryFrom<ZBytes>,
<A as TryFrom<ZBytes>>::Error: Debug,
B: Debug,
B: TryFrom<ZBytes>,
<B as TryFrom<ZBytes>>::Error: Debug,
{
}

impl<A, B> Deserialize<(A, B)> for ZSerde
where
A: TryFrom<ZBytes>,
Expand Down Expand Up @@ -2408,6 +2470,47 @@ where
Three(ZReadOrDeserializeError<C>),
}

impl<A, B, C> std::fmt::Display for ZReadOrDeserializeErrorTuple3<A, B, C>
where
A: Debug,
A: TryFrom<ZBytes>,
<A as TryFrom<ZBytes>>::Error: Debug,
B: Debug,
B: TryFrom<ZBytes>,
<B as TryFrom<ZBytes>>::Error: Debug,
C: Debug,
C: TryFrom<ZBytes>,
<C as TryFrom<ZBytes>>::Error: Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ZReadOrDeserializeErrorTuple3::One(e) => {
f.write_fmt(format_args!("1st tuple element: {}", e))
}
ZReadOrDeserializeErrorTuple3::Two(e) => {
f.write_fmt(format_args!("2nd tuple element: {}", e))
}
ZReadOrDeserializeErrorTuple3::Three(e) => {
f.write_fmt(format_args!("3rd tuple element: {}", e))
}
}
}
}

impl<A, B, C> std::error::Error for ZReadOrDeserializeErrorTuple3<A, B, C>
where
A: Debug,
A: TryFrom<ZBytes>,
<A as TryFrom<ZBytes>>::Error: Debug,
B: Debug,
B: TryFrom<ZBytes>,
<B as TryFrom<ZBytes>>::Error: Debug,
C: Debug,
C: TryFrom<ZBytes>,
<C as TryFrom<ZBytes>>::Error: Debug,
{
}

impl<A, B, C> Deserialize<(A, B, C)> for ZSerde
where
A: TryFrom<ZBytes>,
Expand Down Expand Up @@ -2585,6 +2688,56 @@ where
Four(ZReadOrDeserializeError<D>),
}

impl<A, B, C, D> std::fmt::Display for ZReadOrDeserializeErrorTuple4<A, B, C, D>
where
A: Debug,
A: TryFrom<ZBytes>,
<A as TryFrom<ZBytes>>::Error: Debug,
B: Debug,
B: TryFrom<ZBytes>,
<B as TryFrom<ZBytes>>::Error: Debug,
C: Debug,
C: TryFrom<ZBytes>,
<C as TryFrom<ZBytes>>::Error: Debug,
D: Debug,
D: TryFrom<ZBytes>,
<D as TryFrom<ZBytes>>::Error: Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ZReadOrDeserializeErrorTuple4::One(e) => {
f.write_fmt(format_args!("1st tuple element: {}", e))
}
ZReadOrDeserializeErrorTuple4::Two(e) => {
f.write_fmt(format_args!("2nd tuple element: {}", e))
}
ZReadOrDeserializeErrorTuple4::Three(e) => {
f.write_fmt(format_args!("3rd tuple element: {}", e))
}
ZReadOrDeserializeErrorTuple4::Four(e) => {
f.write_fmt(format_args!("4th tuple element: {}", e))
}
}
}
}

impl<A, B, C, D> std::error::Error for ZReadOrDeserializeErrorTuple4<A, B, C, D>
where
A: Debug,
A: TryFrom<ZBytes>,
<A as TryFrom<ZBytes>>::Error: Debug,
B: Debug,
B: TryFrom<ZBytes>,
<B as TryFrom<ZBytes>>::Error: Debug,
C: Debug,
C: TryFrom<ZBytes>,
<C as TryFrom<ZBytes>>::Error: Debug,
D: Debug,
D: TryFrom<ZBytes>,
<D as TryFrom<ZBytes>>::Error: Debug,
{
}

impl<A, B, C, D> Deserialize<(A, B, C, D)> for ZSerde
where
A: TryFrom<ZBytes>,
Expand Down

0 comments on commit fc18f90

Please sign in to comment.