refactor: cleaned up code

This commit is contained in:
Allinen authored and Ellis Git committed 2026-09-16 17:57:11 +00:00
1 parent 7026f62ff8
commit d52d5003b1
4 files changed
+36 -29

No files matched your search

+5 -1
View File
@@ -2174,7 +2174,11 @@
#connection_uri =
# Same as `connection_uri`, except loaded from a file. If `connection_uri`
# is defined, it is prioritized and `connection_uri_file` is ignored
# is defined, it is prioritized and `connection_uri_file` is ignored.
#
# The file will be read once, when Continuwuity starts. It is not
# currently reread when the server configuration is reloaded. If the file
# cannot be read, Continuwuity will fail to start.
#
#connection_uri_file =
+6 -6
View File
@@ -1,4 +1,4 @@
use std::{env::consts::OS, path::PathBuf};
use std::env::consts::OS;
use either::Either;
use figment::Figment;
@@ -264,14 +264,14 @@ pub fn check(config: &Config) -> Result {
}
if config.smtp.as_ref().is_some_and(|f| {
f.connection_uri.is_none() && f.connection_uri_file.is_none()
|| (f.connection_uri == Some(String::new())
&& f.connection_uri_file == Some(PathBuf::new()))
f.connection_uri.as_ref().is_none_or(String::is_empty)
&& f.connection_uri_file
.as_ref()
.is_none_or(|f| f.as_os_str().is_empty())
}) {
return Err!(Config(
"connection_uri",
"If smtp is enabled, either connection_uri or connection_uri_file has to be \
defined."
"If smtp is enabled, either connection_uri or connection_uri_file has to be defined."
));
}
+5 -1
View File
@@ -2592,7 +2592,11 @@ pub struct SmtpConfig {
pub connection_uri: Option<String>,
/// Same as `connection_uri`, except loaded from a file. If `connection_uri`
/// is defined, it is prioritized and `connection_uri_file` is ignored
/// is defined, it is prioritized and `connection_uri_file` is ignored.
///
/// The file will be read once, when Continuwuity starts. It is not
/// currently reread when the server configuration is reloaded. If the file
/// cannot be read, Continuwuity will fail to start.
pub connection_uri_file: Option<PathBuf>,
/// The outgoing address which will be used for sending emails.
+20 -21
View File
@@ -20,28 +20,27 @@ pub struct Service {
#[async_trait::async_trait]
impl crate::Service for Service {
fn build(args: Args<'_>) -> Result<Arc<Self>> {
let transport =
args.server
.config
.smtp
.as_ref()
.map(|config| -> Result<_> {
let connection_uri: String =
if let Some(uri) = &config.connection_uri {
uri.clone()
} else {
std::fs::read_to_string(
config.connection_uri_file.as_ref().unwrap() // Safe to unwrap here assuming the config checks properly run and throw errors
)
.map_err(|err| err!("Failed to read SMTP connection URI file: {err}"))?
};
let transport = Transport::from_url(&connection_uri)
.map_err(|err| err!("Failed to set up SMTP transport: {err}"))?
.build();
let transport = args
.server
.config
.smtp
.as_ref()
.map(|config| -> Result<_> {
let connection_uri = match (&config.connection_uri, &config.connection_uri_file) {
| (Some(uri), _) => uri.clone(),
| (None, Some(path)) => std::fs::read_to_string(path)
.map_err(|err| err!("Failed to read SMTP connection URI file: {err}"))?,
| (None, None) => unreachable!(
"Config validation should guarantee one SMTP connection source."
),
};
let transport = Transport::from_url(&connection_uri)
.map_err(|err| err!("Failed to set up SMTP transport: {err}"))?
.build();
Ok((config.sender.clone(), transport))
})
.transpose()?;
Ok((config.sender.clone(), transport))
})
.transpose()?;
Ok(Arc::new(Self { transport }))
}