From fd226930c2311c24aaf4d3fcf7f1912fc1c7ad71 Mon Sep 17 00:00:00 2001 From: Johan Klokkhammer Helsing Date: Thu, 11 Jul 2024 07:18:03 +0200 Subject: [PATCH 1/2] fix: error: direct implementation of `ToString` --- matchbox_protocol/src/lib.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/matchbox_protocol/src/lib.rs b/matchbox_protocol/src/lib.rs index 94515922..9bf180df 100644 --- a/matchbox_protocol/src/lib.rs +++ b/matchbox_protocol/src/lib.rs @@ -34,10 +34,12 @@ cfg_if! { if #[cfg(feature = "json")] { pub type JsonPeerRequest = PeerRequest; pub type JsonPeerEvent = PeerEvent; + use std::fmt; - impl ToString for JsonPeerRequest { - fn to_string(&self) -> String { - serde_json::to_string(self).expect("error serializing message") + + impl fmt::Display for JsonPeerRequest { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", serde_json::to_string(self).expect("error serializing message")) } } impl std::str::FromStr for JsonPeerRequest { @@ -48,9 +50,9 @@ cfg_if! { } } - impl ToString for JsonPeerEvent { - fn to_string(&self) -> String { - serde_json::to_string(self).expect("error serializing message") + impl fmt::Display for JsonPeerEvent { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", serde_json::to_string(self).expect("error serializing message")) } } impl std::str::FromStr for JsonPeerEvent { From 86b03d625aa0c15b8d57b33975e8c6d9228f4062 Mon Sep 17 00:00:00 2001 From: Johan Klokkhammer Helsing Date: Thu, 11 Jul 2024 07:30:26 +0200 Subject: [PATCH 2/2] fix: Better error handling for protocol display --- matchbox_protocol/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/matchbox_protocol/src/lib.rs b/matchbox_protocol/src/lib.rs index 9bf180df..30e14def 100644 --- a/matchbox_protocol/src/lib.rs +++ b/matchbox_protocol/src/lib.rs @@ -39,7 +39,7 @@ cfg_if! { impl fmt::Display for JsonPeerRequest { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}", serde_json::to_string(self).expect("error serializing message")) + write!(f, "{}", serde_json::to_string(self).map_err(|_| fmt::Error)?) } } impl std::str::FromStr for JsonPeerRequest { @@ -52,7 +52,7 @@ cfg_if! { impl fmt::Display for JsonPeerEvent { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}", serde_json::to_string(self).expect("error serializing message")) + write!(f, "{}", serde_json::to_string(self).map_err(|_| fmt::Error)?) } } impl std::str::FromStr for JsonPeerEvent {