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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use crate::Directive;
use crate::Scfg;
use std::fmt;
use std::io;

#[derive(Debug)]
enum ErrorKind {
    UnexpectedClosingBrace,
    Io(io::Error),
    ShellWords(shell_words::ParseError),
}

#[derive(Debug)]
pub struct Error {
    kind: ErrorKind,
    lineno: usize,
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "parsing error at line {}: ", self.lineno)?;
        match &self.kind {
            ErrorKind::UnexpectedClosingBrace => write!(f, "unexpected '}}'"),
            ErrorKind::Io(err) => write!(f, "io: {}", err),
            ErrorKind::ShellWords(err) => write!(f, "{}", err),
        }
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match &self.kind {
            ErrorKind::Io(err) => Some(err),
            ErrorKind::ShellWords(err) => Some(err),
            _ => None,
        }
    }
}

pub fn document(mut r: impl io::BufRead) -> Result<Scfg, Error> {
    let mut lineno = 0;
    let (block, closing_brace) = read_block(&mut r, &mut lineno)?;
    if closing_brace {
        return Err(Error {
            kind: ErrorKind::UnexpectedClosingBrace,
            lineno,
        });
    }
    Ok(block)
}

/// Reads a block.
///
/// Returns `(block, closing_brace)` where `closing_brace` is true if parsing stopped on '}', and
/// false if parsing stopped on EOF.
///
/// `lineno` must be set the line number of the first line of the block minus one, and is set to
/// the line number of the closing bracket or EOF.
fn read_block<R: io::BufRead>(r: &mut R, lineno: &mut usize) -> Result<(Scfg, bool), Error> {
    let mut block = Scfg::new();
    let mut line = String::new();

    loop {
        *lineno += 1;
        line.clear();
        let n = r.read_line(&mut line).map_err(|err| Error {
            kind: ErrorKind::Io(err),
            lineno: *lineno,
        })?;
        if n == 0 {
            // reached EOF.
            return Ok((block, false));
        }
        let line = line.trim();

        let mut words = shell_words::split(&line).map_err(|err| Error {
            kind: ErrorKind::ShellWords(err),
            lineno: *lineno,
        })?;
        if words.is_empty() {
            // line is either empty or a comment.
            continue;
        }

        let last_byte = *line.as_bytes().last().unwrap();
        if words.len() == 1 && last_byte == b'}' {
            // The line is a litteral '}' (end of block).
            return Ok((block, true));
        }

        let has_child = words.last().unwrap() == "{" && last_byte == b'{'; // avoid matching `"{"`
        let (name, directive) = if has_child {
            words.pop(); // remove brace
            let name = if words.is_empty() {
                String::new()
            } else {
                words.remove(0)
            };
            let (child, closing_brace) = read_block(r, lineno)?;
            if !closing_brace {
                return Err(Error {
                    kind: ErrorKind::Io(io::ErrorKind::UnexpectedEof.into()),
                    lineno: *lineno,
                });
            }
            (
                name,
                Directive {
                    params: words,
                    child: Some(child),
                },
            )
        } else {
            let name = words.remove(0);
            (
                name,
                Directive {
                    params: words,
                    child: None,
                },
            )
        };
        block.add_directive(name, directive);
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::*;

    #[test]
    fn unexpected_bracket() {
        let src = r#"domain example.com

# TLS endpoint
listen 0.0.0.0:6697 {
    certificate "/etc/letsencrypt/live/example.com/fullchain.pem"
    key         "/etc/letsencrypt/live/example.com/privkey.pem"
}
}

listen 127.0.0.1:6667
"#;

        let err = Scfg::from_str(src).unwrap_err();
        assert!(matches!(err.kind, ErrorKind::UnexpectedClosingBrace));
        assert_eq!(err.lineno, 8);
    }

    #[test]
    fn unexpected_eof() {
        let src = r#"domain example.com

# TLS endpoint
listen 0.0.0.0:6697 {
    certificate "/etc/letsencrypt/live/example.com/fullchain.pem"
"#;

        let err = Scfg::from_str(src).unwrap_err();
        match err.kind {
            ErrorKind::Io(err) => {
                assert_eq!(err.kind(), io::ErrorKind::UnexpectedEof);
            }
            _ => {
                panic!("unexpected error kind {:?}", err.kind);
            }
        }
        assert_eq!(err.lineno, 6);
    }

    #[test]
    fn missing_quote() {
        let src = r#"domain example.com

# TLS endpoint
listen 0.0.0.0:6697 {
    certificate "/etc/letsencrypt/live/example.com/fullchain.pem
    key         "/etc/letsencrypt/live/example.com/privkey.pem"
}

listen 127.0.0.1:6667
"#;

        let err = Scfg::from_str(src).unwrap_err();
        assert!(matches!(err.kind, ErrorKind::ShellWords(_)));
        assert_eq!(err.lineno, 5);
    }
}