use std::collections::BTreeMap;
use serde::{Serialize, Deserialize};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub struct RegistryUrl {
pub scheme: String,
pub host: String,
pub port: u16,
pub username: Option<String>,
pub password: Option<String>,
pub parameters: BTreeMap<String, String>,
}
impl RegistryUrl {
pub fn new(scheme: &str, host: &str, port: u16) -> Self {
Self {
scheme: scheme.to_string(),
host: host.to_string(),
port,
username: None,
password: None,
parameters: BTreeMap::new(),
}
}
pub fn with_credentials(mut self, username: Option<String>, password: Option<String>) -> Self {
self.username = username;
self.password = password;
self
}
pub fn add_param(&mut self, key: &str, value: &str) {
self.parameters.insert(key.to_string(), value.to_string());
}
pub fn get_param(&self, key: &str) -> Option<&String> {
self.parameters.get(key)
}
}
impl std::fmt::Display for RegistryUrl {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}://", self.scheme)?;
if let (Some(u), Some(p)) = (&self.username, &self.password) {
write!(f, "{}:{}@", u, p)?;
}
write!(f, "{}:{}", self.host, self.port)?;
if !self.parameters.is_empty() {
write!(f, "?")?;
for (i, (k, v)) in self.parameters.iter().enumerate() {
if i > 0 {
write!(f, "&")?;
}
write!(f, "{}={}", k, v)?;
}
}
Ok(())
}
}