Skip to content

Commit

Permalink
Accept either strings or structs for hosts (#6763)
Browse files Browse the repository at this point in the history
## Summary

Technically a struct did work in the last release, so let's not break
it.
  • Loading branch information
charliermarsh authored Aug 28, 2024
1 parent 71f5998 commit af32388
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
21 changes: 19 additions & 2 deletions crates/uv-configuration/src/trusted_host.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::str::FromStr;

use url::Url;

/// A trusted host, which could be a host or a host-port pair.
Expand Down Expand Up @@ -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 }),
}
}
}

Expand Down
7 changes: 6 additions & 1 deletion crates/uv/tests/show_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3419,7 +3419,7 @@ fn allow_insecure_host() -> anyhow::Result<()> {

let config = context.temp_dir.child("uv.toml");
config.write_str(indoc::indoc! {r#"
allow-insecure-host = ["google.com"]
allow-insecure-host = ["google.com", { host = "example.com" }]
"#})?;

let requirements_in = context.temp_dir.child("requirements.in");
Expand Down Expand Up @@ -3495,6 +3495,11 @@ fn allow_insecure_host() -> anyhow::Result<()> {
host: "google.com",
port: None,
},
TrustedHost {
scheme: None,
host: "example.com",
port: None,
},
],
no_build_isolation: false,
no_build_isolation_package: [],
Expand Down

0 comments on commit af32388

Please sign in to comment.