ilmen_dot_parser/dot_parser/
node.rs

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
use crate::Attributs;
use super::parsing_error::ParsingError;

#[derive(PartialEq, Eq, Debug, Clone, Default)]
pub struct Node{
    pub identifier: String,
    pub attributes: Attributs
}

impl TryFrom<&String> for Node {
    type Error = ParsingError;

    fn try_from(value: &String) -> Result<Self, Self::Error> {

        let split = value.split_once("[").unwrap_or((value, ""));
        let attr = match split.1.is_empty() {
            true => Attributs::default(),
            false => Attributs::try_from(&split.1.replace("]",""))?
        };
        
        Ok(Self{identifier: split.0.trim().to_string(), attributes: attr})
    }
}



impl Node {
    pub fn new(identifier: &str, attributes: Attributs) -> Self {
        Self{
            identifier: identifier.to_string(), 
            attributes
        }
    }
}

impl ToString for Node {
    fn to_string(&self) -> String {
        let mut content = self.identifier.clone();

        let attributes_to_string = self.attributes.to_string();

        content = content + &attributes_to_string +  ";";
        content
    }
}

mod test {
    

    

    #[test]
    fn try_from_ok() {
        
        let mut first_map = HashMap::new();
        first_map.insert("label".to_string(), "\"toto\"".to_string());
        let mut second_map = HashMap::new();
        second_map.insert("label".to_string(), "\"toto\"".to_string());
        second_map.insert("encore".to_string(), "2".to_string());
        let combinations :Vec<(&str,Node)> = vec![
            ("A", Node::new("A", Attributs::default())),
            ("A_long_name", Node::new("A_long_name", Attributs::default())),
            ("Bepourquoi[label=\"toto\"]", Node::new("Bepourquoi",Attributs::from(first_map))),
            ("Bepourquoi[label=\"toto\",encore=2]", Node::new("Bepourquoi", Attributs::from(second_map)))
            ];
            
    
        combinations.iter().for_each(|combinaisons| assert_eq!(Node::try_from(&combinaisons.0.to_string()).unwrap(), combinaisons.1));
    } 
}