mirror of
https://github.com/astral-sh/uv.git
synced 2025-08-04 10:58:28 +00:00
Implement deserialization for trusted host (#6716)
This commit is contained in:
parent
14074f8775
commit
ffb0c304d1
2 changed files with 177 additions and 2 deletions
|
@ -1,8 +1,7 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
use url::Url;
|
||||
|
||||
/// A trusted host, which could be a host or a host-port pair.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct TrustedHost {
|
||||
scheme: Option<String>,
|
||||
host: String,
|
||||
|
@ -32,6 +31,26 @@ impl TrustedHost {
|
|||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
impl serde::Serialize for TrustedHost {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde::ser::Serializer,
|
||||
{
|
||||
let s = self.to_string();
|
||||
serializer.serialize_str(&s)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum TrustedHostError {
|
||||
#[error("missing host for `--trusted-host`: `{0}`")]
|
||||
|
@ -73,6 +92,22 @@ impl std::str::FromStr for TrustedHost {
|
|||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for TrustedHost {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
if let Some(scheme) = &self.scheme {
|
||||
write!(f, "{}://{}", scheme, self.host)?;
|
||||
} else {
|
||||
write!(f, "{}", self.host)?;
|
||||
}
|
||||
|
||||
if let Some(port) = self.port {
|
||||
write!(f, ":{port}")?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "schemars")]
|
||||
impl schemars::JsonSchema for TrustedHost {
|
||||
fn schema_name() -> String {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue