config_parser/
lib.rs

1#![doc(html_root_url = "https://kilobyte22.de/doc/config_parser/")]
2
3pub mod config;
4pub mod error;
5pub mod lexer;
6pub mod parser;
7
8pub use config::ConfigBlock;
9pub use error::{Result, Error as ParseError};
10
11use std::fs::File;
12use std::io::Read;
13
14/// Parses a configuration file by an iterator of chars
15pub fn parse<T, I>(iter: T) -> Result<ConfigBlock> where 
16        T: IntoIterator<Item=char, IntoIter=I> + Sized,
17        I: Iterator<Item=char> + 'static {
18    parser::run(Box::new(try!(lexer::run(Box::new(iter.into_iter()))).into_iter()))
19}
20
21pub fn parse_string(data: String) -> Result<ConfigBlock> {
22    parse(OwningChars::new(data))
23}
24
25pub fn parse_file(mut file: File) -> Result<ConfigBlock> {
26    let mut s = String::new();
27    file.read_to_string(&mut s).unwrap();
28    parse_string(s)
29}
30
31struct OwningChars { s: String, pos: usize }
32
33impl OwningChars {
34    pub fn new(s: String) -> OwningChars {
35        OwningChars { s: s, pos: 0 }
36    }
37}
38
39impl Iterator for OwningChars {
40    type Item = char;
41    fn next(&mut self) -> Option<char> {
42        if let Some(c) = self.s[self.pos..].chars().next() {
43            self.pos += c.len_utf8();
44            Some(c)
45        } else {
46            None
47        }
48    }
49    fn size_hint(&self) -> (usize, Option<usize>) {
50        let len = self.s.len() - self.pos;
51        ((len + 3) / 4, Some(len)) // see the Chars impl for detail
52    }
53}
54
55#[cfg(test)]
56mod tests {
57    
58}