diff --git a/src/config/environment.rs b/src/config/environment.rs index 6879eae..aa85ea6 100644 --- a/src/config/environment.rs +++ b/src/config/environment.rs @@ -1,6 +1,6 @@ use std::str::FromStr; -use crate::errors::ConversionError; +use super::ConfigError; #[derive(Clone, Debug)] pub enum Environment { @@ -16,13 +16,13 @@ impl Default for Environment { } impl FromStr for Environment { - type Err = ConversionError; + type Err = ConfigError; fn from_str(val: &str) -> Result { let environment = match val { "development" => Environment::Development, "production" => Environment::Production, - _ => return Err(ConversionError), + _ => return Err(ConfigError("invalid environment type")), }; Ok(environment) } diff --git a/src/config/limits.rs b/src/config/limits.rs index df3f2db..2fcfe5e 100644 --- a/src/config/limits.rs +++ b/src/config/limits.rs @@ -4,13 +4,10 @@ use serde::{ Deserializer, de::{Error as DeserializerError}, }; +use super::ConfigError; const FILE_SIZE_RE: &str = r#"^(?i)(?P\d+)(?P[kmg]?)b?$"#; -#[derive(thiserror::Error, Debug)] -#[error("{0}")] -struct ConfigError(&'static str); - fn parse_file_size(value: &str) -> Result { let file_size_re = Regex::new(FILE_SIZE_RE) .expect("regexp should be valid"); diff --git a/src/config/main.rs b/src/config/main.rs index 1e36bb9..2ce0761 100644 --- a/src/config/main.rs +++ b/src/config/main.rs @@ -11,7 +11,6 @@ use url::Url; use crate::activitypub::constants::ACTOR_KEY_SUFFIX; use crate::activitypub::identifiers::local_instance_actor_id; -use crate::errors::ConversionError; use crate::utils::urls::guess_protocol; use super::blockchain::BlockchainConfig; @@ -123,14 +122,14 @@ pub struct Config { } impl Config { - pub(super) fn try_instance_url(&self) -> Result { + pub(super) fn try_instance_url(&self) -> Result { let scheme = match self.environment { Environment::Development => "http", Environment::Production => guess_protocol(&self.instance_uri), }; let url_str = format!("{}://{}", scheme, self.instance_uri); - let url = Url::parse(&url_str).map_err(|_| ConversionError)?; - url.host().ok_or(ConversionError)?; // validates URL + let url = Url::parse(&url_str)?; + url.host().ok_or(url::ParseError::EmptyHost)?; // validates URL Ok(url) } diff --git a/src/config/mod.rs b/src/config/mod.rs index fcec66b..f35b5c1 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -15,3 +15,7 @@ pub use loader::parse_config; pub use main::{Config, Instance, RegistrationType}; pub const MITRA_VERSION: &str = env!("CARGO_PKG_VERSION"); + +#[derive(thiserror::Error, Debug)] +#[error("{0}")] +pub struct ConfigError(&'static str);