summaryrefslogtreecommitdiff
path: root/learn_21/src/config.rs
blob: cb73caab011c238655d8e5f535f4474c2eed4a67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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)
}