From fc18f90310cd451a2f3d6e579dd9cfd9dba4bda6 Mon Sep 17 00:00:00 2001 From: Luca Cominardi Date: Wed, 26 Jun 2024 10:06:23 +0200 Subject: [PATCH] Implement Error trait for ZBytes error types (#1192) * Implement Error trait for ZBytes error types * Improve error display message --- zenoh/src/api/bytes.rs | 153 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 153 insertions(+) diff --git a/zenoh/src/api/bytes.rs b/zenoh/src/api/bytes.rs index 60254ff321..b1d1ff079f 100644 --- a/zenoh/src/api/bytes.rs +++ b/zenoh/src/api/bytes.rs @@ -345,6 +345,28 @@ where Deserialize(>::Error), } +impl std::fmt::Display for ZReadOrDeserializeError +where + T: Debug, + T: TryFrom, + >::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 std::error::Error for ZReadOrDeserializeError +where + T: Debug, + T: TryFrom, + >::Error: Debug, +{ +} + impl ZBytesReader<'_> { /// Returns the number of bytes that can still be read pub fn remaining(&self) -> usize { @@ -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 for ZSerde { type Output = ZBytes; @@ -2255,6 +2285,38 @@ where Two(ZReadOrDeserializeError), } +impl std::fmt::Display for ZReadOrDeserializeErrorTuple2 +where + A: Debug, + A: TryFrom, + >::Error: Debug, + B: Debug, + B: TryFrom, + >::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 std::error::Error for ZReadOrDeserializeErrorTuple2 +where + A: Debug, + A: TryFrom, + >::Error: Debug, + B: Debug, + B: TryFrom, + >::Error: Debug, +{ +} + impl Deserialize<(A, B)> for ZSerde where A: TryFrom, @@ -2408,6 +2470,47 @@ where Three(ZReadOrDeserializeError), } +impl std::fmt::Display for ZReadOrDeserializeErrorTuple3 +where + A: Debug, + A: TryFrom, + >::Error: Debug, + B: Debug, + B: TryFrom, + >::Error: Debug, + C: Debug, + C: TryFrom, + >::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 std::error::Error for ZReadOrDeserializeErrorTuple3 +where + A: Debug, + A: TryFrom, + >::Error: Debug, + B: Debug, + B: TryFrom, + >::Error: Debug, + C: Debug, + C: TryFrom, + >::Error: Debug, +{ +} + impl Deserialize<(A, B, C)> for ZSerde where A: TryFrom, @@ -2585,6 +2688,56 @@ where Four(ZReadOrDeserializeError), } +impl std::fmt::Display for ZReadOrDeserializeErrorTuple4 +where + A: Debug, + A: TryFrom, + >::Error: Debug, + B: Debug, + B: TryFrom, + >::Error: Debug, + C: Debug, + C: TryFrom, + >::Error: Debug, + D: Debug, + D: TryFrom, + >::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 std::error::Error for ZReadOrDeserializeErrorTuple4 +where + A: Debug, + A: TryFrom, + >::Error: Debug, + B: Debug, + B: TryFrom, + >::Error: Debug, + C: Debug, + C: TryFrom, + >::Error: Debug, + D: Debug, + D: TryFrom, + >::Error: Debug, +{ +} + impl Deserialize<(A, B, C, D)> for ZSerde where A: TryFrom,