♻️ refactor: replace all unwraps in my code with question marks
parent
c7681ce9f5
commit
33e6ae29ca
|
@ -25,8 +25,3 @@ I can't remember the trick for that. I think last time I did this
|
|||
# 01FPDWYH3ZY52DFF6PNRSA63GB
|
||||
|
||||
Sending an invalid packet directly to the server's UDP port crashes the server.
|
||||
|
||||
# 01FPDY51GHC7NRFV7Z14Q1KV7N
|
||||
|
||||
Remove all unwraps. Since I used abort-on-panic to make the binary smaller,
|
||||
it's not giving any error message for panics
|
||||
|
|
|
@ -92,7 +92,7 @@ impl Message {
|
|||
Self::write_response_2 (&mut dummy_writer, x)?;
|
||||
|
||||
// Write length and real params to real output
|
||||
let len = u32::try_from (dummy_writer.position).unwrap ();
|
||||
let len = u32::try_from (dummy_writer.position)?;
|
||||
w.write_all (&len.to_le_bytes ())?;
|
||||
Self::write_response_2 (w, x)?;
|
||||
},
|
||||
|
@ -206,7 +206,7 @@ mod test {
|
|||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_write_2 () {
|
||||
fn test_write_2 () -> Result <(), MessageError> {
|
||||
for (input, expected) in [
|
||||
(
|
||||
vec! [
|
||||
|
@ -254,13 +254,15 @@ mod test {
|
|||
],
|
||||
),
|
||||
] {
|
||||
let actual = Message::many_to_vec (&input).unwrap ();
|
||||
let actual = Message::many_to_vec (&input)?;
|
||||
assert_eq! (actual, expected, "{:?}", input);
|
||||
}
|
||||
|
||||
Ok (())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_write_1 () {
|
||||
fn test_write_1 () -> Result <(), MessageError> {
|
||||
for (input, expected) in [
|
||||
(
|
||||
Message::Request1 {
|
||||
|
@ -302,13 +304,15 @@ mod test {
|
|||
],
|
||||
),
|
||||
].into_iter () {
|
||||
let actual = input.to_vec ().unwrap ();
|
||||
let actual = input.to_vec ()?;
|
||||
assert_eq! (actual, expected, "{:?}", input);
|
||||
}
|
||||
|
||||
Ok (())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_read_2 () {
|
||||
fn test_read_2 () -> Result <(), MessageError> {
|
||||
for input in [
|
||||
vec! [
|
||||
Message::Request1 {
|
||||
|
@ -330,9 +334,11 @@ mod test {
|
|||
}),
|
||||
],
|
||||
].into_iter () {
|
||||
let encoded = Message::many_to_vec (&input).unwrap ();
|
||||
let decoded = Message::from_slice2 (&encoded).unwrap ();
|
||||
let encoded = Message::many_to_vec (&input)?;
|
||||
let decoded = Message::from_slice2 (&encoded)?;
|
||||
assert_eq! (input, decoded);
|
||||
}
|
||||
|
||||
Ok (())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,9 +29,9 @@ pub async fn server <I: Iterator <Item=String>> (mut args: I) -> Result <(), App
|
|||
println! ("Warning: Can't find our own MAC address. We won't be able to respond to MAC-specific lookaround requests");
|
||||
}
|
||||
|
||||
let socket = UdpSocket::bind (SocketAddrV4::new (Ipv4Addr::from_str (&bind_addr)?, common_params.server_port)).await.unwrap ();
|
||||
let socket = UdpSocket::bind (SocketAddrV4::new (Ipv4Addr::from_str (&bind_addr)?, common_params.server_port)).await?;
|
||||
|
||||
socket.join_multicast_v4 (common_params.multicast_addr, [0u8, 0, 0, 0].into ()).unwrap ();
|
||||
socket.join_multicast_v4 (common_params.multicast_addr, [0u8, 0, 0, 0].into ())?;
|
||||
|
||||
let mut recent_idem_ids = Vec::with_capacity (32);
|
||||
|
||||
|
@ -72,7 +72,7 @@ pub async fn server <I: Iterator <Item=String>> (mut args: I) -> Result <(), App
|
|||
};
|
||||
|
||||
if let Some (resp) = resp {
|
||||
socket.send_to (&Message::many_to_vec (&resp)?, remote_addr).await.unwrap ();
|
||||
socket.send_to (&Message::many_to_vec (&resp)?, remote_addr).await?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
12
src/tlv.rs
12
src/tlv.rs
|
@ -90,15 +90,17 @@ impl <R: std::io::Read> Reader <R> {
|
|||
|
||||
#[cfg (test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_1 () {
|
||||
fn test_1 () -> Result <()> {
|
||||
use std::io::Cursor;
|
||||
|
||||
let b = "hi there".as_bytes ();
|
||||
|
||||
let mut w = Cursor::new (Vec::default ());
|
||||
|
||||
super::Writer::lv_bytes (&mut w, b).unwrap ();
|
||||
super::Writer::lv_bytes (&mut w, b)?;
|
||||
|
||||
let v = w.into_inner ();
|
||||
|
||||
|
@ -110,9 +112,11 @@ mod test {
|
|||
|
||||
let mut r = Cursor::new (v);
|
||||
|
||||
let buf = super::Reader::lv_bytes_to_vec (&mut r, 1024).unwrap ();
|
||||
let buf = Reader::lv_bytes_to_vec (&mut r, 1024)?;
|
||||
|
||||
assert_eq! (buf.len (), b.len ());
|
||||
assert_eq! (b, &buf [0..usize::try_from (buf.len ()).unwrap ()]);
|
||||
assert_eq! (b, &buf);
|
||||
|
||||
Ok (())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue