renderdag/pipeline/types.rs
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#[cfg(feature = "serialize")]
9use serde::Serialize;
10
11pub use crate::render::Ancestor;
12pub use crate::render::LinkLine;
13pub use crate::render::NodeLine;
14pub use crate::render::PadLine;
15
16/// Style choices that affect graph row shapes and prefix-line placement.
17#[derive(Clone, Copy, Debug, PartialEq, Eq)]
18#[cfg_attr(feature = "serialize", derive(Serialize))]
19pub struct GraphRowShapeStyle {
20 /// How long indirect-ancestor dash lines remain in ancestor style.
21 pub dash_style: DashStyle,
22
23 /// Where link-related padding lines are placed around the node line.
24 pub link_line_placement: LinkLinePlacement,
25}
26
27impl Default for GraphRowShapeStyle {
28 fn default() -> Self {
29 Self {
30 dash_style: DashStyle::Persistent,
31 link_line_placement: LinkLinePlacement::Top,
32 }
33 }
34}
35
36/// How indirect-ancestor dash lines continue across rows.
37#[derive(Clone, Copy, Debug, PartialEq, Eq)]
38#[cfg_attr(feature = "serialize", derive(Serialize))]
39pub enum DashStyle {
40 /// Keep ancestor lines in ancestor style until the branch disappears.
41 ///
42 /// ```plain
43 /// o
44 /// ╷
45 /// ╷
46 /// ╷
47 /// o
48 /// ```
49 Persistent,
50
51 /// Render ancestor style at most once per branch, then continue as parent.
52 ///
53 /// ```plain
54 /// o
55 /// ┆
56 /// │
57 /// │
58 /// o
59 /// ```
60 Once,
61}
62
63/// Where link-line padding is placed relative to the node line.
64#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
65#[cfg_attr(feature = "serialize", derive(Serialize))]
66pub enum LinkLinePlacement {
67 /// Link lines are at the top.
68 ///
69 /// ```plain
70 /// o
71 /// ├─╮
72 /// │ │
73 /// │ │
74 /// ```
75 #[default]
76 Top,
77
78 /// Link lines are at the bottom.
79 ///
80 /// ```plain
81 /// o
82 /// │
83 /// │
84 /// ├─╮
85 /// ```
86 Bottom,
87}
88
89/// Options that affect the graph row shape produced from the input node stream.
90///
91/// These options belong to the first pipeline stage. They may affect column
92/// allocation, separator rows, and the abstract edge shape. They do not choose
93/// glyph characters or place message text.
94pub type GraphRowShapeOptions = crate::OutputRendererOptions;
95
96/// An abstract graph row shape for one rendered node.
97///
98/// This is the output of the graph-to-rows stage. It captures the node's
99/// current column, the edge shape around it, and any row-level facts needed by
100/// later stages. It intentionally does not contain glyphs or message text.
101///
102/// Example of different type of lines, depending on the rendering style,
103/// certain lines can be skipped or repeated:
104///
105/// ```plain
106/// │ // pre_node_line() (optional, repeatable)
107/// │ // pre_node_line()
108/// o F // node_line (mandory, once)
109/// │ very long message 0 // post_node_line() (optional, repeatable)
110/// │ very long message 0 // post_node_line()
111/// ├─┬─╮ very long message 1 // link_line (mandory, once)
112/// │ │ ~ very long message 2 // term_line (mandory, once)
113/// : │ very long message 3 // ancestry_line (mandory, once or repeatable)
114/// │ │ very long message 4 // post_ancestry_line() (optional, repeatable)
115/// │ │ very long message 5 // post_ancestry_line()
116/// ```
117#[derive(Clone, Debug, PartialEq, Eq)]
118// This is `Serialize` so non-text environment (e.g. via wasm) can get
119// the rich data (than just ambiguous text characters) to render more
120// precisely. Example: `RenderDag.js` in D41252231.
121#[cfg_attr(feature = "serialize", derive(Serialize))]
122pub struct GraphRowShape<N> {
123 /// The node represented by this row.
124 pub node: N,
125
126 /// True if this row connects to multiple parents.
127 ///
128 /// Some prefix renderers use this to choose clearer junction glyphs.
129 pub merge: bool,
130
131 /// True if this row needs a blank separator before its graph lines.
132 pub separator_line: bool,
133
134 /// Style used to produce this row shape.
135 pub style: GraphRowShapeStyle,
136
137 /// True if this node is a head (no children, uses a new column).
138 pub is_head: bool,
139
140 /// True if this node has no parents.
141 pub is_root: bool,
142
143 /// The column containing the node glyph.
144 pub node_column: usize,
145
146 /// The columns occupied by this node's parents.
147 pub parent_columns: Vec<usize>,
148
149 /// Abstract columns for the line containing the node.
150 pub node_line: Vec<NodeLine>,
151
152 /// Abstract columns for the line connecting the node to its parents.
153 ///
154 /// This is absent when the node and its parents can be represented without
155 /// an explicit link line.
156 pub link_line: Option<Vec<LinkLine>>,
157
158 /// The part of `link_line` that is directly emitted from this row's node.
159 ///
160 /// For example:
161 ///
162 /// ```plain
163 /// │ o // node line
164 /// ├─╯ // link line
165 /// ```
166 ///
167 /// The `link_line_from_node` LinkLine looks like:
168 ///
169 /// ```plain
170 /// ╭─╯
171 /// ```
172 ///
173 /// Note "├" is changed to "╭".
174 ///
175 /// Consumers can use this to follow or highlight edges that originate from
176 /// the current node without guessing from the full link line.
177 pub link_line_from_node: Option<Vec<LinkLine>>,
178
179 /// Terminator columns for anonymous parents.
180 ///
181 /// A `true` entry marks a column where the edge should terminate. Other
182 /// columns should be rendered using the row's padding columns.
183 pub term_line: Option<Vec<bool>>,
184
185 /// Abstract columns for ancestry continuation below this row.
186 pub ancestry_line: Vec<PadLine>,
187}
188
189// See `GraphRowShape` docstring for what the lines mean.
190impl<N> GraphRowShape<N> {
191 /// True if the ancestry line contains an indirect ancestor edge.
192 pub fn has_indirect_ancestor(&self) -> bool {
193 self.ancestry_line
194 .iter()
195 .any(|line| *line == PadLine::Ancestor)
196 }
197
198 /// Derive the repeatable padding line before the node line.
199 pub fn pre_node_line(&self) -> Vec<PadLine> {
200 self.node_line
201 .iter()
202 .map(|line| node_to_pad_line(*line, self.is_head))
203 .collect()
204 }
205
206 /// Derive the repeatable padding line after the node line.
207 pub fn post_node_line(&self) -> Vec<PadLine> {
208 self.node_line
209 .iter()
210 .map(|line| node_to_pad_line(*line, self.is_root))
211 .collect()
212 }
213
214 /// Derive the repeatable padding line after the ancestry line.
215 pub fn post_ancestry_line(&self) -> Vec<PadLine> {
216 self.ancestry_line
217 .iter()
218 .map(|line| match line {
219 PadLine::Ancestor => PadLine::Parent,
220 other => *other,
221 })
222 .collect()
223 }
224}
225
226fn node_to_pad_line(line: NodeLine, use_blank_for_node: bool) -> PadLine {
227 match line {
228 NodeLine::Blank => PadLine::Blank,
229 NodeLine::Ancestor => PadLine::Ancestor,
230 NodeLine::Parent => PadLine::Parent,
231 NodeLine::Node => {
232 if use_blank_for_node {
233 PadLine::Blank
234 } else {
235 PadLine::Parent
236 }
237 }
238 }
239}
240
241/// A left-side graph prefix line before message text is attached.
242///
243/// This is the output of the row-shapes-to-prefix-lines stage. A prefix line
244/// may contain a node glyph slot, but it does not know what glyph will fill
245/// that slot and it does not contain any right-side message text.
246#[derive(Clone, Debug, PartialEq, Eq)]
247pub struct PrefixLine {
248 /// The ordered pieces that make up the graph prefix.
249 pub parts: Vec<PrefixLinePart>,
250
251 /// The semantic role of this line within the rendered row.
252 pub kind: PrefixLineKind,
253}
254
255/// A piece of a graph prefix line.
256#[derive(Clone, Debug, PartialEq, Eq)]
257pub enum PrefixLinePart {
258 /// Literal graph text.
259 Text(String),
260
261 /// Placeholder for the rendered node glyph.
262 ///
263 /// Keeping the glyph as a slot lets callers cache prefix lines and fill in
264 /// status-dependent glyphs later.
265 NodeGlyph,
266}
267
268/// The semantic role of a prefix line.
269///
270/// The text-output stage can use this to decide where messages should attach,
271/// which lines may repeat, and which lines are meaningful for consumers that do
272/// not render plain text.
273#[derive(Clone, Copy, Debug, PartialEq, Eq)]
274#[cfg_attr(feature = "serialize", derive(Serialize))]
275pub enum PrefixLineKind {
276 /// A blank line separating disconnected one-line rows.
277 Separator,
278
279 /// A repeatable line before the node line.
280 PreNode,
281
282 /// The line containing the rendered node glyph.
283 Node,
284
285 /// A repeatable line after the node line and before edge links.
286 PostNode,
287
288 /// A line connecting the node to parent or ancestor columns.
289 Link,
290
291 /// A line terminating an anonymous parent edge.
292 Term,
293
294 /// A line continuing ancestry or parent edges.
295 Ancestry,
296
297 /// A repeatable line after ancestry continuation.
298 PostAncestry,
299}
300
301impl PrefixLineKind {
302 /// True if this line kind can be repeated to carry additional message lines.
303 pub fn is_repeatable(self) -> bool {
304 matches!(
305 self,
306 PrefixLineKind::PreNode | PrefixLineKind::PostNode | PrefixLineKind::PostAncestry
307 )
308 }
309}
310
311/// Converts graph row shapes into prefix lines.
312pub trait PrefixLineRenderer<N> {
313 /// Convert the next graph row shape into prefix lines.
314 fn next_prefix_lines(&mut self, row_shape: &GraphRowShape<N>) -> Vec<PrefixLine>;
315}