Skip to main content

libpetri_export/
graph.rs

1/// Direction of graph layout.
2#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3pub enum RankDir {
4    TopToBottom,
5    LeftToRight,
6    BottomToTop,
7    RightToLeft,
8}
9
10impl RankDir {
11    pub fn as_dot(&self) -> &'static str {
12        match self {
13            RankDir::TopToBottom => "TB",
14            RankDir::LeftToRight => "LR",
15            RankDir::BottomToTop => "BT",
16            RankDir::RightToLeft => "RL",
17        }
18    }
19}
20
21/// Shape of a graph node.
22#[derive(Debug, Clone, Copy, PartialEq, Eq)]
23pub enum NodeShape {
24    Circle,
25    DoubleCircle,
26    Box,
27    Diamond,
28    Ellipse,
29    Point,
30    Record,
31}
32
33impl NodeShape {
34    pub fn as_dot(&self) -> &'static str {
35        match self {
36            NodeShape::Circle => "circle",
37            NodeShape::DoubleCircle => "doublecircle",
38            NodeShape::Box => "box",
39            NodeShape::Diamond => "diamond",
40            NodeShape::Ellipse => "ellipse",
41            NodeShape::Point => "point",
42            NodeShape::Record => "record",
43        }
44    }
45}
46
47/// Style of an edge line.
48#[derive(Debug, Clone, Copy, PartialEq, Eq)]
49pub enum EdgeLineStyle {
50    Solid,
51    Dashed,
52    Bold,
53}
54
55impl EdgeLineStyle {
56    pub fn as_dot(&self) -> &'static str {
57        match self {
58            EdgeLineStyle::Solid => "solid",
59            EdgeLineStyle::Dashed => "dashed",
60            EdgeLineStyle::Bold => "bold",
61        }
62    }
63}
64
65/// Arrowhead type for edges.
66#[derive(Debug, Clone, Copy, PartialEq, Eq)]
67pub enum ArrowHead {
68    Normal,
69    None,
70    Odot,
71    Dot,
72    Diamond,
73    Vee,
74}
75
76impl ArrowHead {
77    pub fn as_dot(&self) -> &'static str {
78        match self {
79            ArrowHead::Normal => "normal",
80            ArrowHead::None => "none",
81            ArrowHead::Odot => "odot",
82            ArrowHead::Dot => "dot",
83            ArrowHead::Diamond => "diamond",
84            ArrowHead::Vee => "vee",
85        }
86    }
87}
88
89/// A node in the graph.
90#[derive(Debug, Clone)]
91pub struct GraphNode {
92    pub id: String,
93    pub label: String,
94    pub shape: NodeShape,
95    pub fill: Option<String>,
96    pub stroke: Option<String>,
97    pub penwidth: Option<f64>,
98    pub semantic_id: Option<String>,
99    pub style: Option<String>,
100    pub height: Option<f64>,
101    pub width: Option<f64>,
102    pub attrs: Vec<(String, String)>,
103}
104
105/// An edge in the graph.
106#[derive(Debug, Clone)]
107pub struct GraphEdge {
108    pub from: String,
109    pub to: String,
110    pub label: Option<String>,
111    pub color: Option<String>,
112    pub style: Option<EdgeLineStyle>,
113    pub arrowhead: Option<ArrowHead>,
114    pub penwidth: Option<f64>,
115    pub arc_type: Option<String>,
116    pub attrs: Vec<(String, String)>,
117}
118
119/// A subgraph (cluster).
120#[derive(Debug, Clone)]
121pub struct Subgraph {
122    pub id: String,
123    pub label: Option<String>,
124    pub nodes: Vec<GraphNode>,
125    pub edges: Vec<GraphEdge>,
126    pub attrs: Vec<(String, String)>,
127}
128
129/// A complete graph.
130#[derive(Debug, Clone)]
131pub struct Graph {
132    pub name: String,
133    pub rankdir: RankDir,
134    pub nodes: Vec<GraphNode>,
135    pub edges: Vec<GraphEdge>,
136    pub subgraphs: Vec<Subgraph>,
137    pub node_defaults: Vec<(String, String)>,
138    pub edge_defaults: Vec<(String, String)>,
139    pub graph_attrs: Vec<(String, String)>,
140}
141
142impl Graph {
143    pub fn new(name: impl Into<String>) -> Self {
144        Self {
145            name: name.into(),
146            rankdir: RankDir::TopToBottom,
147            nodes: Vec::new(),
148            edges: Vec::new(),
149            subgraphs: Vec::new(),
150            node_defaults: Vec::new(),
151            edge_defaults: Vec::new(),
152            graph_attrs: Vec::new(),
153        }
154    }
155}