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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
use std::io::BufRead;
use std::fmt::{self, Debug};

#[derive(Debug)]
enum Condition {
    UnexpectedEof,
    Io(std::io::Error),
    Encoding(std::string::FromUtf8Error),
}
pub struct Error {
    condition: Condition,
    line: usize,
}
impl Debug for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "While parsing line {}: {:?}", self.line, self.condition)
    }
}
pub type Result<T> = std::result::Result<T, Error>;

pub struct Param {
    name: Vec<u8>,
    values: Vec<String>,
}
impl Param {
    pub fn name(&self) -> &str {
        std::str::from_utf8(&self.name).unwrap()
    }

    pub fn values(&self) -> impl Iterator<Item=&str> {
        self.values.iter().map(|s| s.as_str())
    }
}

pub struct ContentLine {
    name: Vec<u8>,
    params: Vec<Param>,
    value: String,
}
impl ContentLine {
    pub fn name(&self) -> &str {
        std::str::from_utf8(&self.name).unwrap()
    }

    pub fn params(&self) -> impl Iterator<Item=&Param> {
        self.params.iter()
    }

    pub fn value(&self) -> &str {
        &self.value
    }
}

pub struct Parser<S> {
    stream: S,
    line: usize,
}

fn bad_eof(line: usize) -> Error {
    Error { condition: Condition::UnexpectedEof, line }
}

fn bad_io(line: usize, e: std::io::Error) -> Error {
    Error { condition: Condition::Io(e), line }
}

fn bad_encoding(line: usize, e: std::string::FromUtf8Error) -> Error {
    Error { condition: Condition::Encoding(e), line }
}

impl<S: BufRead> Parser<S> {
    pub fn new(stream: S) -> Self {
        let line = 1;
        Self { stream, line }
    }

    pub fn parse_content_line(&mut self) -> Result<Option<ContentLine>> {
        let line = self.line;
        if self.stream.fill_buf().map_err(|e| bad_io(line, e))?.is_empty() {
            return Ok(None);
        }
        let name = self.read_name()?;
        let params = self.read_params()?;
        let value = self.read_value()?;
        Ok(Some(ContentLine { name, params, value }))
    }

    pub fn finish(self) -> S {
        self.stream
    }
}

impl<S: BufRead> Parser<S> {
    /// Get the next octet, handling "unfolding" and normalization of raw line breaks from CRLF to LF.
    fn peek(&mut self) -> Result<u8> {
        Ok(loop {
            let line = self.line;
            let c = *self.stream.fill_buf().map_err(|e| bad_io(line, e))?
                .get(0).ok_or_else(|| bad_eof(line))?;
            match c {
                b'\r' => {
                    self.stream.consume(1);
                    let c = *self.stream.fill_buf().map_err(|e| bad_io(line, e))?
                        .get(0).ok_or_else(|| bad_eof(line))?;
                    self.stream.consume(1);
                    match c {
                        b'\n' => {
                            self.line += 1;
                            let line = self.line;
                            let c = self.stream.fill_buf().map_err(|e| bad_io(line, e))?.get(0);
                            match c {
                                Some(b' ') | Some(b'\t') => {
                                    self.stream.consume(1);
                                    continue;
                                }
                                _ => break b'\n',
                            }
                        }
                        c => break c,
                    }
                }
                c => break c,
            }
        })
    }

    fn read_name(&mut self) -> Result<Vec<u8>> {
        let mut name = Vec::new();
        Ok(loop {
            match self.peek()? {
                c @ b'-' | c @ b'A'..=b'Z' | c @ b'a'..=b'z' | c @ b'0'..=b'9' => {
                    name.push(c);
                    self.stream.consume(1);
                }
                _ => break name,
            }
        })
    }

    fn read_escaped(&mut self) -> Result<u8> {
        self.stream.consume(1);
        Ok(match self.peek()? {
            b'n' => b'\n',
            c => {
                self.stream.consume(1);
                c
            }
        })
    }

    fn read_quoted(&mut self) -> Result<Vec<u8>> {
        let mut param_value = Vec::new();
        Ok(loop {
            match self.peek()? {
                b'"' => break param_value,
                b'\\' => param_value.push(self.read_escaped()?),
                c => {
                    param_value.push(c);
                    self.stream.consume(1);
                }
            }
        })
    }

    fn read_param_value(&mut self) -> Result<String> {
        let param_value = if self.peek()? == b'"' {
            self.stream.consume(1);
            let param_value = self.read_quoted()?;
            self.stream.consume(1);
            param_value
        } else {
            let mut param_value = Vec::new();
            loop {
                match self.peek()? {
                    b',' | b';' | b':' => break,
                    b'\\' => param_value.push(self.read_escaped()?),
                    c => {
                        param_value.push(c);
                        self.stream.consume(1);
                    }
                }
            }
            param_value
        };
        let line = self.line;
        String::from_utf8(param_value).map_err(|e| bad_encoding(line, e))
    }

    fn read_params(&mut self) -> Result<Vec<Param>> {
        let mut params = Vec::new();
        Ok('params: loop {
            let c = self.peek()?;
            self.stream.consume(1);
            match c {
                b';' => {
                    let param_name = self.read_name()?;
                    let mut param_values = Vec::new();
                    'param_values: loop {
                        param_values.push(self.read_param_value()?);
                        match self.peek()? {
                            b',' => continue,
                            b';' => break 'param_values,
                            b':' => break 'params params,
                            _ => unreachable!(),
                        }
                    }
                    params.push(Param { name: param_name, values: param_values });
                }
                b':' => break params,
                _ => unreachable!(),
            }
        })
    }

    fn read_value(&mut self) -> Result<String> {
        let mut value = Vec::new();
        loop {
            match self.peek()? {
                b'\n' => break,
                b'\\' => value.push(self.read_escaped()?),
                c => {
                    value.push(c);
                    self.stream.consume(1);
                }
            }
        }
        let line = self.line;
        String::from_utf8(value).map_err(|e| bad_encoding(line, e))
    }
}