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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
use std::fmt::{Display, Formatter};
#[derive(Debug, PartialEq, Clone, Eq)]
pub struct Port(pub Option<Id>, pub Option<String>);
#[derive(Debug, Clone,PartialEq,Eq)]
pub enum Id {
Html(String),
Escaped(String),
Plain(String),
Anonymous(String),
}
impl Display for Id {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Id::Html(v) => f.write_str(format!("html {}", v).as_str()),
Id::Escaped(v) => f.write_str(format!("esc {}", v).as_str()),
Id::Plain(v) => f.write_str(format!("{}", v).as_str()),
Id::Anonymous(v) => f.write_str(format!("anon {}", v).as_str()),
}
}
}
#[derive(Debug, PartialEq, Clone)]
pub struct NodeId(pub Id, pub Option<Port>);
#[derive(PartialEq, Debug, Clone)]
pub struct Attribute(pub Id, pub Id);
#[derive(PartialEq, Debug, Clone)]
pub enum GraphAttributes {
Graph(Vec<Attribute>),
Node(Vec<Attribute>),
Edge(Vec<Attribute>),
}
impl GraphAttributes {
pub fn new(ty: &str, attrs: Vec<Attribute>) -> Self {
match ty.to_lowercase().as_str() {
"graph" => GraphAttributes::Graph(attrs),
"node" => GraphAttributes::Node(attrs),
"edge" => GraphAttributes::Edge(attrs),
_ => panic!("only graph, node, edge is applied here. ")
}
}
}
#[derive(Debug, PartialEq, Clone)]
pub struct Edge {
pub ty: EdgeTy,
pub attributes: Vec<Attribute>,
}
impl Edge {
fn add_attr(&mut self,attr:Attribute){
self.attributes.push(attr)
}
}
#[derive(Debug, PartialEq, Clone)]
pub enum EdgeTy {
Pair(Vertex, Vertex),
Chain(Vec<Vertex>),
}
#[derive(Debug, PartialEq, Clone)]
pub struct Node {
pub id: NodeId,
pub attributes: Vec<Attribute>,
}
impl Node {
pub fn new(id: NodeId, attributes: Vec<Attribute>) -> Self {
Node { id, attributes }
}
fn add_attr(&mut self,attr:Attribute){
self.attributes.push(attr)
}
}
#[derive(PartialEq, Debug, Clone)]
pub enum Stmt {
Node(Node),
Subgraph(Subgraph),
Attribute(Attribute),
GAttribute(GraphAttributes),
Edge(Edge),
}
impl From<Node> for Stmt {
fn from(v: Node) -> Self {
Stmt::Node(v)
}
}
impl From<Edge> for Stmt {
fn from(v: Edge) -> Self {
Stmt::Edge(v)
}
}
impl From<GraphAttributes> for Stmt {
fn from(v: GraphAttributes) -> Self {
Stmt::GAttribute(v)
}
}
impl From<Attribute> for Stmt {
fn from(v: Attribute) -> Self {
Stmt::Attribute(v)
}
}
impl From<Subgraph> for Stmt {
fn from(v: Subgraph) -> Self {
Stmt::Subgraph(v)
}
}
#[derive(PartialEq, Debug, Clone)]
pub struct Subgraph {
pub id: Id,
pub stmts: Vec<Stmt>,
}
impl Subgraph {
fn add_stmt(&mut self,stmt:Stmt){
self.stmts.push(stmt)
}
}
#[derive(PartialEq, Debug, Clone)]
pub enum Vertex {
N(NodeId),
S(Subgraph),
}
impl From<NodeId> for Vertex {
fn from(v: NodeId) -> Self {
Vertex::N(v)
}
}
impl From<Subgraph> for Vertex {
fn from(v: Subgraph) -> Self {
Vertex::S(v)
}
}
#[derive(Debug, PartialEq,Clone)]
pub enum Graph {
Graph { id: Id, strict: bool, stmts: Vec<Stmt> },
DiGraph { id: Id, strict: bool, stmts: Vec<Stmt> },
}
impl Graph {
pub fn add_stmt(&mut self,stmt:Stmt){
match self {
Graph::Graph { stmts,.. } => {stmts.push(stmt)}
Graph::DiGraph { stmts,.. } => {stmts.push(stmt)}
}
}
}