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 std::str::FromStr;
use crate::errors::ConversionError; use super::ConfigError;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum Environment { pub enum Environment {
@ -16,13 +16,13 @@ impl Default for Environment {
} }
impl FromStr for Environment { impl FromStr for Environment {
type Err = ConversionError; type Err = ConfigError;
fn from_str(val: &str) -> Result<Self, Self::Err> { fn from_str(val: &str) -> Result<Self, Self::Err> {
let environment = match val { let environment = match val {
"development" => Environment::Development, "development" => Environment::Development,
"production" => Environment::Production, "production" => Environment::Production,
_ => return Err(ConversionError), _ => return Err(ConfigError("invalid environment type")),
}; };
Ok(environment) Ok(environment)
} }

View file

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

View file

@ -11,7 +11,6 @@ use url::Url;
use crate::activitypub::constants::ACTOR_KEY_SUFFIX; use crate::activitypub::constants::ACTOR_KEY_SUFFIX;
use crate::activitypub::identifiers::local_instance_actor_id; use crate::activitypub::identifiers::local_instance_actor_id;
use crate::errors::ConversionError;
use crate::utils::urls::guess_protocol; use crate::utils::urls::guess_protocol;
use super::blockchain::BlockchainConfig; use super::blockchain::BlockchainConfig;
@ -123,14 +122,14 @@ pub struct Config {
} }
impl 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 { let scheme = match self.environment {
Environment::Development => "http", Environment::Development => "http",
Environment::Production => guess_protocol(&self.instance_uri), Environment::Production => guess_protocol(&self.instance_uri),
}; };
let url_str = format!("{}://{}", scheme, self.instance_uri); let url_str = format!("{}://{}", scheme, self.instance_uri);
let url = Url::parse(&url_str).map_err(|_| ConversionError)?; let url = Url::parse(&url_str)?;
url.host().ok_or(ConversionError)?; // validates URL url.host().ok_or(url::ParseError::EmptyHost)?; // validates URL
Ok(url) Ok(url)
} }

View file

@ -15,3 +15,7 @@ pub use loader::parse_config;
pub use main::{Config, Instance, RegistrationType}; pub use main::{Config, Instance, RegistrationType};
pub const MITRA_VERSION: &str = env!("CARGO_PKG_VERSION"); pub const MITRA_VERSION: &str = env!("CARGO_PKG_VERSION");
#[derive(thiserror::Error, Debug)]
#[error("{0}")]
pub struct ConfigError(&'static str);