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
// (c) 2017 Joost Yervante Damad <joost@damad.be>

#![warn(missing_docs)]
#![recursion_limit="128"]

//! `beerXML`/json/yaml/toml parsing and generating library

extern crate quick_xml;
extern crate serde;
extern crate serde_json;
extern crate serde_yaml;
extern crate toml as serde_toml;

#[macro_use]
extern crate error_chain;
#[macro_use]
extern crate log;
#[macro_use]
extern crate serde_derive;

use std::path::Path;

use data::RecordSet;
use error::*;

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {}
}

/// read a `beerXML`, json, yaml or toml file
pub fn read_file(filename: &Path) -> Result<RecordSet> {
    if let Some(ext) = filename.extension() {
        match ext.to_str().unwrap() {
            "xml" => xml::read_file(filename),
            "json" => json::read_file(filename),
            "yaml" => yaml::read_file(filename),
            "toml" => toml::read_file(filename),
            e => Err(format!("unknown file extension {}", e).into()),
        }
    } else {
        Err("no file extension found".into())
    }
}

/// write a `beerXML`, json, yaml or toml file
pub fn write_file(filename: &Path, set: &RecordSet) -> Result<()> {
    if let Some(ext) = filename.extension() {
        match ext.to_str().unwrap() {
            "xml" => xml::write_file(filename, set),
            "json" => json::write_file(filename, set),
            "yaml" => yaml::write_file(filename, set),
            "toml" => toml::write_file(filename, set),
            e => Err(format!("unknown file extension {}", e).into()),
        }
    } else {
        Err("no file extension found".into())
    }
}

/// data structures
pub mod data;
/// error handling
pub mod error;
/// xml parsing and generating
pub mod xml;
/// json parsing and generating
pub mod json;
/// toml parsing and generating
pub mod toml;
/// yaml parsing and generating
pub mod yaml;