Replace ConversionError with ConfigError in config module

This commit is contained in:
silverpill 2023-02-05 21:30:51 +00:00
parent 831739d215
commit 5d4cfab00a
4 changed files with 11 additions and 11 deletions

View file

@ -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<Self, Self::Err> {
let environment = match val {
"development" => Environment::Development,
"production" => Environment::Production,
_ => return Err(ConversionError),
_ => return Err(ConfigError("invalid environment type")),
};
Ok(environment)
}

View file

@ -4,13 +4,10 @@ use serde::{
Deserializer,
de::{Error as DeserializerError},
};
use super::ConfigError;
const FILE_SIZE_RE: &str = r#"^(?i)(?P<size>\d+)(?P<unit>[kmg]?)b?$"#;
#[derive(thiserror::Error, Debug)]
#[error("{0}")]
struct ConfigError(&'static str);
fn parse_file_size(value: &str) -> Result<usize, ConfigError> {
let file_size_re = Regex::new(FILE_SIZE_RE)
.expect("regexp should be valid");

View file

@ -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<Url, ConversionError> {
pub(super) fn try_instance_url(&self) -> Result<Url, url::ParseError> {
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)
}

View file

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