From 65e2103365bfc1867a6f9c5630eb7f36608c5fdd Mon Sep 17 00:00:00 2001 From: Valentin Tolmer Date: Sat, 7 Sep 2024 23:50:08 +0200 Subject: [PATCH] server: Simplify the debug print of various structs And use derive_more more liberally to simplify the impls --- Cargo.lock | 56 ++++++++++++++++++++--------- auth/Cargo.toml | 5 +++ auth/src/lib.rs | 19 ++++++---- server/Cargo.toml | 6 +++- server/src/domain/types.rs | 42 ++++++++++++++-------- server/src/infra/configuration.rs | 29 ++++++++++----- server/src/infra/database_string.rs | 15 +++----- server/src/infra/mail.rs | 4 +-- server/src/infra/tcp_server.rs | 2 +- 9 files changed, 118 insertions(+), 60 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 69e3838..ed1e10c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -56,7 +56,7 @@ dependencies = [ "askama_escape", "bitflags 1.3.2", "bytes", - "derive_more", + "derive_more 0.99.17", "futures-core", "http-range", "log", @@ -82,7 +82,7 @@ dependencies = [ "brotli", "bytes", "bytestring", - "derive_more", + "derive_more 0.99.17", "encoding_rs", "flate2", "futures-core", @@ -216,7 +216,7 @@ dependencies = [ "bytestring", "cfg-if", "cookie", - "derive_more", + "derive_more 0.99.17", "encoding_rs", "futures-core", "futures-util", @@ -1154,6 +1154,27 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "derive_more" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a9b99b9cbbe49445b21764dc0625032a89b145a2642e67603e1c936f5458d05" +dependencies = [ + "derive_more-impl", +] + +[[package]] +name = "derive_more-impl" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb7330aeadfbe296029522e6c40f315320aba36fc43a5b3632f3795348f3bd22" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.77", + "unicode-xid", +] + [[package]] name = "derive_utils" version = "0.11.2" @@ -2173,7 +2194,7 @@ checksum = "ce243b1bfa62ffc028f1cc3b6034ec63d649f3031bc8a4fbbb004e1ac17d1f68" dependencies = [ "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.77", ] [[package]] @@ -2484,7 +2505,7 @@ dependencies = [ "clap", "cron", "derive_builder", - "derive_more", + "derive_more 1.0.0", "figment", "figment_file_provider_adapter", "futures", @@ -2575,6 +2596,7 @@ version = "0.4.0" dependencies = [ "chrono", "curve25519-dalek", + "derive_more 1.0.0", "digest 0.9.0", "generic-array", "getrandom 0.2.8", @@ -2994,7 +3016,7 @@ dependencies = [ "proc-macro-error", "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.77", ] [[package]] @@ -3234,9 +3256,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.66" +version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" dependencies = [ "unicode-ident", ] @@ -3256,9 +3278,9 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.33" +version = "1.0.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" dependencies = [ "proc-macro2", ] @@ -3662,7 +3684,7 @@ dependencies = [ "proc-macro-error", "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.77", ] [[package]] @@ -3699,7 +3721,7 @@ dependencies = [ "proc-macro2", "quote", "sea-bae", - "syn 2.0.32", + "syn 2.0.77", "unicode-ident", ] @@ -3805,7 +3827,7 @@ checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3" dependencies = [ "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.77", ] [[package]] @@ -3853,7 +3875,7 @@ checksum = "91d129178576168c589c9ec973feedf7d3126c01ac2bf08795109aa35b69fb8f" dependencies = [ "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.77", ] [[package]] @@ -4265,7 +4287,7 @@ dependencies = [ "proc-macro2", "quote", "rustversion", - "syn 2.0.32", + "syn 2.0.77", ] [[package]] @@ -4287,9 +4309,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.32" +version = "2.0.77" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "239814284fd6f1a4ffe4ca893952cdd93c224b6a1571c9a9eadd670295c0c9e2" +checksum = "9f35bcdf61fd8e7be6caf75f429fdca8beb3ed76584befb503b1569faee373ed" dependencies = [ "proc-macro2", "quote", diff --git a/auth/Cargo.toml b/auth/Cargo.toml index 1f8cc10..6d11fd1 100644 --- a/auth/Cargo.toml +++ b/auth/Cargo.toml @@ -25,6 +25,11 @@ serde = "*" sha2 = "0.9" thiserror = "*" +[dependencies.derive_more] +features = ["debug", "display"] +default-features = false +version = "1" + [dependencies.opaque-ke] version = "0.6" diff --git a/auth/src/lib.rs b/auth/src/lib.rs index 411d6bd..de2f83c 100644 --- a/auth/src/lib.rs +++ b/auth/src/lib.rs @@ -151,10 +151,22 @@ pub mod types { } #[derive( - PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Default, Hash, Serialize, Deserialize, + PartialEq, + Eq, + PartialOrd, + Ord, + Clone, + Default, + Hash, + Serialize, + Deserialize, + derive_more::Debug, + derive_more::Display, )] #[cfg_attr(feature = "sea_orm", derive(DeriveValueType))] #[serde(from = "CaseInsensitiveString")] + #[debug(r#""{}""#, _0.as_str())] + #[display("{}", _0.as_str())] pub struct UserId(CaseInsensitiveString); impl UserId { @@ -176,11 +188,6 @@ pub mod types { Self(s.into()) } } - impl std::fmt::Display for UserId { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "{}", self.0.as_str()) - } - } #[cfg(feature = "sea_orm")] impl From<&UserId> for Value { diff --git a/server/Cargo.toml b/server/Cargo.toml index 6a4c98b..f0ecdb7 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -25,7 +25,6 @@ base64 = "0.21" bincode = "1.3" cron = "*" derive_builder = "0.12" -derive_more = "0.99" figment_file_provider_adapter = "0.1" futures = "*" futures-util = "*" @@ -64,6 +63,11 @@ version = "*" features = ["std", "color", "suggestions", "derive", "env"] version = "4" +[dependencies.derive_more] +features = ["debug", "display", "from", "from_str"] +default-features = false +version = "1" + [dependencies.figment] features = ["env", "toml"] version = "*" diff --git a/server/src/domain/types.rs b/server/src/domain/types.rs index a2f2bdf..48e1be3 100644 --- a/server/src/domain/types.rs +++ b/server/src/domain/types.rs @@ -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 { diff --git a/server/src/infra/configuration.rs b/server/src/infra/configuration.rs index 2cc7a16..3cb8b00 100644 --- a/server/src/infra/configuration.rs +++ b/server/src/infra/configuration.rs @@ -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, } @@ -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, @@ -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); diff --git a/server/src/infra/database_string.rs b/server/src/infra/database_string.rs index 5278e79..12d1b8a 100644 --- a/server/src/infra/database_string.rs +++ b/server/src/infra/database_string.rs @@ -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 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(), diff --git a/server/src/infra/mail.rs b/server/src/infra/mail.rs index 7ed6c2a..e793d71 100644 --- a/server/src/infra/mail.rs +++ b/server/src/infra/mail.rs @@ -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( diff --git a/server/src/infra/tcp_server.rs b/server/src/infra/tcp_server.rs index fa5f11f..a07ff5d 100644 --- a/server/src/infra/tcp_server.rs +++ b/server/src/infra/tcp_server.rs @@ -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);