Accept either strings or structs for hosts (#6763)

## Summary

Technically a struct did work in the last release, so let's not break
it.
This commit is contained in:
Charlie Marsh 2024-08-28 12:36:12 -04:00 committed by GitHub
parent 71f5998752
commit af323888ee
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 25 additions and 3 deletions

View file

@ -1,3 +1,5 @@
use std::str::FromStr;
use url::Url;
/// A trusted host, which could be a host or a host-port pair.
@ -31,13 +33,28 @@ impl TrustedHost {
}
}
#[derive(serde::Deserialize)]
#[serde(untagged)]
enum TrustHostWire {
String(String),
Struct {
scheme: Option<String>,
host: String,
port: Option<u16>,
},
}
impl<'de> serde::de::Deserialize<'de> for TrustedHost {
fn deserialize<D>(deserializer: D) -> Result<TrustedHost, D::Error>
where
D: serde::de::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
s.parse().map_err(serde::de::Error::custom)
let helper = TrustHostWire::deserialize(deserializer)?;
match helper {
TrustHostWire::String(s) => TrustedHost::from_str(&s).map_err(serde::de::Error::custom),
TrustHostWire::Struct { scheme, host, port } => Ok(TrustedHost { scheme, host, port }),
}
}
}