open_hypergraphs_dot/
options.rs

1use std::fmt;
2use std::fmt::{Debug, Display};
3
4pub struct Options<O, A> {
5    pub orientation: Orientation,
6    pub theme: Theme,
7    pub node_label: Box<dyn Fn(&O) -> String>,
8    pub edge_label: Box<dyn Fn(&A) -> String>,
9}
10
11impl<O: Debug, A: Debug> Default for Options<O, A> {
12    fn default() -> Self {
13        Self {
14            orientation: Default::default(),
15            theme: Default::default(),
16            node_label: Box::new(|n| format!("{:?}", n)),
17            edge_label: Box::new(|e| format!("{:?}", e)),
18        }
19    }
20}
21
22// Fluent interface for Options
23impl<O: Display, A: Display> Options<O, A> {
24    pub fn display(mut self) -> Self {
25        self.node_label = Box::new(|n| format!("{}", n));
26        self.edge_label = Box::new(|e| format!("{}", e));
27        self
28    }
29
30    pub fn lr(mut self) -> Self {
31        self.orientation = Orientation::LR;
32        self
33    }
34}
35
36////////////////////////////////////////////////////////////////////////////////
37// Orientation
38
39/// Graph orientation for visualization
40#[derive(Debug, Clone, Copy, Default)]
41pub enum Orientation {
42    /// Left to right layout
43    LR,
44    /// Top to bottom layout
45    #[default]
46    TB,
47}
48
49// Used for dot output
50impl fmt::Display for Orientation {
51    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52        match self {
53            Orientation::LR => write!(f, "LR"),
54            Orientation::TB => write!(f, "TB"),
55        }
56    }
57}
58
59////////////////////////////////////////////////////////////////////////////////
60// Themes
61
62/// Theme for graph visualization
63pub struct Theme {
64    pub bgcolor: String,
65    pub fontcolor: String,
66    pub color: String,
67    pub orientation: Orientation,
68}
69
70pub fn light_theme() -> Theme {
71    Theme {
72        bgcolor: String::from("white"),
73        fontcolor: String::from("black"),
74        color: String::from("black"),
75        orientation: Orientation::LR,
76    }
77}
78/// A dark theme preset
79pub fn dark_theme() -> Theme {
80    Theme {
81        bgcolor: String::from("#4a4a4a"),
82        fontcolor: String::from("white"),
83        color: String::from("white"),
84        orientation: Orientation::LR,
85    }
86}
87
88impl Default for Theme {
89    fn default() -> Self {
90        dark_theme()
91    }
92}