diff --git a/src/config.rs b/src/config.rs index 990bb865..04e723f4 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,8 +1,13 @@ -use std::net::{IpAddr, Ipv4Addr}; +use std::{ + net::{IpAddr, Ipv4Addr}, + path::Path, +}; use ruma::{OwnedServerName, RoomVersionId}; use serde::Deserialize; +use crate::error; + mod proxy; use proxy::ProxyConfig; @@ -153,3 +158,20 @@ fn default_turn_ttl() -> u64 { pub(crate) fn default_default_room_version() -> RoomVersionId { RoomVersionId::V10 } + +/// Load the configuration from the given path +pub(crate) async fn load

(path: P) -> Result +where + P: AsRef, +{ + use error::Config as Error; + + let path = path.as_ref(); + + toml::from_str( + &tokio::fs::read_to_string(path) + .await + .map_err(|e| Error::Read(e, path.to_owned()))?, + ) + .map_err(|e| Error::Parse(e, path.to_owned())) +} diff --git a/src/error.rs b/src/error.rs index f6286c0e..94d9a847 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,6 +1,6 @@ //! Error handling facilities -use std::{fmt, iter}; +use std::{fmt, iter, path::PathBuf}; use thiserror::Error; @@ -38,11 +38,8 @@ impl fmt::Display for DisplayWithSources<'_> { #[allow(missing_docs)] #[derive(Error, Debug)] pub(crate) enum Main { - #[error("failed to read configuration file")] - ConfigRead(#[source] std::io::Error), - - #[error("failed to parse configuration")] - ConfigParse(#[from] toml::de::Error), + #[error("failed to load configuration")] + Config(#[from] Config), #[error("failed to initialize observability")] Observability(#[from] Observability), @@ -74,3 +71,16 @@ pub(crate) enum Observability { #[error("tracing_flame error")] TracingFlame(#[from] tracing_flame::Error), } + +/// Configuration errors +// Missing docs are allowed here since that kind of information should be +// encoded in the error messages themselves anyway. +#[allow(missing_docs)] +#[derive(Error, Debug)] +pub(crate) enum Config { + #[error("failed to read configuration file {1:?}")] + Read(#[source] std::io::Error, PathBuf), + + #[error("failed to parse configuration file {1:?}")] + Parse(#[source] toml::de::Error, PathBuf), +} diff --git a/src/main.rs b/src/main.rs index e71430c4..ba1f9e29 100644 --- a/src/main.rs +++ b/src/main.rs @@ -106,12 +106,7 @@ async fn try_main() -> Result<(), error::Main> { let args = args::parse(); - let config = toml::from_str( - &tokio::fs::read_to_string(&args.config) - .await - .map_err(Error::ConfigRead)?, - ) - .map_err(Error::ConfigParse)?; + let config = config::load(&args.config).await?; let _guard = observability::init(&config);