2024-09-11 15:12:31 +02:00
|
|
|
//! # Config
|
|
|
|
//! Data structure defining clients configuration
|
|
|
|
|
|
|
|
/// Configuration struct for `codemp` client
|
2024-09-11 15:45:35 +02:00
|
|
|
///
|
|
|
|
/// username and password are required fields, while everything else is optional
|
|
|
|
///
|
|
|
|
/// host, port and tls affect all connections to all grpc services
|
|
|
|
/// resulting endpoint is composed like this:
|
|
|
|
/// http{tls?'s':''}://{host}:{port}
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
#[cfg_attr(feature = "js", napi_derive::napi(object))]
|
2024-09-19 21:32:46 +02:00
|
|
|
#[cfg_attr(
|
|
|
|
any(feature = "py", feature = "py-noabi"),
|
|
|
|
pyo3::pyclass(get_all, set_all)
|
|
|
|
)]
|
2024-09-11 15:45:35 +02:00
|
|
|
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
2024-09-11 15:12:31 +02:00
|
|
|
pub struct Config {
|
|
|
|
/// user identifier used to register, possibly your email
|
|
|
|
pub username: String,
|
|
|
|
/// user password chosen upon registration
|
|
|
|
pub password: String,
|
|
|
|
/// address of server to connect to, default api.code.mp
|
|
|
|
pub host: Option<String>,
|
|
|
|
/// port to connect to, default 50053
|
|
|
|
pub port: Option<u16>,
|
|
|
|
/// enable or disable tls, default true
|
|
|
|
pub tls: Option<bool>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Config {
|
2024-09-11 15:45:35 +02:00
|
|
|
/// construct a new Config object, with given username and password
|
2024-09-21 12:05:09 +02:00
|
|
|
pub fn new(username: impl ToString, password: impl ToString) -> Self {
|
2024-09-14 00:17:46 +02:00
|
|
|
Self {
|
2024-09-21 12:05:09 +02:00
|
|
|
username: username.to_string(),
|
|
|
|
password: password.to_string(),
|
2024-09-14 00:17:46 +02:00
|
|
|
host: None,
|
|
|
|
port: None,
|
|
|
|
tls: None,
|
|
|
|
}
|
2024-09-11 15:45:35 +02:00
|
|
|
}
|
|
|
|
|
2024-09-11 15:12:31 +02:00
|
|
|
#[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!(
|
2024-09-11 17:50:40 +02:00
|
|
|
"{}://{}:{}",
|
|
|
|
if self.tls() { "https" } else { "http" },
|
2024-09-11 15:12:31 +02:00
|
|
|
self.host(),
|
|
|
|
self.port()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|