ilmen_dot_parser/dot_parser/
edge.rs1use crate::TypeRelation;
2
3use super::{attributs::Attributs, parsing_error::ParsingError};
4
5
6#[derive(PartialEq, Eq, Debug, Clone)]
7#[cfg_attr(
8 feature = "serde",
9 derive(serde::Serialize, serde::Deserialize)
10)]
11pub struct Edge{
12 pub node_out: NodeId,
13 pub node_in: NodeId,
14 pub relation: TypeRelation,
15 pub attributs: Attributs
16}
17
18type NodeId = String;
19
20
21impl TryFrom<(&str, &str)> for Edge {
22 type Error = ParsingError;
23
24 fn try_from(value: (&str, &str)) -> Result<Self, Self::Error> {
25 let splitted = value.0
26 .split_once(value.1)
27 .ok_or(ParsingError::DefaultError("wtf".to_string()))?;
28
29 let left_node= splitted.0.trim().to_string();
30 let relation = TypeRelation::try_from(value.1)?;
31 let right_node = splitted.1
32 .split_once("[")
33 .unwrap_or((splitted.1, "")).0
34 .trim()
35 .to_string();
36
37 let attributs = match splitted.1.split_once("[") {
38 Some((_, "")) => Attributs::default(),
39 Some((_,b)) => Attributs::try_from(&b.replace("]",""))?,
40 None => Attributs::default()
41 };
42
43 Ok(Self{node_out: left_node, node_in: right_node, relation, attributs})
44 }
45}
46
47impl ToString for Edge {
48 fn to_string(&self) -> String {
49 self.node_out.clone() + " " + "->" + " " + &self.node_in + " " + &self.attributs.to_string() + ";"
50 }
51}
52
53mod tests {
54
55
56
57 #[test]
58 fn try_from_ok() {
59 let mut map = HashMap::new();
60 map.insert("toto".to_string(), "tutu".to_string());
61 let combinations :Vec<(&str,Edge)> = vec![
62 ("A->B", Edge{node_out: "A".to_string(), node_in: "B".to_string(), relation: TypeRelation::Oriente, attributs: Attributs::default()}),
63 (" A -> B ", Edge{node_out: "A".to_string(), node_in: "B".to_string(), relation: TypeRelation::Oriente, attributs: Attributs::default()}),
64 ("A->B[toto=tutu]", Edge{node_out: "A".to_string(), node_in: "B".to_string(), relation: TypeRelation::Oriente, attributs: Attributs::from(map)})
65 ];
66
67
68 combinations.iter().for_each(|combinaisons| assert_eq!(Edge::try_from((combinaisons.0, "->")).unwrap(), combinaisons.1));
69 }
70}