use std::fmt;
#[derive(Clone, PartialEq, Eq)]
pub struct AuthConfig {
user: String,
password: String,
}
impl fmt::Debug for AuthConfig {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("AuthConfig")
.field("user", &"<redacted>")
.field("password", &"<redacted>")
.finish()
}
}
impl AuthConfig {
pub fn new(user: String, password: String) -> Option<Self> {
if user.is_empty() {
None
} else {
Some(Self { user, password })
}
}
pub fn user(&self) -> &str {
&self.user
}
pub fn password(&self) -> &str {
&self.password
}
pub fn auth_text(&self) -> String {
format!("{}:{}", self.user, self.password)
}
}
impl fmt::Display for AuthConfig {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "{}:{}", self.user, self.password)
}
}
#[derive(Clone)]
pub struct BasicCred {
pub user: String,
pub password: String,
pub host: String,
pub port: u16,
pub path: String,
pub activated: bool,
}
impl fmt::Debug for BasicCred {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("BasicCred")
.field("user", &"<redacted>")
.field("password", &"<redacted>")
.field("host", &self.host)
.field("port", &self.port)
.field("path", &self.path)
.field("activated", &self.activated)
.finish()
}
}
impl BasicCred {
pub fn new(
user: String,
password: String,
host: String,
port: u16,
path: String,
activated: bool,
) -> Self {
let mut path = path;
if path.is_empty() || !path.ends_with('/') {
path.push('/');
}
Self {
user,
password,
host,
port,
path,
activated,
}
}
pub fn activate(&mut self) {
self.activated = true;
}
pub fn is_activated(&self) -> bool {
self.activated
}
}
impl PartialEq for BasicCred {
fn eq(&self, other: &Self) -> bool {
self.host == other.host && self.port == other.port && self.path == other.path
}
}
impl Eq for BasicCred {}
impl PartialOrd for BasicCred {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for BasicCred {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
match self.host.cmp(&other.host) {
std::cmp::Ordering::Equal => match self.port.cmp(&other.port) {
std::cmp::Ordering::Equal => other.path.cmp(&self.path),
order => order,
},
order => order,
}
}
}