#[derive(Clone, Default)]
#[cfg_attr(feature = "js", napi_derive::napi(object))]
#[cfg_attr(feature = "py", pyo3::pyclass(get_all, set_all))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct Config {
pub username: String,
pub password: String, pub host: Option<String>,
pub port: Option<u16>,
pub tls: Option<bool>,
}
impl Config {
pub fn new(username: impl ToString, password: impl ToString) -> Self {
Self {
username: username.to_string(),
password: password.to_string(),
host: None,
port: None,
tls: None,
}
}
#[inline]
pub(crate) fn host(&self) -> &str {
self.host.as_deref().unwrap_or("api.code.mp")
}
#[inline]
pub(crate) fn port(&self) -> u16 {
self.port.unwrap_or(50053)
}
#[inline]
pub(crate) fn tls(&self) -> bool {
self.tls.unwrap_or(true)
}
pub(crate) fn endpoint(&self) -> String {
format!(
"{}://{}:{}",
if self.tls() { "https" } else { "http" },
self.host(),
self.port()
)
}
}
impl std::fmt::Debug for Config {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if f.alternate() {
write!(f,
r#"""Config {{
username: {},
password: ********,
host: {:#?},
port: {:#?},
tls: {:#?}
}}"""#,
self.username, self.host, self.port, self.tls
)
} else {
write!(f, "Config {{ username: {}, password: ********, host: {:?}, port: {:?}, tls: {:?} }}", self.username, self.host, self.port, self.tls)
}
}
}