move config loading to config module

This separates concerns a bit more. We will probably want to extend the
logic for config loading in the future, and that stuff should all live
in the relevant place. This change points us in the right direction.
This commit is contained in:
Charles Hall 2024-06-06 22:44:30 -07:00
parent 2b0bc140cf
commit 6b819d6f2d
No known key found for this signature in database
GPG key ID: 7B8E0645816E07CF
3 changed files with 40 additions and 13 deletions

View file

@ -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<P>(path: P) -> Result<Config, error::Config>
where
P: AsRef<Path>,
{
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()))
}

View file

@ -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),
}

View file

@ -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);