rusty_ci/helper/
yaml.rs

1use rusty_yaml::Yaml;
2
3/// This function unwraps a Yaml object,
4/// takes its first value, and converts it into a string,
5/// and trims quotation marks.
6pub fn unwrap<S: ToString>(yaml: &Yaml, section: S) -> String {
7    let result = yaml
8        .get_section(section.to_string())
9        .unwrap()
10        .nth(0)
11        .to_string();
12
13    let first = match result.chars().nth(0) {
14        Some(v) => v,
15        None => ' ',
16    };
17    let last = match result.chars().nth(result.len() - 1) {
18        Some(v) => v,
19        None => ' ',
20    };
21    if first == last {
22        match first {
23            // If the first and last character are the same, and are
24            // both forms of quotes, trim the outer most ones.
25            '\'' | '"' => result[1..result.len() - 1].to_string(),
26            _ => result.to_string(),
27        }
28    } else {
29        result.to_string()
30    }
31}
32
33/// This function takes a Yaml object and confirms that there are no unmatched quotes!
34/// If there are unmatched quotes, it returns the line with unmatched quotes
35pub fn unmatched_quotes(yaml: &Yaml) -> Option<String> {
36    for line in yaml.to_string().lines() {
37        if line.matches('"').count() % 2 != 0 {
38            return Some(line.to_string());
39        }
40    }
41    None
42}