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
// Std
use std::error::Error;
use std::fs;
use std::io;

// External
use clap::ArgMatches;
use toml::ser;
use toml::Value;

// Internal
use crate::error::TomlqError;

mod error;

#[cfg(test)]
mod test;

/// A structure to store parameters
pub struct Param {
    pub key: String,
    pub filename: String,
}

impl Param {
    /// Creates a new instance of an Param.
    ///
    /// # Examples
    ///
    /// ```
    /// # use clap::{Command, Arg};
    /// # use tomlq_rs::Param;
    /// let matches = Command::new("myapp")
    ///                 .arg(
    ///                     Arg::with_name("KEY")
    ///                         .help("Key to query from the TOML file")
    ///                         .required(true)
    ///                         .index(1),
    ///                     )
    ///                 .arg(
    ///                     Arg::with_name("FILE")
    ///                         .help("A TOML file to load")
    ///                         .required(true)
    ///                         .index(2),
    ///                     )
    ///                 .get_matches_from(vec!["myapp", "key", "a.toml"]);
    ///
    /// let param = Param::new(&matches).unwrap();
    /// assert_eq!(param.key, "key");
    /// assert_eq!(param.filename, "a.toml");
    /// ```
    pub fn new(matches: &ArgMatches) -> Result<Param, &'static str> {
        let key = match matches.value_of("KEY") {
            Some(f) => f.to_string(),
            None => {
                return Err("Must specify Key to query!");
            }
        };
        let filename = match matches.value_of("FILE") {
            Some(f) => f.to_string(),
            None => {
                return Err("Must specify File to load!");
            }
        };
        Ok(Param { key, filename })
    }
}

/// Parse toml file
///
/// # Example
///
/// ```
/// # use tomlq_rs::{Param, parse};
/// let param = Param {
///     key: String::from("package.name"),
///     filename: String::from("Cargo.toml")
/// };
/// let result = parse(param).unwrap();
/// assert_eq!(result, ());
/// ```
pub fn parse(param: Param) -> Result<(), Box<dyn Error>> {
    let toml_string = match fs::read_to_string(&param.filename) {
        Ok(c) => c,
        Err(e) => {
            match e.kind() {
                io::ErrorKind::NotFound => {
                    return Err(Box::new(TomlqError(String::from("No such file!"))));
                }
                _ => {
                    return Err(Box::new(TomlqError(String::from("Cannot read the file!"))));
                }
            };
        }
    };

    let value = query_toml_value(&toml_string, &param.key)?;
    println!("{}", value);

    Ok(())
}

/// Query value from toml by key.
///
/// # Examples
///
/// ```
/// # use tomlq_rs::query_toml_value;
/// let toml_str = r#"key = "value"  # This is a comment at the end of a line"#;
/// let key = "key";
/// let value = query_toml_value(toml_str, key).unwrap();
/// assert_eq!(value, "value");
/// ```
pub fn query_toml_value(toml_str: &str, key: &str) -> Result<String, String> {
    let toml_obj = match toml::from_str(toml_str) {
        Ok(v) => v,
        Err(_) => return Err(String::from("Parsing failed!")),
    };

    let value = parse_key(key)
        .iter()
        .fold(
            Some(&toml_obj),
            |accumulator: Option<&Value>, key| match accumulator {
                Some(a) => a.get(key),
                None => None,
            },
        );

    match value {
        // TODO: 添加选项输出 ser::to_string_pretty()
        Some(v) => match ser::to_string(v) {
            Ok(s) => match v.type_str() {
                "string" => Ok(format!("{}", &s[1..s.len() - 1])),
                _ => Ok(format!("{}", s)),
            },
            Err(_) => Err(format!("Key {} serialization failed!", key)),
        },
        None => Err(format!("Key {} not found!", key)),
    }
}

/// Parse key.
///
/// # Examples
///
/// ```
/// # use tomlq_rs::parse_key;
/// let key = r#"site."google.com""#;
/// let result = parse_key(key);
/// assert_eq!(result, vec!["site", "google.com"]);
/// ```
pub fn parse_key(key: &str) -> Vec<&str> {
    let mut flag: char = '.';
    let mut index: usize = 0;
    let mut key_vector: Vec<&str> = Vec::new();

    for (i, s) in key.chars().enumerate() {
        match s {
            '.' => match flag {
                '.' => {
                    key_vector.push(key[index..i].trim_matches('"').trim_matches('\''));
                    index = i + 1;
                }
                _ => (),
            },
            '\'' => match flag {
                '\'' => flag = '.',
                '.' => flag = '\'',
                _ => (),
            },
            '"' => match flag {
                '"' => flag = '.',
                '.' => flag = '"',
                _ => (),
            },
            _ => (),
        }
    }

    key_vector.push(key[index..key.len()].trim_matches('"').trim_matches('\''));
    key_vector
}