server: Simplify the debug print of various structs

And use derive_more more liberally to simplify the impls
This commit is contained in:
Valentin Tolmer
2024-09-07 23:50:08 +02:00
committed by nitnelave
parent 5db0072cfa
commit 65e2103365
9 changed files with 118 additions and 60 deletions

View File

@@ -14,9 +14,22 @@ use strum::{EnumString, IntoStaticStr};
pub use super::model::UserColumn;
pub use lldap_auth::types::UserId;
#[derive(PartialEq, Hash, Eq, Clone, Debug, Default, Serialize, Deserialize, DeriveValueType)]
#[derive(
PartialEq,
Hash,
Eq,
Clone,
Default,
Serialize,
Deserialize,
DeriveValueType,
derive_more::Debug,
derive_more::Display,
)]
#[serde(try_from = "&str")]
#[sea_orm(column_type = "String(Some(36))")]
#[debug(r#""{_0}""#)]
#[display("{_0}")]
pub struct Uuid(String);
impl Uuid {
@@ -53,12 +66,6 @@ impl<'a> std::convert::TryFrom<&'a str> for Uuid {
}
}
impl std::fmt::Display for Uuid {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.0.as_str())
}
}
#[cfg(test)]
#[macro_export]
macro_rules! uuid {
@@ -144,7 +151,17 @@ fn compare_str_case_insensitive(s1: &str, s2: &str) -> Ordering {
macro_rules! make_case_insensitive_comparable_string {
($c:ident) => {
#[derive(Clone, Debug, Default, Serialize, Deserialize, DeriveValueType)]
#[derive(
Clone,
Default,
Serialize,
Deserialize,
DeriveValueType,
derive_more::Debug,
derive_more::Display,
)]
#[debug(r#""{_0}""#)]
#[display("{_0}")]
pub struct $c(String);
impl PartialEq for $c {
@@ -199,12 +216,6 @@ macro_rules! make_case_insensitive_comparable_string {
}
}
impl std::fmt::Display for $c {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.0.as_str())
}
}
impl From<&$c> for Value {
fn from(user_id: &$c) -> Self {
user_id.as_str().into()
@@ -429,7 +440,6 @@ impl Default for User {
}
#[derive(
Debug,
Copy,
Clone,
PartialEq,
@@ -440,7 +450,9 @@ impl Default for User {
Serialize,
Deserialize,
DeriveValueType,
derive_more::Debug,
)]
#[debug("{_0}")]
pub struct GroupId(pub i32);
impl TryFromU64 for GroupId {

View File

@@ -17,13 +17,19 @@ use figment::{
Figment,
};
use figment_file_provider_adapter::FileAdapter;
use lettre::message::Mailbox;
use lldap_auth::opaque::{server::ServerSetup, KeyPair};
use secstr::SecUtf8;
use serde::{Deserialize, Serialize};
use url::Url;
#[derive(Clone, Debug, Deserialize, Serialize, derive_builder::Builder)]
#[derive(
Clone, Deserialize, Serialize, derive_more::FromStr, derive_more::Debug, derive_more::Display,
)]
#[debug(r#""{_0}""#)]
#[display("{_0}")]
pub struct Mailbox(pub lettre::message::Mailbox);
#[derive(Clone, derive_more::Debug, Deserialize, Serialize, derive_builder::Builder)]
#[builder(pattern = "owned")]
pub struct MailOptions {
#[builder(default = "false")]
@@ -43,6 +49,8 @@ pub struct MailOptions {
#[builder(default = "SmtpEncryption::Tls")]
pub smtp_encryption: SmtpEncryption,
/// Deprecated.
#[debug(skip)]
#[serde(skip)]
#[builder(default = "None")]
pub tls_required: Option<bool>,
}
@@ -72,7 +80,11 @@ impl std::default::Default for LdapsOptions {
}
}
#[derive(Clone, Debug, Deserialize, Serialize, derive_builder::Builder)]
#[derive(Clone, Deserialize, Serialize, derive_more::Debug)]
#[debug(r#""{_0}""#)]
pub struct HttpUrl(pub Url);
#[derive(Clone, Deserialize, Serialize, derive_builder::Builder, derive_more::Debug)]
#[builder(pattern = "owned", build_fn(name = "private_build"))]
pub struct Configuration {
#[builder(default = r#"String::from("0.0.0.0")"#)]
@@ -115,8 +127,9 @@ pub struct Configuration {
pub smtp_options: MailOptions,
#[builder(default)]
pub ldaps_options: LdapsOptions,
#[builder(default = r#"Url::parse("http://localhost").unwrap()"#)]
pub http_url: Url,
#[builder(default = r#"HttpUrl(Url::parse("http://localhost").unwrap())"#)]
pub http_url: HttpUrl,
#[debug(skip)]
#[serde(skip)]
#[builder(field(private), default = "None")]
server_setup: Option<ServerSetupConfig>,
@@ -419,7 +432,7 @@ impl ConfigOverrider for RunOpts {
}
if let Some(url) = self.http_url.as_ref() {
config.http_url = url.clone();
config.http_url = HttpUrl(url.clone());
}
if let Some(database_url) = self.database_url.as_ref() {
@@ -473,10 +486,10 @@ impl ConfigOverrider for GeneralConfigOpts {
impl ConfigOverrider for SmtpOpts {
fn override_config(&self, config: &mut Configuration) {
if let Some(from) = &self.smtp_from {
config.smtp_options.from = Some(from.clone());
config.smtp_options.from = Some(Mailbox(from.clone()));
}
if let Some(reply_to) = &self.smtp_reply_to {
config.smtp_options.reply_to = Some(reply_to.clone());
config.smtp_options.reply_to = Some(Mailbox(reply_to.clone()));
}
if let Some(server) = &self.smtp_server {
config.smtp_options.server.clone_from(server);

View File

@@ -1,7 +1,8 @@
use serde::{Deserialize, Serialize};
use url::Url;
#[derive(Clone, Serialize, Deserialize)]
#[derive(Clone, Serialize, Deserialize, derive_more::Display)]
#[display("{_0}")]
pub struct DatabaseUrl(Url);
impl From<Url> for DatabaseUrl {
@@ -22,19 +23,13 @@ impl std::fmt::Debug for DatabaseUrl {
let mut url = self.0.clone();
// It can fail for URLs that cannot have a password, like "mailto:bob@example".
let _ = url.set_password(Some("***PASSWORD***"));
f.write_fmt(format_args!("{}", url))
f.write_fmt(format_args!(r#""{}""#, url))
} else {
f.write_fmt(format_args!("{}", self.0))
f.write_fmt(format_args!(r#""{}""#, self.0))
}
}
}
impl std::fmt::Display for DatabaseUrl {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!("{}", self.0))
}
}
#[cfg(test)]
mod tests {
use super::*;
@@ -44,7 +39,7 @@ mod tests {
let url = DatabaseUrl::from("postgres://user:pass@localhost:5432/dbname");
assert_eq!(
format!("{:?}", url),
"postgres://user:***PASSWORD***@localhost:5432/dbname"
r#""postgres://user:***PASSWORD***@localhost:5432/dbname""#
);
assert_eq!(
url.to_string(),

View File

@@ -33,8 +33,8 @@ async fn send_email(
),
server_url.domain().unwrap_or_default()
)))
.from(from)
.reply_to(reply_to)
.from(from.0)
.reply_to(reply_to.0)
.to(to)
.subject(subject)
.singlepart(

View File

@@ -194,7 +194,7 @@ where
.get_jwt_blacklist()
.await
.context("while getting the jwt blacklist")?;
let server_url = config.http_url.clone();
let server_url = config.http_url.0.clone();
let mail_options = config.smtp_options.clone();
let verbose = config.verbose;
info!("Starting the API/web server on port {}", config.http_port);