From 7757056c7a43d4fbd85e3e817237d178bf6b1761 Mon Sep 17 00:00:00 2001 From: Brad Clawsie Date: Wed, 25 Nov 2026 14:18:46 -0600 Subject: fix: learn_21 --- learn_21/src/config.rs | 25 ++++++++++++++++--------- learn_21/tests/load_config.rs | 8 ++++++-- learn_21/tests/parse_config.rs | 25 ++++++++++++++++++++----- 3 files changed, 42 insertions(+), 16 deletions(-) diff --git a/learn_21/src/config.rs b/learn_21/src/config.rs index cb73caa..e0f21c4 100644 --- a/learn_21/src/config.rs +++ b/learn_21/src/config.rs @@ -26,12 +26,17 @@ pub fn parse_config(input: &str) -> Result { continue; } - let (key, val) = trimmed.split_once('=').ok_or_else(|| ConfigError::InvalidLine(i))?; + let (k, v) = trimmed.split_once('=').ok_or_else(|| ConfigError::InvalidLine(i))?; + let key = k.trim(); + let val = v.trim(); // one of the components is empty - if key.is_empty() || val.is_empty() { + if key.is_empty() { return Err(ConfigError::InvalidLine(i)); } + if val.is_empty() { + return Err(ConfigError::InvalidValue("".to_string())) + } // bad key if !valid_keys.contains(&key) { @@ -44,12 +49,6 @@ pub fn parse_config(input: &str) -> Result { 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()); } @@ -58,6 +57,10 @@ pub fn parse_config(input: &str) -> Result { if port.is_some() { return Err(ConfigError::DuplicateField("port".to_string())); } + + if !val.chars().all(|c| c.is_ascii_digit()) { + return Err(ConfigError::InvalidValue(val.to_string())); + } port = Some(val.parse(). map_err(|_| ConfigError::InvalidValue(val.to_string()))?); @@ -68,7 +71,11 @@ pub fn parse_config(input: &str) -> Result { if workers.is_some() { return Err(ConfigError::DuplicateField("workers".to_string())); } - + + if !val.chars().all(|c| c.is_ascii_digit()) { + return Err(ConfigError::InvalidValue(val.to_string())); + } + workers = Some(val.parse(). map_err(|_| ConfigError::InvalidValue(val.to_string()))?); } diff --git a/learn_21/tests/load_config.rs b/learn_21/tests/load_config.rs index 827ed02..5fc7866 100644 --- a/learn_21/tests/load_config.rs +++ b/learn_21/tests/load_config.rs @@ -16,8 +16,12 @@ fn test_load_config() -> io::Result<()> { 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()); + assert!(load_config(&file_path).is_ok_and(|x| x == + Config{host:"myhost".to_string(), port:1, workers:1})); + assert!(matches!( + load_config(std::path::Path::new("/bad/path")), + Err(ConfigError::Io(Error)), + )); fs::remove_file(&file_path)?; Ok(()) diff --git a/learn_21/tests/parse_config.rs b/learn_21/tests/parse_config.rs index 8ca6626..6b963bc 100644 --- a/learn_21/tests/parse_config.rs +++ b/learn_21/tests/parse_config.rs @@ -11,20 +11,23 @@ fn test_ok() { 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"; + 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})); + 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})); + let ok4 = "\n# comment\n host= my-host \nport=1 \n workers=1 \n"; + assert!(parse_config(ok4).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)) @@ -69,10 +72,10 @@ fn test_duplicate_field() { #[test] fn test_invalid_value() { - let invalid0 = "host=hello...&&&\nport=1\nworkers=1\n"; + let invalid0 = "host= \nport=1\nworkers=1\n"; assert!(matches!( parse_config(invalid0), - Err(ConfigError::InvalidValue(s)) if s == "hello...&&&" + Err(ConfigError::InvalidValue(s)) if s == "" )); let invalid1 = "host=hello\nport=0\nworkers=1\n"; @@ -110,6 +113,18 @@ fn test_invalid_value() { parse_config(invalid6), Err(ConfigError::InvalidValue(s)) if s == "def" )); + + let invalid7 = "host=hello\nport=+1\nworkers=1\n"; + assert!(matches!( + parse_config(invalid7), + Err(ConfigError::InvalidValue(s)) if s == "+1" + )); + + let invalid8 = "host=hello\nport=1\nworkers=+1\n"; + assert!(matches!( + parse_config(invalid8), + Err(ConfigError::InvalidValue(s)) if s == "+1" + )); } #[test] -- cgit v1.2.3