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
extern crate regex;
extern crate yaml_rust;


use std::fs;
use regex::Regex;
use yaml_rust::{YamlLoader, YamlEmitter};

pub struct YamlIdentifiers{
    list: Vec<Regex>,
    combined: Regex
}

impl YamlIdentifiers{
    fn is_match(&self, s: &str) -> bool {
        return self.combined.is_match(s);
    }
}

pub fn extract_from_file(filename: String) {

    // let str_id = "ID[.*]";
    // let str_ref = "";
    // let str_add = "";
    // "let str_end = "";
    
    let content = fs::read_to_string(filename)
        .expect("Could not read the file");
    
    println!("{}", check(content, get_yogurt_identifiers()));
}

pub fn check(content: String, identifiers: YamlIdentifiers) -> bool {
    return identifiers.is_match(&content)
}

pub fn get_yogurt_identifiers() -> YamlIdentifiers {
    let re_id = Regex::new(r"ID\[.*\]").unwrap();
    let re_ref = Regex::new(r"REF\[.*\]").unwrap();
    let re_add = Regex::new(r"ADD\[.*\]").unwrap();
    let re_end = Regex::new(r"END\[.*\]").unwrap();
    let re_combined = Regex::new(r"(END\[.*\]|ADD\[.*\]|REF\[.*\]|ID\[.*\])").unwrap();

    return YamlIdentifiers{list: vec![re_id, re_ref, re_add, re_end], combined: re_combined};
}

fn check_wired(content: String) -> bool{
    let identifiers = get_yogurt_identifiers();
    
    print!("ID {} ", identifiers.list[0].is_match(&content));
    print!("REF {} ", identifiers.list[1].is_match(&content));
    print!("ADD {} ", identifiers.list[2].is_match(&content));
    print!("END {} ", identifiers.list[3].is_match(&content));

    return identifiers.is_match(&content);
}

pub fn example() {
    let s =
"
foo:
    - list1
    - list2
bar:
    - 1
    - 2.0
";
    let docs = YamlLoader::load_from_str(s).unwrap();

    // Multi document support, doc is a yaml::Yaml
    let doc = &docs[0];

    // Debug support
    println!("{:?}", doc);

    // Index access for map & array
    assert_eq!(doc["foo"][0].as_str().unwrap(), "list1");
    assert_eq!(doc["bar"][1].as_f64().unwrap(), 2.0);

    // Chained key/array access is checked and won't panic,
    // return BadValue if they are not exist.
    assert!(doc["INVALID_KEY"][100].is_badvalue());

    // Dump the YAML object
    let mut out_str = String::new();
    {
        let mut emitter = YamlEmitter::new(&mut out_str);
        emitter.dump(doc).unwrap(); // dump the YAML object to a String
    }
    println!("{}", out_str);
}

#[cfg(test)]
mod tests {
    use crate::check_wired;

    #[test]
    fn checker_id() {
        assert_eq!( check_wired("ID[Hello]".to_string()), true );
        assert_eq!( check_wired("ID[Hello".to_string()), false );
        assert_eq!( check_wired("I[Hello]".to_string()), false );
    }

    #[test]
    fn checker_ref() {
        assert_eq!( check_wired("REF[Hello]".to_string()), true );
    }

    #[test]
    fn checker_add() {
        assert_eq!( check_wired("ADD[Hello]".to_string()), true );
    }

    #[test]
    fn checker_end() {
        assert_eq!( check_wired("END[Hello]".to_string()), true );
    }
}