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
extern crate toml;
#[macro_use] extern crate quick_error;

use toml::{Parser, Value, ParserError};
use std::path::Path;
use std::fs::File;
use std::io;
use std::io::Read;

quick_error! {
    /// Wraps all errors this library can produce
    #[derive(Debug)]
    pub enum LoadError {
        /// IO Error
        Io(err: io::Error) {
            from()
        }
        /// TOML parsing error
        Parse(err: Vec<ParserError>) {
            from()
        }
    }
}

/// Imlements helper functions for loading and parsing .toml files
pub struct Loader;

impl Loader {
    /// Create Toml::Value from file specified by path
    ///
    /// # Example
    /// ```no_run
    /// use toml_loader::Loader;
    /// use std::path::Path;
    ///
    /// let toml = Loader::from_file(Path::new("some.toml")).unwrap();
    /// ```
    pub fn from_file(path: &Path) -> Result<Value, LoadError> {
       let mut f = try!(File::open(path));
       let mut s = String::new();
       try!(f.read_to_string(&mut s));
       let mut parser = Parser::new(&s);
       let res = try!(parser.parse().map(Value::Table).ok_or(parser.errors));
       Ok(res)
    }
}

#[cfg(test)]
mod test {
    extern crate tempfile;

    use super::Loader;
    use self::tempfile::NamedTempFile;
    use std::io::Write;
    use std::path::Path;

    macro_rules! tmp_toml_file {
        ($content:expr) => {
            {
                let mut f = NamedTempFile::new().unwrap();
                f.write_all($content.as_bytes()).unwrap();
                f.flush().unwrap();
                f
            }
        }
    }

    #[test]
    fn loads_valid_toml_single() {
        let f = tmp_toml_file!("a = 1");
        Loader::from_file(f.path()).unwrap();
    }

    #[test]
    #[should_panic]
    fn fails_to_load_invalid_toml_single() {
        let f = tmp_toml_file!("a - 1");
        Loader::from_file(f.path()).unwrap();
    }

    #[test]
    #[should_panic]
    fn fails_to_load_non_existing_file() {
        // Path "?\0" is invalid on both windows and linux (windows can't have ? and unix can't have \0)
        Loader::from_file(Path::new("?\0")).unwrap();
    }
}