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:
parent
48a86506be
commit
fb672dc688
|
@ -825,14 +825,38 @@ dependencies = [
|
|||
"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]]
|
||||
name = "darling"
|
||||
version = "0.14.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4529658bdda7fd6769b8614be250cdcfc3aeb0ee72fe66f9e41e5e5eb73eac02"
|
||||
dependencies = [
|
||||
"darling_core",
|
||||
"darling_macro",
|
||||
"darling_core 0.14.1",
|
||||
"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]]
|
||||
|
@ -849,13 +873,24 @@ dependencies = [
|
|||
"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]]
|
||||
name = "darling_macro"
|
||||
version = "0.14.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ddfc69c5bfcbd2fc09a0f38451d2daf0e372e367986a83906d1b0dbc88134fb5"
|
||||
dependencies = [
|
||||
"darling_core",
|
||||
"darling_core 0.14.1",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
@ -882,7 +917,7 @@ version = "0.11.2"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "24c1b715c79be6328caa9a5e1a387a196ea503740f0722ec3dd8f67a9e72314d"
|
||||
dependencies = [
|
||||
"darling",
|
||||
"darling 0.14.1",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
|
@ -2897,6 +2932,28 @@ dependencies = [
|
|||
"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]]
|
||||
name = "serial_test"
|
||||
version = "0.7.0"
|
||||
|
@ -3787,6 +3844,8 @@ dependencies = [
|
|||
"phf",
|
||||
"rand 0.8.5",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"serde_with",
|
||||
"signature",
|
||||
"thiserror",
|
||||
"time",
|
||||
|
|
|
@ -55,6 +55,7 @@ once_cell = "1"
|
|||
phf = { version = "0.10.0", features = ["macros"] }
|
||||
rand = { version = "0.8", optional = true }
|
||||
serde = "1.0.103"
|
||||
serde_with = "1.14.0"
|
||||
signature = "1"
|
||||
thiserror = "1"
|
||||
time = { version = "0.3", features = ["std", "parsing", "macros"] }
|
||||
|
@ -71,3 +72,4 @@ weak-table = "0.3.0"
|
|||
|
||||
[dev-dependencies]
|
||||
hex-literal = "0.3"
|
||||
serde_json = "1.0.50"
|
||||
|
|
|
@ -1 +1,2 @@
|
|||
MODIFIED: AddrPortPattern and friends now impl Eq and PartialEq.
|
||||
MODIFIED: AddrPortPattern now implements {Des,S}erialize
|
||||
|
|
|
@ -130,7 +130,9 @@ impl Display for AddrPolicyRule {
|
|||
/// assert!(pat.matches(&localhost, 22));
|
||||
/// assert!(! pat.matches(¬_localhost, 22));
|
||||
/// ```
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
#[derive(
|
||||
Clone, Debug, Eq, PartialEq, serde_with::SerializeDisplay, serde_with::DeserializeFromStr,
|
||||
)]
|
||||
pub struct AddrPortPattern {
|
||||
/// A pattern to match somewhere between zero and all IP addresses.
|
||||
pattern: IpPattern,
|
||||
|
@ -176,6 +178,11 @@ impl FromStr for AddrPortPattern {
|
|||
}
|
||||
|
||||
/// 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)]
|
||||
enum IpPattern {
|
||||
/// Match all addresses.
|
||||
|
@ -379,4 +386,25 @@ mod test {
|
|||
);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue