From 8404f38182fd8aa600107ee6ba32596f3e43196a Mon Sep 17 00:00:00 2001 From: Jacob Kiers Date: Wed, 4 Oct 2023 23:08:07 +0200 Subject: [PATCH] Move ProxyToUpstream parsing to TryFrom trait This seems cleaner to me than parsing it externally. Signed-off-by: Jacob Kiers --- src/config.rs | 103 ++++++++++++++++++++++++++------------------------ 1 file changed, 53 insertions(+), 50 deletions(-) diff --git a/src/config.rs b/src/config.rs index cd6a69f..7e162d9 100644 --- a/src/config.rs +++ b/src/config.rs @@ -56,7 +56,6 @@ impl Clone for Addr { #[derive(Debug, Clone, Deserialize, Default)] pub struct ProxyToUpstream { - pub name: String, pub addr: String, pub protocol: String, #[serde(skip_deserializing)] @@ -70,46 +69,10 @@ impl ProxyToUpstream { } } -#[derive(Debug)] -pub enum ConfigError { - IO(IOError), - Yaml(serde_yaml::Error), - Custom(String), -} +impl TryFrom<&str> for ProxyToUpstream { + type Error = ConfigError; -impl Config { - pub fn new(path: &str) -> Result { - let base = (load_config(path))?; - - Ok(Config { base }) - } -} - -fn load_config(path: &str) -> Result { - let mut contents = String::new(); - let mut file = (File::open(path))?; - (file.read_to_string(&mut contents))?; - - let base: BaseConfig = serde_yaml::from_str(&contents)?; - - if base.version != 1 { - return Err(ConfigError::Custom( - "Unsupported config version".to_string(), - )); - } - - let log_level = base.log.clone().unwrap_or_else(|| "info".to_string()); - if !log_level.eq("disable") { - std::env::set_var("FOURTH_LOG", log_level.clone()); - pretty_env_logger::init_custom_env("FOURTH_LOG"); - debug!("Set log level to {}", log_level); - } - - debug!("Config version {}", base.version); - - let mut parsed_upstream: HashMap = HashMap::new(); - - for (name, upstream) in base.upstream.iter() { + fn try_from(upstream: &str) -> Result { let upstream_url = match Url::parse(upstream) { Ok(url) => url, Err(_) => { @@ -151,21 +114,61 @@ fn load_config(path: &str) -> Result { } let addr = UpstreamAddress::new(format!("{}:{}", upstream_host, upstream_port)); - parsed_upstream.insert( - name.to_string(), - Upstream::Proxy(ProxyToUpstream { - name: name.to_string(), - addr: format!("{}:{}", upstream_host, upstream_port), - protocol: upstream_url.scheme().to_string(), - addresses: Addr(Mutex::new(addr)), - }), - ); + Ok(ProxyToUpstream { + addr: format!("{}:{}", upstream_host, upstream_port), + protocol: upstream_url.scheme().to_string(), + addresses: Addr(Mutex::new(addr)), + }) + } +} + +#[derive(Debug)] +pub enum ConfigError { + IO(IOError), + Yaml(serde_yaml::Error), + Custom(String), +} + +impl Config { + pub fn new(path: &str) -> Result { + let base = load_config(path)?; + + Ok(Config { base }) + } +} + +fn load_config(path: &str) -> Result { + let mut contents = String::new(); + let mut file = File::open(path)?; + file.read_to_string(&mut contents)?; + + let base: BaseConfig = serde_yaml::from_str(&contents)?; + + if base.version != 1 { + return Err(ConfigError::Custom( + "Unsupported config version".to_string(), + )); } - parsed_upstream.insert("ban".to_string(), Upstream::Ban); + let log_level = base.log.clone().unwrap_or_else(|| "info".to_string()); + if !log_level.eq("disable") { + std::env::set_var("FOURTH_LOG", log_level.clone()); + pretty_env_logger::init_custom_env("FOURTH_LOG"); + debug!("Set log level to {}", log_level); + } + debug!("Config version {}", base.version); + + let mut parsed_upstream: HashMap = HashMap::new(); + + parsed_upstream.insert("ban".to_string(), Upstream::Ban); parsed_upstream.insert("echo".to_string(), Upstream::Echo); + for (name, upstream) in base.upstream.iter() { + let ups = ProxyToUpstream::try_from(upstream.as_str())?; + parsed_upstream.insert(name.to_string(), Upstream::Proxy(ups)); + } + let parsed = ParsedConfig { version: base.version, log: base.log,