From 07e343541d0e60e34e582d43efd8cf573f8bb344 Mon Sep 17 00:00:00 2001 From: Brad Clawsie Date: Wed, 25 Nov 2026 13:31:44 -0600 Subject: feat: learn_21 commit 1aef59339ed32f05560fb2410f3f1db919b68b08 Author: Brad Clawsie Date: Wed Nov 25 13:30:40 2026 -0600 feat: learn_21 commit a1f45dffa085500ae070a023f31f05ed6bec2c3c Author: Brad Clawsie Date: Wed Nov 25 11:02:28 2026 -0600 wip: learn_21 commit 927abcffa154fe6e1ea2658e9d55d14f49f101ae Author: Brad Clawsie Date: Wed Nov 25 10:15:35 2026 -0600 wip: learn_21 commit 53396b0796762119b51d0b74ed821dece20c1ff6 Author: Brad Clawsie Date: Tue Nov 24 15:31:04 2026 -0600 wip: learn_21 --- learn_21/src/config.rs | 100 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 learn_21/src/config.rs (limited to 'learn_21/src/config.rs') diff --git a/learn_21/src/config.rs b/learn_21/src/config.rs new file mode 100644 index 0000000..cb73caa --- /dev/null +++ b/learn_21/src/config.rs @@ -0,0 +1,100 @@ +use std::fs::read_to_string; +use std::num::NonZeroUsize; +use std::num::NonZeroU16; +use crate::error::ConfigError; + +#[derive(Debug, PartialEq, Eq)] +pub struct Config { + pub host: String, + pub port: u16, + pub workers: usize, +} + +pub fn parse_config(input: &str) -> Result { + let mut host: Option = None; + let mut port: Option = None; + let mut workers: Option = None; + let mut i: usize = 0; + let valid_keys = ["host", "port", "workers"]; + + for line in input.lines() { + i += 1; + let trimmed = line.trim(); + + // skip + if trimmed.is_empty() || trimmed.starts_with('#') { + continue; + } + + let (key, val) = trimmed.split_once('=').ok_or_else(|| ConfigError::InvalidLine(i))?; + + // one of the components is empty + if key.is_empty() || val.is_empty() { + return Err(ConfigError::InvalidLine(i)); + } + + // bad key + if !valid_keys.contains(&key) { + return Err(ConfigError::UnknownField(key.to_string())) + } + + if key == "host" { + // already set + if host.is_some() { + return Err(ConfigError::DuplicateField("host".to_string())); + } + + // hostnames can be nonempty a-zA-Z0-9 and hyphen + if val.is_empty() || + !val.chars().all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '=') { + return Err(ConfigError::InvalidValue(val.to_string())); + } + + host = Some(val.to_string()); + } + + if key == "port" { + // arlready set + if port.is_some() { + return Err(ConfigError::DuplicateField("port".to_string())); + } + + port = Some(val.parse(). + map_err(|_| ConfigError::InvalidValue(val.to_string()))?); + } + + if key == "workers" { + // already set + if workers.is_some() { + return Err(ConfigError::DuplicateField("workers".to_string())); + } + + workers = Some(val.parse(). + map_err(|_| ConfigError::InvalidValue(val.to_string()))?); + } + } + + if host.is_none() { + return Err(ConfigError::MissingField("host".to_string())); + } + + if port.is_none() { + return Err(ConfigError::MissingField("port".to_string())); + } + + if workers.is_none() { + return Err(ConfigError::MissingField("workers".to_string())); + } + + Ok(Config{ + host: host.unwrap(), + port: port.unwrap().get(), + workers: workers.unwrap().get(), + }) +} + +pub fn load_config(path: &std::path::Path) -> Result { + let contents: String = read_to_string(path).map_err(ConfigError::Io)?; + parse_config(&contents) +} + -- cgit v1.2.3