Skip to content

Commit

Permalink
Returns an Option for MdnsPeer::new(...)
Browse files Browse the repository at this point in the history
Per @MarcoPolo points that an error isn't useful for the caller
and following MdnsPacket API.

See also: libp2p#2311 (comment)
  • Loading branch information
jochasinga committed Nov 17, 2021
1 parent ace9e37 commit 7dc6b1f
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions protocols/mdns/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,7 @@ impl MdnsResponse {
_ => return None,
};

match MdnsPeer::new(&packet, record_value, record.ttl) {
Ok(peer) => Some(peer),
Err(err) => {
log::debug!("Creating mdns peer failed: {:?}", err);
None
}
}
MdnsPeer::new(&packet, record_value, record.ttl)
})
.collect();

Expand Down Expand Up @@ -209,7 +203,7 @@ impl MdnsPeer {
packet: &Packet<'_>,
record_value: String,
ttl: u32,
) -> Result<MdnsPeer, String> {
) -> Option<MdnsPeer> {
let mut my_peer_id: Option<PeerId> = None;
let addrs = packet
.additional
Expand Down Expand Up @@ -265,12 +259,12 @@ impl MdnsPeer {

match my_peer_id {
Some(peer_id) =>
Ok(MdnsPeer {
Some(MdnsPeer {
addrs,
peer_id,
ttl,
}),
None => Err("Missing Peer ID".to_string())
None => None,
}
}

Expand Down Expand Up @@ -309,7 +303,7 @@ mod tests {
use super::*;

#[test]
fn test_create_mdns_peer() -> Result<(), String> {
fn test_create_mdns_peer() {
let ttl = 300;
let peer_id = PeerId::random();

Expand Down Expand Up @@ -341,11 +335,9 @@ mod tests {
return Some(record_value);
}).next().unwrap();

let peer = MdnsPeer::new(&packet, record_value, ttl)?;
let peer = MdnsPeer::new(&packet, record_value, ttl).unwrap();
assert_eq!(peer.peer_id, peer_id);
}

Ok(())
}
}

Expand Down

0 comments on commit 7dc6b1f

Please sign in to comment.