aboutsummaryrefslogtreecommitdiff
path: root/dot-dsl/src
diff options
context:
space:
mode:
Diffstat (limited to 'dot-dsl/src')
-rw-r--r--dot-dsl/src/lib.rs119
1 files changed, 119 insertions, 0 deletions
diff --git a/dot-dsl/src/lib.rs b/dot-dsl/src/lib.rs
new file mode 100644
index 0000000..654ee09
--- /dev/null
+++ b/dot-dsl/src/lib.rs
@@ -0,0 +1,119 @@
1pub mod graph {
2
3 use graph_items::edge::Edge;
4 use graph_items::node::Node;
5 use std::collections::HashMap;
6
7 pub mod graph_items {
8
9 pub mod node {
10
11 use std::collections::HashMap;
12
13 #[derive(Default, Debug, PartialEq, Eq, Clone)]
14 pub struct Node<'a> {
15 pub name: &'a str,
16 attrs: HashMap<String, String>,
17 }
18
19 impl<'a> Node<'a> {
20 pub fn new(name: &'a str) -> Node<'a> {
21 Node {
22 name,
23 ..Default::default()
24 }
25 }
26
27 pub fn with_attrs(mut self, attrs: &[(&'a str, &'a str)]) -> Node<'a> {
28 for (key, value) in attrs {
29 self.attrs.insert(key.to_string(), value.to_string());
30 }
31 self
32 }
33
34 pub fn get_attr(&self, key: &str) -> Option<&str> {
35 self.attrs.get(key).map(|s| &s[..])
36 }
37 }
38 }
39
40 pub mod edge {
41
42 use std::collections::HashMap;
43
44 #[derive(Default, Debug, PartialEq, Eq, Clone)]
45 pub struct Edge<'a> {
46 pub x: &'a str,
47 pub y: &'a str,
48 attrs: HashMap<String, String>,
49 }
50
51 impl<'a> Edge<'a> {
52 pub fn new(x: &'a str, y: &'a str) -> Edge<'a> {
53 Edge {
54 x,
55 y,
56 ..Default::default()
57 }
58 }
59
60 pub fn with_attrs(mut self, attrs: &[(&'a str, &'a str)]) -> Edge<'a> {
61 for (key, value) in attrs {
62 self.attrs.insert(key.to_string(), value.to_string());
63 }
64 self
65 }
66
67 pub fn get_attr(&self, key: &str) -> Option<&str> {
68 self.attrs.get(key).map(|s| &s[..])
69 }
70 }
71 }
72 }
73
74 #[derive(Default, Debug)]
75 pub struct Graph<'a> {
76 pub nodes: Vec<Node<'a>>,
77 pub edges: Vec<Edge<'a>>,
78 pub attrs: HashMap<String, String>,
79 }
80
81 impl<'a> Graph<'a> {
82 pub fn new() -> Self {
83 Default::default()
84 }
85
86 pub fn with_attrs(mut self, attrs: &[(&'a str, &'a str)]) -> Graph<'a> {
87 for (key, value) in attrs {
88 self.attrs.insert(key.to_string(), value.to_string());
89 }
90 self
91 }
92
93 pub fn with_nodes(mut self, nodes: &[Node<'a>]) -> Graph<'a> {
94 for node in nodes {
95 self.nodes.push(node.clone());
96 }
97 self
98 }
99
100 pub fn with_edges(mut self, edges: &[Edge<'a>]) -> Graph<'a> {
101 for edge in edges {
102 self.edges.push(edge.clone())
103 }
104 self
105 }
106
107 pub fn get_node(&self, name: &str) -> Option<&Node<'a>> {
108 self.nodes.iter().find(|&node| node.name == name)
109 }
110
111 pub fn get_edge(&self, x: &str, y: &str) -> Option<&Edge<'a>> {
112 self.edges.iter().find(|&edge| edge.x == x && edge.y == y)
113 }
114
115 pub fn get_attr(&self, key: &str) -> Option<&str> {
116 self.attrs.get(key).map(|s| &s[..])
117 }
118 }
119}