Skip to content

Commit

Permalink
Fixes for #62
Browse files Browse the repository at this point in the history
  • Loading branch information
Rohit Kulkarni committed Feb 14, 2025
1 parent 2eeb99c commit 12f0b41
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,18 @@ futures-io = { version = "0.3", optional = true }
tokio = { version = "1.0", features = ["io-util", "net"], optional = true }
either = "1"
thiserror = "1.0"
serde = { version = "1.0", features = ["derive"] }
borsh = "0.10"
serde_json = "1.0" # Required for JSON serialization in tests


[dev-dependencies]
futures-executor = "0.3"
futures-util = { version = "0.3", default-features = false, features = ["io"] }
tokio = { version = "1.0", features = ["io-util", "rt-multi-thread", "net"] }
once_cell = "1.2.0"
smol = "2.0.0"
serde = { version = "1.0", features = ["derive"] }
borsh = "0.10"
serde_json = "1.0" # Required for JSON serialization in tests

40 changes: 30 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
use borsh::{BorshDeserialize, BorshSerialize};
use either::Either;
pub use error::Error;
use futures_util::{
future,
stream::{self, Once, Stream},
};
use serde::{Deserialize, Serialize};
use std::{
borrow::Cow,
io::Result as IoResult,
Expand All @@ -7,13 +15,6 @@ use std::{
vec,
};

use either::Either;
pub use error::Error;
use futures_util::{
future,
stream::{self, Once, Stream},
};

pub type Result<T> = std::result::Result<T, Error>;

/// A trait for objects which can be converted or resolved to one or more
Expand Down Expand Up @@ -98,7 +99,7 @@ impl Stream for ProxyAddrsStream {
}

/// A SOCKS connection target.
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
pub enum TargetAddr<'a> {
/// Connect to an IP address.
Ip(SocketAddr),
Expand Down Expand Up @@ -236,7 +237,8 @@ impl IntoTargetAddr<'static> for (String, u16) {
}

impl<'a, T> IntoTargetAddr<'a> for &'a T
where T: IntoTargetAddr<'a> + Copy
where
T: IntoTargetAddr<'a> + Copy,
{
fn into_target_addr(self) -> Result<TargetAddr<'a>> {
(*self).into_target_addr()
Expand Down Expand Up @@ -302,7 +304,9 @@ mod tests {
}

fn into_target_addr<'a, T>(t: T) -> Result<TargetAddr<'a>>
where T: IntoTargetAddr<'a> {
where
T: IntoTargetAddr<'a>,
{
t.into_target_addr()
}

Expand Down Expand Up @@ -374,4 +378,20 @@ mod tests {
let addr = "www.example.com:65536";
assert!(into_target_addr(addr).is_err());
}

#[test]
fn test_serde_serialization() {
let addr = TargetAddr::Domain(Cow::Borrowed("example.com"), 80);
let serialized = serde_json::to_string(&addr).unwrap();
let deserialized: TargetAddr = serde_json::from_str(&serialized).unwrap();
assert_eq!(addr, deserialized);
}

#[test]
fn test_borsh_serialization() {
let addr = TargetAddr::Domain(Cow::Borrowed("example.com"), 80);
let serialized = addr.try_to_vec().unwrap();
let deserialized: TargetAddr = TargetAddr::try_from_slice(&serialized).unwrap();
assert_eq!(addr, deserialized);
}
}

0 comments on commit 12f0b41

Please sign in to comment.