From 5092b05fdc06729f6a8c073fc87838b9ca7a7727 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Wed, 28 Aug 2024 12:29:31 -0400 Subject: [PATCH] Accept either strings or structs for hosts --- crates/uv-configuration/src/trusted_host.rs | 21 +++++++++++++++++++-- crates/uv/tests/show_settings.rs | 7 ++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/crates/uv-configuration/src/trusted_host.rs b/crates/uv-configuration/src/trusted_host.rs index b99efc6eab5a..ea4f529caba4 100644 --- a/crates/uv-configuration/src/trusted_host.rs +++ b/crates/uv-configuration/src/trusted_host.rs @@ -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, + host: String, + port: Option, + }, +} + impl<'de> serde::de::Deserialize<'de> for TrustedHost { fn deserialize(deserializer: D) -> Result 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 }), + } } } diff --git a/crates/uv/tests/show_settings.rs b/crates/uv/tests/show_settings.rs index 7e9d8efa9e12..374ef8a5abf1 100644 --- a/crates/uv/tests/show_settings.rs +++ b/crates/uv/tests/show_settings.rs @@ -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"); @@ -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: [],