1use bitflags::bitflags;
9#[cfg(feature = "serialize")]
10use serde::Serialize;
11
12use super::output::OutputRendererBuilder;
13use super::output::OutputRendererOptions;
14use super::pipeline::graph_to_row_shape::GraphRowShaper;
15
16pub trait Renderer<N> {
17 type Output;
18
19 fn width(&self, new_node: Option<&N>, new_parents: Option<&Vec<Ancestor<N>>>) -> u64;
21
22 fn reserve(&mut self, node: N);
24
25 fn next_row(
27 &mut self,
28 node: N,
29 parents: Vec<Ancestor<N>>,
30 glyph: String,
31 message: String,
32 ) -> Self::Output;
33
34 fn output_options_mut(&mut self) -> &mut OutputRendererOptions;
36
37 fn output_options(&self) -> &OutputRendererOptions;
39}
40
41pub struct GraphRowRenderer<N> {
45 inner: GraphRowShaper<N>,
46 output_options: OutputRendererOptions,
47}
48
49pub enum Ancestor<N> {
51 Ancestor(N),
53
54 Parent(N),
56
57 Anonymous,
59}
60
61#[derive(Debug, Copy, Clone, PartialEq, Eq)]
63#[cfg_attr(feature = "serialize", derive(Serialize))]
64pub enum NodeLine {
65 Blank,
67
68 Ancestor,
70
71 Parent,
73
74 Node,
76}
77
78#[derive(Debug, Copy, Clone, PartialEq, Eq)]
80#[cfg_attr(feature = "serialize", derive(Serialize))]
81pub enum PadLine {
82 Blank,
84
85 Ancestor,
87
88 Parent,
90}
91
92bitflags! {
93 #[cfg_attr(feature = "serialize", derive(Serialize))]
95 #[derive(Default, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
96 pub struct LinkLine: u16 {
97 const HORIZ_PARENT = 0b0_0000_0000_0001;
99
100 const HORIZ_ANCESTOR = 0b0_0000_0000_0010;
102
103 const VERT_PARENT = 0b0_0000_0000_0100;
105
106 const VERT_ANCESTOR = 0b0_0000_0000_1000;
108
109 const LEFT_FORK_PARENT = 0b0_0000_0001_0000;
112
113 const LEFT_FORK_ANCESTOR = 0b0_0000_0010_0000;
116
117 const RIGHT_FORK_PARENT = 0b0_0000_0100_0000;
120
121 const RIGHT_FORK_ANCESTOR = 0b0_0000_1000_0000;
124
125 const LEFT_MERGE_PARENT = 0b0_0001_0000_0000;
127
128 const LEFT_MERGE_ANCESTOR = 0b0_0010_0000_0000;
130
131 const RIGHT_MERGE_PARENT = 0b0_0100_0000_0000;
133
134 const RIGHT_MERGE_ANCESTOR = 0b0_1000_0000_0000;
136
137 const CHILD = 0b1_0000_0000_0000;
141
142 const HORIZONTAL = Self::HORIZ_PARENT.bits() | Self::HORIZ_ANCESTOR.bits();
143 const VERTICAL = Self::VERT_PARENT.bits() | Self::VERT_ANCESTOR.bits();
144 const LEFT_FORK = Self::LEFT_FORK_PARENT.bits() | Self::LEFT_FORK_ANCESTOR.bits();
145 const RIGHT_FORK = Self::RIGHT_FORK_PARENT.bits() | Self::RIGHT_FORK_ANCESTOR.bits();
146 const LEFT_MERGE = Self::LEFT_MERGE_PARENT.bits() | Self::LEFT_MERGE_ANCESTOR.bits();
147 const RIGHT_MERGE = Self::RIGHT_MERGE_PARENT.bits() | Self::RIGHT_MERGE_ANCESTOR.bits();
148 const ANY_MERGE = Self::LEFT_MERGE.bits() | Self::RIGHT_MERGE.bits();
149 const ANY_FORK = Self::LEFT_FORK.bits() | Self::RIGHT_FORK.bits();
150 const ANY_FORK_OR_MERGE = Self::ANY_MERGE.bits() | Self::ANY_FORK.bits();
151 }
152}
153
154#[derive(Debug)]
170#[cfg_attr(feature = "serialize", derive(Serialize))]
171pub struct GraphRow<N> {
172 pub node: N,
174
175 pub glyph: String,
177
178 pub message: String,
180
181 pub merge: bool,
183
184 pub separator_line: bool,
186
187 pub node_line: Vec<NodeLine>,
189
190 pub link_line: Option<Vec<LinkLine>>,
192
193 pub term_line: Option<Vec<bool>>,
196
197 pub pad_lines: Vec<PadLine>,
199}
200
201impl<N> GraphRowRenderer<N>
202where
203 N: Clone + Eq + std::fmt::Debug,
204{
205 pub fn new() -> Self {
207 GraphRowRenderer {
208 inner: GraphRowShaper::new(),
209 output_options: Default::default(),
210 }
211 }
212
213 pub fn output(self) -> OutputRendererBuilder<N, Self> {
215 OutputRendererBuilder::new(self)
216 }
217}
218
219impl<N> Renderer<N> for GraphRowRenderer<N>
220where
221 N: Clone + Eq + std::fmt::Debug,
222{
223 type Output = GraphRow<N>;
224
225 fn width(&self, node: Option<&N>, parents: Option<&Vec<Ancestor<N>>>) -> u64 {
226 self.inner
227 .width_with_options(node, parents, self.output_options)
228 }
229
230 fn reserve(&mut self, node: N) {
231 self.inner.reserve(node);
232 }
233
234 fn next_row(
235 &mut self,
236 node: N,
237 parents: Vec<Ancestor<N>>,
238 glyph: String,
239 message: String,
240 ) -> GraphRow<N> {
241 *self.inner.options_mut() = self.output_options;
242 let row = self.inner.next_row_shape(node, parents);
243
244 GraphRow {
245 node: row.node,
246 glyph,
247 message,
248 merge: row.merge,
249 node_line: row.node_line,
250 link_line: row.link_line,
251 term_line: row.term_line,
252 pad_lines: row.ancestry_line,
253 separator_line: row.separator_line,
254 }
255 }
256
257 fn output_options_mut(&mut self) -> &mut OutputRendererOptions {
259 &mut self.output_options
260 }
261
262 fn output_options(&self) -> &OutputRendererOptions {
264 &self.output_options
265 }
266}