Skip to main content

renderdag/
render.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
8use 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    // Returns the width of the graph line, possibly including another node.
20    fn width(&self, new_node: Option<&N>, new_parents: Option<&Vec<Ancestor<N>>>) -> u64;
21
22    // Reserve a column for the given node.
23    fn reserve(&mut self, node: N);
24
25    // Render the next row.
26    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    // Set the output options.
35    fn output_options_mut(&mut self) -> &mut OutputRendererOptions;
36
37    // Get the output options.
38    fn output_options(&self) -> &OutputRendererOptions;
39}
40
41/// Renderer for a DAG.
42///
43/// Converts a sequence of DAG node descriptions into rendered graph rows.
44pub struct GraphRowRenderer<N> {
45    inner: GraphRowShaper<N>,
46    output_options: OutputRendererOptions,
47}
48
49/// Ancestor type indication for an ancestor or parent node.
50pub enum Ancestor<N> {
51    /// The node is an eventual ancestor.
52    Ancestor(N),
53
54    /// The node is an immediate parent.
55    Parent(N),
56
57    /// The node is an anonymous ancestor.
58    Anonymous,
59}
60
61/// A column in the node row.
62#[derive(Debug, Copy, Clone, PartialEq, Eq)]
63#[cfg_attr(feature = "serialize", derive(Serialize))]
64pub enum NodeLine {
65    /// Blank.
66    Blank,
67
68    /// Vertical line indicating an ancestor.
69    Ancestor,
70
71    /// Vertical line indicating a parent.
72    Parent,
73
74    /// The node for this row.
75    Node,
76}
77
78/// A column in a padding row.
79#[derive(Debug, Copy, Clone, PartialEq, Eq)]
80#[cfg_attr(feature = "serialize", derive(Serialize))]
81pub enum PadLine {
82    /// Blank.
83    Blank,
84
85    /// Vertical line indicating an ancestor.
86    Ancestor,
87
88    /// Vertical line indicating a parent.
89    Parent,
90}
91
92bitflags! {
93    /// A column in a linking row.
94    #[cfg_attr(feature = "serialize", derive(Serialize))]
95    #[derive(Default, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
96    pub struct LinkLine: u16 {
97        /// This cell contains a horizontal line that connects to a parent.
98        const HORIZ_PARENT = 0b0_0000_0000_0001;
99
100        /// This cell contains a horizontal line that connects to an ancestor.
101        const HORIZ_ANCESTOR = 0b0_0000_0000_0010;
102
103        /// The descendent of this cell is connected to the parent.
104        const VERT_PARENT = 0b0_0000_0000_0100;
105
106        /// The descendent of this cell is connected to an ancestor.
107        const VERT_ANCESTOR = 0b0_0000_0000_1000;
108
109        /// The parent of this cell is linked in this link row and the child
110        /// is to the left.
111        const LEFT_FORK_PARENT = 0b0_0000_0001_0000;
112
113        /// The ancestor of this cell is linked in this link row and the child
114        /// is to the left.
115        const LEFT_FORK_ANCESTOR = 0b0_0000_0010_0000;
116
117        /// The parent of this cell is linked in this link row and the child
118        /// is to the right.
119        const RIGHT_FORK_PARENT = 0b0_0000_0100_0000;
120
121        /// The ancestor of this cell is linked in this link row and the child
122        /// is to the right.
123        const RIGHT_FORK_ANCESTOR = 0b0_0000_1000_0000;
124
125        /// The child of this cell is linked to parents on the left.
126        const LEFT_MERGE_PARENT = 0b0_0001_0000_0000;
127
128        /// The child of this cell is linked to ancestors on the left.
129        const LEFT_MERGE_ANCESTOR = 0b0_0010_0000_0000;
130
131        /// The child of this cell is linked to parents on the right.
132        const RIGHT_MERGE_PARENT = 0b0_0100_0000_0000;
133
134        /// The child of this cell is linked to ancestors on the right.
135        const RIGHT_MERGE_ANCESTOR = 0b0_1000_0000_0000;
136
137        /// The target node of this link line is the child of this column.
138        /// This disambiguates between the node that is connected in this link
139        /// line, and other nodes that are also connected vertically.
140        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/// An output graph row.
155///
156/// This "Row" contains a "node" and its surrounding edges. It does not define
157/// precisely how many lines this row should have. Lines characters are abstract
158/// so the actual rendering logic (svg, box drawing, etc) can decide what to
159/// use.
160///
161/// ```plain
162///                          // separator line
163///  o      F                // node line
164///  ├─┬─╮  long message 1   // link line
165///  ╷ │ ~  long message 2   // term line
166///  ╷ │    long message 3   // pad line
167///  ╷ │    long message 4   // pad line
168/// ```
169#[derive(Debug)]
170#[cfg_attr(feature = "serialize", derive(Serialize))]
171pub struct GraphRow<N> {
172    /// The name of the node for this row.
173    pub node: N,
174
175    /// The glyph for this node.
176    pub glyph: String,
177
178    /// The message for this row.
179    pub message: String,
180
181    /// True if this row is for a merge commit.
182    pub merge: bool,
183
184    /// True if this row needs a blank-line separator from the previous row.
185    pub separator_line: bool,
186
187    /// The node columns for this row.
188    pub node_line: Vec<NodeLine>,
189
190    /// The link columns for this row, if a link row is necessary.
191    pub link_line: Option<Vec<LinkLine>>,
192
193    /// The location of any terminators, if necessary.  Other columns should be
194    /// filled in with pad lines.
195    pub term_line: Option<Vec<bool>>,
196
197    /// The pad columns for this row.
198    pub pad_lines: Vec<PadLine>,
199}
200
201impl<N> GraphRowRenderer<N>
202where
203    N: Clone + Eq + std::fmt::Debug,
204{
205    /// Create a new renderer.
206    pub fn new() -> Self {
207        GraphRowRenderer {
208            inner: GraphRowShaper::new(),
209            output_options: Default::default(),
210        }
211    }
212
213    /// Build an output renderer from this renderer.
214    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    // Set the output options.
258    fn output_options_mut(&mut self) -> &mut OutputRendererOptions {
259        &mut self.output_options
260    }
261
262    // Get the output options.
263    fn output_options(&self) -> &OutputRendererOptions {
264        &self.output_options
265    }
266}