diff options
| author | Brad Clawsie <brad@b7j0c.org> | 2026-11-25 14:18:46 -0600 |
|---|---|---|
| committer | Brad Clawsie <brad@b7j0c.org> | 2026-11-25 14:18:46 -0600 |
| commit | 7757056c7a43d4fbd85e3e817237d178bf6b1761 (patch) | |
| tree | 3c4fc7eac704b1237799b034d2d883aac8c1574c /learn_21/src/config.rs | |
| parent | 07e343541d0e60e34e582d43efd8cf573f8bb344 (diff) | |
fix: learn_21
Diffstat (limited to 'learn_21/src/config.rs')
| -rw-r--r-- | learn_21/src/config.rs | 25 |
1 files changed, 16 insertions, 9 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<Config, ConfigError> { 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<Config, ConfigError> { 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<Config, ConfigError> { 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<Config, ConfigError> { 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()))?); } |
