diff --git a/Cargo.toml b/Cargo.toml index cfb8826..acb298a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,6 +34,10 @@ 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" @@ -41,3 +45,7 @@ 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 + diff --git a/src/lib.rs b/src/lib.rs index c5e75d2..027fd3d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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, @@ -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 = std::result::Result; /// A trait for objects which can be converted or resolved to one or more @@ -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), @@ -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> { (*self).into_target_addr() @@ -302,7 +304,9 @@ mod tests { } fn into_target_addr<'a, T>(t: T) -> Result> - where T: IntoTargetAddr<'a> { + where + T: IntoTargetAddr<'a>, + { t.into_target_addr() } @@ -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); + } }