ilmen_dot_parser/dot_parser/
edge.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
use crate::TypeRelation;

use super::{attributs::Attributs,  parsing_error::ParsingError};

#[derive(PartialEq, Eq, Debug, Clone)]
pub struct Edge{
    pub node_out: NodeId,
    pub node_in: NodeId,
    pub relation: TypeRelation,
    pub attributs: Attributs
}

type NodeId = String;


impl TryFrom<(&str, &str)> for Edge {
    type Error = ParsingError;

    fn try_from(value: (&str, &str)) -> Result<Self, Self::Error> {
        let splitted = value.0
            .split_once(value.1)
            .ok_or(ParsingError::DefaultError("wtf".to_string()))?;

        let left_node= splitted.0.trim().to_string();
        let relation = TypeRelation::try_from(value.1)?;
        let right_node = splitted.1
            .split_once("[")
            .unwrap_or((splitted.1, "")).0
            .trim()
            .to_string();

        let attributs = match splitted.1.split_once("[") {
            Some((_, "")) => Attributs::default(),
            Some((_,b)) => Attributs::try_from(&b.replace("]",""))?,
            None => Attributs::default()
        };

        Ok(Self{node_out: left_node, node_in: right_node, relation, attributs})
    }
}

impl ToString for Edge {
    fn to_string(&self) -> String {
        self.node_out.clone() + " " + "->" + " " + &self.node_in + " " + &self.attributs.to_string() + ";"
    }
}

mod tests {
    

    
    
    
    
    #[test]
    fn try_from_ok() {
        let mut  map = HashMap::new();
        map.insert("toto".to_string(), "tutu".to_string());
        let combinations :Vec<(&str,Edge)> = vec![
            ("A->B", Edge{node_out: "A".to_string(), node_in: "B".to_string(), relation: TypeRelation::Oriente, attributs: Attributs::default()}),
            (" A -> B ", Edge{node_out: "A".to_string(), node_in: "B".to_string(), relation: TypeRelation::Oriente, attributs: Attributs::default()}),
            ("A->B[toto=tutu]", Edge{node_out: "A".to_string(), node_in: "B".to_string(), relation: TypeRelation::Oriente, attributs: Attributs::from(map)})
            ];
            

        combinations.iter().for_each(|combinaisons| assert_eq!(Edge::try_from((combinaisons.0, "->")).unwrap(), combinaisons.1));
    } 
}