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
119
|
pub mod graph {
use graph_items::edge::Edge;
use graph_items::node::Node;
use std::collections::HashMap;
pub mod graph_items {
pub mod node {
use std::collections::HashMap;
#[derive(Default, Debug, PartialEq, Eq, Clone)]
pub struct Node<'a> {
pub name: &'a str,
attrs: HashMap<String, String>,
}
impl<'a> Node<'a> {
pub fn new(name: &'a str) -> Node<'a> {
Node {
name,
..Default::default()
}
}
pub fn with_attrs(mut self, attrs: &[(&'a str, &'a str)]) -> Node<'a> {
for (key, value) in attrs {
self.attrs.insert(key.to_string(), value.to_string());
}
self
}
pub fn get_attr(&self, key: &str) -> Option<&str> {
self.attrs.get(key).map(|s| &s[..])
}
}
}
pub mod edge {
use std::collections::HashMap;
#[derive(Default, Debug, PartialEq, Eq, Clone)]
pub struct Edge<'a> {
pub x: &'a str,
pub y: &'a str,
attrs: HashMap<String, String>,
}
impl<'a> Edge<'a> {
pub fn new(x: &'a str, y: &'a str) -> Edge<'a> {
Edge {
x,
y,
..Default::default()
}
}
pub fn with_attrs(mut self, attrs: &[(&'a str, &'a str)]) -> Edge<'a> {
for (key, value) in attrs {
self.attrs.insert(key.to_string(), value.to_string());
}
self
}
pub fn get_attr(&self, key: &str) -> Option<&str> {
self.attrs.get(key).map(|s| &s[..])
}
}
}
}
#[derive(Default, Debug)]
pub struct Graph<'a> {
pub nodes: Vec<Node<'a>>,
pub edges: Vec<Edge<'a>>,
pub attrs: HashMap<String, String>,
}
impl<'a> Graph<'a> {
pub fn new() -> Self {
Default::default()
}
pub fn with_attrs(mut self, attrs: &[(&'a str, &'a str)]) -> Graph<'a> {
for (key, value) in attrs {
self.attrs.insert(key.to_string(), value.to_string());
}
self
}
pub fn with_nodes(mut self, nodes: &[Node<'a>]) -> Graph<'a> {
for node in nodes {
self.nodes.push(node.clone());
}
self
}
pub fn with_edges(mut self, edges: &[Edge<'a>]) -> Graph<'a> {
for edge in edges {
self.edges.push(edge.clone())
}
self
}
pub fn get_node(&self, name: &str) -> Option<&Node<'a>> {
self.nodes.iter().find(|&node| node.name == name)
}
pub fn get_edge(&self, x: &str, y: &str) -> Option<&Edge<'a>> {
self.edges.iter().find(|&edge| edge.x == x && edge.y == y)
}
pub fn get_attr(&self, key: &str) -> Option<&str> {
self.attrs.get(key).map(|s| &s[..])
}
}
}
|