diff --git a/aquatic_http/src/lib/protocol/mod.rs b/aquatic_http/src/lib/protocol/mod.rs index 653c800..4d5a440 100644 --- a/aquatic_http/src/lib/protocol/mod.rs +++ b/aquatic_http/src/lib/protocol/mod.rs @@ -14,6 +14,7 @@ use serde_helpers::*; pub struct PeerId( #[serde( deserialize_with = "deserialize_20_bytes", + serialize_with = "serialize_20_bytes", )] pub [u8; 20] ); @@ -24,11 +25,13 @@ pub struct PeerId( pub struct InfoHash( #[serde( deserialize_with = "deserialize_20_bytes", + serialize_with = "serialize_20_bytes", )] pub [u8; 20] ); + #[derive(Clone, Copy, Debug, Serialize)] pub struct ResponsePeer { pub ip_address: IpAddr, @@ -225,6 +228,13 @@ pub enum Response { impl Response { pub fn to_bytes(self) -> Vec { - bendy::serde::to_bytes(&self).unwrap() + match bendy::serde::to_bytes(&self){ + Ok(bytes) => bytes, + Err(err) => { + log::error!("error encoding response: {}", err); + + Vec::new() + } + } } } \ No newline at end of file diff --git a/aquatic_http/src/lib/protocol/serde_helpers.rs b/aquatic_http/src/lib/protocol/serde_helpers.rs index 94b39bf..f599ce5 100644 --- a/aquatic_http/src/lib/protocol/serde_helpers.rs +++ b/aquatic_http/src/lib/protocol/serde_helpers.rs @@ -91,6 +91,15 @@ pub fn deserialize_20_bytes<'de, D>( } +#[inline] +pub fn serialize_20_bytes( + bytes: &[u8; 20], + serializer: S +) -> Result where S: Serializer { + serializer.serialize_bytes(bytes) +} + + pub struct InfoHashVecVisitor; @@ -162,12 +171,10 @@ pub fn serialize_response_peers_compact( bytes.extend_from_slice(&peer.port.to_be_bytes()) }, IpAddr::V6(_) => { - continue + continue; } } } - let text: String = bytes.into_iter().map(|byte| byte as char).collect(); - - serializer.serialize_str(&text) + serializer.serialize_bytes(&bytes) } \ No newline at end of file