oxidd_dump/dot/
mod.rs

1//! Visual representation using [dot]
2//!
3//! [dot]: https://graphviz.org/docs/layouts/dot/
4
5use std::fmt;
6
7use oxidd_core::Tag;
8
9/// Edge styles
10#[derive(Clone, Copy, PartialEq, Eq, Debug)]
11pub enum EdgeStyle {
12    #[allow(missing_docs)]
13    Solid,
14    #[allow(missing_docs)]
15    Dashed,
16    #[allow(missing_docs)]
17    Dotted,
18}
19
20impl fmt::Display for EdgeStyle {
21    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22        f.write_str(match self {
23            EdgeStyle::Solid => "solid",
24            EdgeStyle::Dashed => "dashed",
25            EdgeStyle::Dotted => "dotted",
26        })
27    }
28}
29
30/// RGB colors
31///
32/// `Color(0, 0, 0)` is black, `Color(255, 255, 255)` is white.
33#[derive(Clone, Copy, PartialEq, Eq, Debug)]
34pub struct Color(pub u8, pub u8, pub u8);
35
36impl Color {
37    #[allow(missing_docs)]
38    pub const BLACK: Self = Color(0, 0, 0);
39}
40
41impl fmt::Display for Color {
42    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43        write!(f, "#{:02X}{:02X}{:02X}", self.0, self.1, self.2)
44    }
45}
46
47/// Styling attributes defined by the node type
48pub trait DotStyle<T: Tag> {
49    /// Get the style for the `n`-th outgoing edge, tagged with `tag`
50    ///
51    /// Returns the edge style (solid/dashed/dotted), whether it is bold, and
52    /// the color.
53    ///
54    /// The default implementation distinguishes three kinds of edges (all
55    /// non-bold and black):
56    ///
57    /// 1. If the edge is tagged (i.e. `tag` is not the default value) then the
58    ///    edge is dotted.
59    /// 2. If the edge is untagged and the second edge (`no == 1`), then it is
60    ///    dashed
61    /// 3. Otherwise the edge is solid
62    fn edge_style(no: usize, tag: T) -> (EdgeStyle, bool, Color) {
63        (
64            if tag != Default::default() {
65                EdgeStyle::Dotted
66            } else if no == 1 {
67                EdgeStyle::Dashed
68            } else {
69                EdgeStyle::Solid
70            },
71            false,
72            Color::BLACK,
73        )
74    }
75}
76
77#[cfg(feature = "dot")]
78mod dot_impl;
79#[cfg(feature = "dot")]
80pub use dot_impl::dump_all;