From 4774cbd18d06b2c3d6ece16784275b1d1a435fdc Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Mon, 14 Feb 2022 18:48:35 +0000 Subject: [PATCH 1/2] Provide, and use From impl for InternalError Adding this autoconversion is quite safe since every error generation site is explicit and has its own context, and we don't really need to add more. This simplifies the code and will simplify future work. --- crates/tor-socksproto/src/err.rs | 2 +- crates/tor-socksproto/src/handshake.rs | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/crates/tor-socksproto/src/err.rs b/crates/tor-socksproto/src/err.rs index 0733cf50e..c68125c03 100644 --- a/crates/tor-socksproto/src/err.rs +++ b/crates/tor-socksproto/src/err.rs @@ -41,7 +41,7 @@ pub enum Error { /// Something went wrong with the programming of this module. #[error("Internal programming error while handling SOCKS handshake")] - Internal(InternalError), + Internal(#[from] InternalError), } // Note: at present, tor-socksproto isn't used in any settings where ErrorKind diff --git a/crates/tor-socksproto/src/handshake.rs b/crates/tor-socksproto/src/handshake.rs index 742c25e86..155ea5748 100644 --- a/crates/tor-socksproto/src/handshake.rs +++ b/crates/tor-socksproto/src/handshake.rs @@ -113,10 +113,10 @@ impl SocksHandshake { let mut r = Reader::from_slice(input); let version = r.take_u8()?.try_into()?; if version != SocksVersion::V4 { - return Err(Error::Internal(internal!( + return Err(internal!( "called s4 on wrong type {:?}", version - ))); + ).into()); } let cmd: SocksCmd = r.take_u8()?.into(); @@ -157,10 +157,10 @@ impl SocksHandshake { let mut r = Reader::from_slice(input); let version: SocksVersion = r.take_u8()?.try_into()?; if version != SocksVersion::V5 { - return Err(Error::Internal(internal!( + return Err(internal!( "called on wrong handshake type {:?}", version - ))); + ).into()); } /// Constant for Username/Password-style authentication. @@ -220,10 +220,10 @@ impl SocksHandshake { let version: SocksVersion = r.take_u8()?.try_into()?; if version != SocksVersion::V5 { - return Err(Error::Internal(internal!( + return Err(internal!( "called s5 on non socks5 handshake with type {:?}", version - ))); + ).into()); } let cmd = r.take_u8()?.into(); let _ignore = r.take_u8()?; @@ -233,7 +233,7 @@ impl SocksHandshake { let auth = self .socks5_auth .take() - .ok_or_else(|| Error::Internal(internal!("called s5 without negotiating auth")))?; + .ok_or_else(|| internal!("called s5 without negotiating auth"))?; let request = SocksRequest::new(version, cmd, addr, port, auth)?; From 31075e80467575212c069430d463bc4044585716 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Mon, 14 Feb 2022 14:47:42 -0500 Subject: [PATCH 2/2] Run rustfmt. --- crates/tor-socksproto/src/handshake.rs | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/crates/tor-socksproto/src/handshake.rs b/crates/tor-socksproto/src/handshake.rs index 155ea5748..3344f8c3f 100644 --- a/crates/tor-socksproto/src/handshake.rs +++ b/crates/tor-socksproto/src/handshake.rs @@ -113,10 +113,7 @@ impl SocksHandshake { let mut r = Reader::from_slice(input); let version = r.take_u8()?.try_into()?; if version != SocksVersion::V4 { - return Err(internal!( - "called s4 on wrong type {:?}", - version - ).into()); + return Err(internal!("called s4 on wrong type {:?}", version).into()); } let cmd: SocksCmd = r.take_u8()?.into(); @@ -157,10 +154,7 @@ impl SocksHandshake { let mut r = Reader::from_slice(input); let version: SocksVersion = r.take_u8()?.try_into()?; if version != SocksVersion::V5 { - return Err(internal!( - "called on wrong handshake type {:?}", - version - ).into()); + return Err(internal!("called on wrong handshake type {:?}", version).into()); } /// Constant for Username/Password-style authentication. @@ -220,10 +214,9 @@ impl SocksHandshake { let version: SocksVersion = r.take_u8()?.try_into()?; if version != SocksVersion::V5 { - return Err(internal!( - "called s5 on non socks5 handshake with type {:?}", - version - ).into()); + return Err( + internal!("called s5 on non socks5 handshake with type {:?}", version).into(), + ); } let cmd = r.take_u8()?.into(); let _ignore = r.take_u8()?;