Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support serde and borsh Fixes for #62 #64

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

33 changes: 25 additions & 8 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,
fmt,
Expand All @@ -8,13 +16,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 @@ -99,7 +100,7 @@ impl Stream for ProxyAddrsStream {
}

/// A SOCKS connection target.
#[derive(Debug, PartialEq, Eq, Clone)]
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, BorshSerialize, BorshDeserialize, Clone)]
pub enum TargetAddr<'a> {
/// Connect to an IP address.
Ip(SocketAddr),
Expand Down Expand Up @@ -427,4 +428,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);
}
}