NetDoc: Make AddrPortPattern implement serde traits

I'm using serde_with here to just re-use the Display and FromStr
implementations, since those are what has proven easier to type in
the past.
This commit is contained in:
Nick Mathewson 2022-06-09 16:52:52 -04:00
parent 48a86506be
commit fb672dc688
4 changed files with 96 additions and 6 deletions

67
Cargo.lock generated
View File

@ -825,14 +825,38 @@ dependencies = [
"zeroize", "zeroize",
] ]
[[package]]
name = "darling"
version = "0.13.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a01d95850c592940db9b8194bc39f4bc0e89dee5c4265e4b1807c34a9aba453c"
dependencies = [
"darling_core 0.13.4",
"darling_macro 0.13.4",
]
[[package]] [[package]]
name = "darling" name = "darling"
version = "0.14.1" version = "0.14.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4529658bdda7fd6769b8614be250cdcfc3aeb0ee72fe66f9e41e5e5eb73eac02" checksum = "4529658bdda7fd6769b8614be250cdcfc3aeb0ee72fe66f9e41e5e5eb73eac02"
dependencies = [ dependencies = [
"darling_core", "darling_core 0.14.1",
"darling_macro", "darling_macro 0.14.1",
]
[[package]]
name = "darling_core"
version = "0.13.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "859d65a907b6852c9361e3185c862aae7fafd2887876799fa55f5f99dc40d610"
dependencies = [
"fnv",
"ident_case",
"proc-macro2",
"quote",
"strsim 0.10.0",
"syn",
] ]
[[package]] [[package]]
@ -849,13 +873,24 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "darling_macro"
version = "0.13.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c972679f83bdf9c42bd905396b6c3588a843a17f0f16dfcfa3e2c5d57441835"
dependencies = [
"darling_core 0.13.4",
"quote",
"syn",
]
[[package]] [[package]]
name = "darling_macro" name = "darling_macro"
version = "0.14.1" version = "0.14.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ddfc69c5bfcbd2fc09a0f38451d2daf0e372e367986a83906d1b0dbc88134fb5" checksum = "ddfc69c5bfcbd2fc09a0f38451d2daf0e372e367986a83906d1b0dbc88134fb5"
dependencies = [ dependencies = [
"darling_core", "darling_core 0.14.1",
"quote", "quote",
"syn", "syn",
] ]
@ -882,7 +917,7 @@ version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24c1b715c79be6328caa9a5e1a387a196ea503740f0722ec3dd8f67a9e72314d" checksum = "24c1b715c79be6328caa9a5e1a387a196ea503740f0722ec3dd8f67a9e72314d"
dependencies = [ dependencies = [
"darling", "darling 0.14.1",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn",
@ -2897,6 +2932,28 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "serde_with"
version = "1.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "678b5a069e50bf00ecd22d0cd8ddf7c236f68581b03db652061ed5eb13a312ff"
dependencies = [
"serde",
"serde_with_macros",
]
[[package]]
name = "serde_with_macros"
version = "1.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e182d6ec6f05393cc0e5ed1bf81ad6db3a8feedf8ee515ecdd369809bcce8082"
dependencies = [
"darling 0.13.4",
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "serial_test" name = "serial_test"
version = "0.7.0" version = "0.7.0"
@ -3787,6 +3844,8 @@ dependencies = [
"phf", "phf",
"rand 0.8.5", "rand 0.8.5",
"serde", "serde",
"serde_json",
"serde_with",
"signature", "signature",
"thiserror", "thiserror",
"time", "time",

View File

@ -55,6 +55,7 @@ once_cell = "1"
phf = { version = "0.10.0", features = ["macros"] } phf = { version = "0.10.0", features = ["macros"] }
rand = { version = "0.8", optional = true } rand = { version = "0.8", optional = true }
serde = "1.0.103" serde = "1.0.103"
serde_with = "1.14.0"
signature = "1" signature = "1"
thiserror = "1" thiserror = "1"
time = { version = "0.3", features = ["std", "parsing", "macros"] } time = { version = "0.3", features = ["std", "parsing", "macros"] }
@ -71,3 +72,4 @@ weak-table = "0.3.0"
[dev-dependencies] [dev-dependencies]
hex-literal = "0.3" hex-literal = "0.3"
serde_json = "1.0.50"

View File

@ -1 +1,2 @@
MODIFIED: AddrPortPattern and friends now impl Eq and PartialEq. MODIFIED: AddrPortPattern and friends now impl Eq and PartialEq.
MODIFIED: AddrPortPattern now implements {Des,S}erialize

View File

@ -130,7 +130,9 @@ impl Display for AddrPolicyRule {
/// assert!(pat.matches(&localhost, 22)); /// assert!(pat.matches(&localhost, 22));
/// assert!(! pat.matches(&not_localhost, 22)); /// assert!(! pat.matches(&not_localhost, 22));
/// ``` /// ```
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(
Clone, Debug, Eq, PartialEq, serde_with::SerializeDisplay, serde_with::DeserializeFromStr,
)]
pub struct AddrPortPattern { pub struct AddrPortPattern {
/// A pattern to match somewhere between zero and all IP addresses. /// A pattern to match somewhere between zero and all IP addresses.
pattern: IpPattern, pattern: IpPattern,
@ -176,6 +178,11 @@ impl FromStr for AddrPortPattern {
} }
/// A pattern that matches one or more IP addresses. /// A pattern that matches one or more IP addresses.
//
// TODO(nickm): At present there is no way for Display or FromStr to distinguish
// V4Star, V6Star, and Star. If we decide it's important to have a syntax for
// "all IPv4 addresses" that isn't "0.0.0.0/0", we'll need to revisit that.
// At present, C tor allows '*', '*4', and '*6'.
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
enum IpPattern { enum IpPattern {
/// Match all addresses. /// Match all addresses.
@ -379,4 +386,25 @@ mod test {
); );
Ok(()) Ok(())
} }
#[test]
fn serde() {
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize, Eq, PartialEq)]
struct X {
p1: AddrPortPattern,
p2: AddrPortPattern,
}
let x = X {
p1: "127.0.0.1/8:9-10".parse().unwrap(),
p2: "*:80".parse().unwrap(),
};
let encoded = serde_json::to_string(&x).unwrap();
let expected = r#"{"p1":"127.0.0.1/8:9-10","p2":"*:80"}"#;
let x2: X = serde_json::from_str(&encoded).unwrap();
let x3: X = serde_json::from_str(expected).unwrap();
assert_eq!(&x2, &x3);
assert_eq!(&x2, &x);
}
} }