diff options
| author | Brad Clawsie <brad@b7j0c.org> | 2026-11-25 13:31:44 -0600 |
|---|---|---|
| committer | Brad Clawsie <brad@b7j0c.org> | 2026-11-25 13:31:44 -0600 |
| commit | 07e343541d0e60e34e582d43efd8cf573f8bb344 (patch) | |
| tree | c13f9aaaa30b45581126c0c90c6c2a022029dfc4 | |
| parent | f091cb475a1e53f84b9876cd45e11b531f660237 (diff) | |
feat: learn_21
commit 1aef59339ed32f05560fb2410f3f1db919b68b08
Author: Brad Clawsie <brad@b7j0c.org>
Date: Wed Nov 25 13:30:40 2026 -0600
feat: learn_21
commit a1f45dffa085500ae070a023f31f05ed6bec2c3c
Author: Brad Clawsie <brad@b7j0c.org>
Date: Wed Nov 25 11:02:28 2026 -0600
wip: learn_21
commit 927abcffa154fe6e1ea2658e9d55d14f49f101ae
Author: Brad Clawsie <brad@b7j0c.org>
Date: Wed Nov 25 10:15:35 2026 -0600
wip: learn_21
commit 53396b0796762119b51d0b74ed821dece20c1ff6
Author: Brad Clawsie <brad@b7j0c.org>
Date: Tue Nov 24 15:31:04 2026 -0600
wip: learn_21
| -rw-r--r-- | learn_21/Cargo.lock | 7 | ||||
| -rw-r--r-- | learn_21/Cargo.toml | 6 | ||||
| -rw-r--r-- | learn_21/src/config.rs | 100 | ||||
| -rw-r--r-- | learn_21/src/error.rs | 9 | ||||
| -rw-r--r-- | learn_21/src/lib.rs | 2 | ||||
| -rw-r--r-- | learn_21/src/main.rs | 3 | ||||
| -rw-r--r-- | learn_21/tests/load_config.rs | 24 | ||||
| -rw-r--r-- | learn_21/tests/parse_config.rs | 134 |
8 files changed, 285 insertions, 0 deletions
diff --git a/learn_21/Cargo.lock b/learn_21/Cargo.lock new file mode 100644 index 0000000..8a45a7a --- /dev/null +++ b/learn_21/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "learn_21" +version = "0.1.0" diff --git a/learn_21/Cargo.toml b/learn_21/Cargo.toml new file mode 100644 index 0000000..9ca2c19 --- /dev/null +++ b/learn_21/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "learn_21" +version = "0.1.0" +edition = "2024" + +[dependencies] 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<Config, ConfigError> { + let mut host: Option<String> = None; + let mut port: Option<NonZeroU16> = None; + let mut workers: Option<NonZeroUsize> = 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<Config, ConfigError> { + let contents: String = read_to_string(path).map_err(ConfigError::Io)?; + parse_config(&contents) +} + diff --git a/learn_21/src/error.rs b/learn_21/src/error.rs new file mode 100644 index 0000000..34dfeb9 --- /dev/null +++ b/learn_21/src/error.rs @@ -0,0 +1,9 @@ +#[derive(Debug)] +pub enum ConfigError { + Io(std::io::Error), + MissingField(String), + InvalidLine(usize), + DuplicateField(String), + InvalidValue(String), + UnknownField(String), +} diff --git a/learn_21/src/lib.rs b/learn_21/src/lib.rs new file mode 100644 index 0000000..7404805 --- /dev/null +++ b/learn_21/src/lib.rs @@ -0,0 +1,2 @@ +pub mod config; +pub mod error; diff --git a/learn_21/src/main.rs b/learn_21/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/learn_21/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/learn_21/tests/load_config.rs b/learn_21/tests/load_config.rs new file mode 100644 index 0000000..827ed02 --- /dev/null +++ b/learn_21/tests/load_config.rs @@ -0,0 +1,24 @@ +use std::env; +use std::fs::{self, File}; +use std::io::{self, Write}; +use std::process; + +use learn_21::config::{Config, load_config, parse_config}; +use learn_21::error::ConfigError; + +#[test] +fn test_load_config() -> io::Result<()> { + let mut file_path = env::temp_dir(); + file_path.push(format!("test_{}.txt", process::id())); + + { + let mut file = File::create(&file_path)?; + file.write_all(b"host=myhost\nport=1\nworkers=1\n")?; + } + + assert!(load_config(&file_path).is_ok()); + assert!(load_config(std::path::Path::new("/bad/path")).is_err()); + + fs::remove_file(&file_path)?; + Ok(()) +} diff --git a/learn_21/tests/parse_config.rs b/learn_21/tests/parse_config.rs new file mode 100644 index 0000000..8ca6626 --- /dev/null +++ b/learn_21/tests/parse_config.rs @@ -0,0 +1,134 @@ +use learn_21::config::{Config, parse_config}; +use learn_21::error::ConfigError; + +#[test] +fn test_ok() { + let ok0 = "\n# comment\n host=my-host\nport=1 \n workers=1 \n"; + assert!(parse_config(ok0).is_ok_and(|x| x == + Config{host:"my-host".to_string(), port:1, workers:1})); + + let ok1 = format!("host=myhost-012-\nport=65535 \n workers={} \n", usize::MAX); + assert!(parse_config(&ok1).is_ok_and(|x| x == + Config{host:"myhost-012-".to_string(), port:65535, workers:usize::MAX})); + + let ok2 = "port=1\n# comment\n host=my-host \n\n workers=1 \n"; + assert!(parse_config(ok2).is_ok_and(|x| x == + Config{host:"my-host".to_string(), port:1, workers:1})); + + let ok3 = "port=1\n# comment\n host=my=host\n\n workers=1 \n"; + assert!(parse_config(ok3).is_ok_and(|x| x == + Config{host:"my=host".to_string(), port:1, workers:1})); + +} + +#[test] +fn test_invalid_line() { + let invalid0 = "host=hello\nport\nworkers=1\n"; + dbg!(parse_config(invalid0)); + assert!(matches!( + parse_config(invalid0), + Err(ConfigError::InvalidLine(2)) + )); +} + +#[test] +fn test_unknown_field() { + let invalid0 = "host=hello\n__port=1\nworkers=1\n"; + assert!(matches!( + parse_config(invalid0), + Err(ConfigError::UnknownField(s)) if s == "__port" + )); + + let invalid1 = "host=hello\nport__=1\n"; + assert!(matches!( + parse_config(invalid1), + Err(ConfigError::UnknownField(s)) if s == "port__" + )); + } + +#[test] +fn test_duplicate_field() { + let invalid0 = "host=hello\nport=1\nworkers=1\nhost=hello\n"; + assert!(matches!( + parse_config(invalid0), + Err(ConfigError::DuplicateField(s)) if s == "host" + )); + + let invalid1 = "host=hello\nport=1\nworkers=1\nport=2\n"; + assert!(matches!( + parse_config(invalid1), + Err(ConfigError::DuplicateField(s)) if s == "port" + )); + + let invalid2 = "host=hello\nport=1\nworkers=1\nworkers=4\n"; + assert!(matches!( + parse_config(invalid2), + Err(ConfigError::DuplicateField(s)) if s == "workers" + )); +} + +#[test] +fn test_invalid_value() { + let invalid0 = "host=hello...&&&\nport=1\nworkers=1\n"; + assert!(matches!( + parse_config(invalid0), + Err(ConfigError::InvalidValue(s)) if s == "hello...&&&" + )); + + let invalid1 = "host=hello\nport=0\nworkers=1\n"; + assert!(matches!( + parse_config(invalid1), + Err(ConfigError::InvalidValue(s)) if s == "0" + )); + + let invalid2 = "host=hello\nport=1\nworkers=0\n"; + assert!(matches!( + parse_config(invalid2), + Err(ConfigError::InvalidValue(s)) if s == "0" + )); + + let invalid3 = "host=hello\nport=99999999999999999999\nworkers=1\n"; + assert!(matches!( + parse_config(invalid3), + Err(ConfigError::InvalidValue(s)) if s == "99999999999999999999" + )); + + let invalid4 = "host=hello\nport=1\nworkers=999999999999999999999999999999999\n"; + assert!(matches!( + parse_config(invalid4), + Err(ConfigError::InvalidValue(s)) if s == "999999999999999999999999999999999" + )); + + let invalid5 = "host=hello\nport=abc\nworkers=1\n"; + assert!(matches!( + parse_config(invalid5), + Err(ConfigError::InvalidValue(s)) if s == "abc" + )); + + let invalid6 = "host=hello\nport=1\nworkers=def\n"; + assert!(matches!( + parse_config(invalid6), + Err(ConfigError::InvalidValue(s)) if s == "def" + )); +} + +#[test] +fn test_missing_field() { + let invalid0 = "port=1\nworkers=1\n"; + assert!(matches!( + parse_config(invalid0), + Err(ConfigError::MissingField(s)) if s == "host" + )); + + let invalid1 = "host=hello\nworkers=1\n"; + assert!(matches!( + parse_config(invalid1), + Err(ConfigError::MissingField(s)) if s == "port" + )); + + let invalid2 = "host=hello\nport=1\n"; + assert!(matches!( + parse_config(invalid2), + Err(ConfigError::MissingField(s)) if s == "workers" + )); +} |
