mdbook_d2/
config.rs

1use std::path::PathBuf;
2
3use serde::Deserialize;
4
5mod default {
6    use std::path::PathBuf;
7
8    pub fn bin_path() -> PathBuf {
9        PathBuf::from("d2")
10    }
11
12    pub fn output_dir() -> PathBuf {
13        PathBuf::from("d2")
14    }
15
16    pub const fn inline() -> bool {
17        true
18    }
19}
20
21#[derive(Deserialize, PartialEq, Eq, Debug)]
22pub struct Fonts {
23    pub regular: PathBuf,
24    pub italic: PathBuf,
25    pub bold: PathBuf,
26}
27#[derive(Deserialize, PartialEq, Eq, Debug)]
28#[serde(rename_all = "kebab-case")]
29pub struct Config {
30    /// The path to the d2 binary
31    #[serde(default = "default::bin_path")]
32    pub path: PathBuf,
33
34    #[serde(default = "default::output_dir")]
35    pub output_dir: PathBuf,
36
37    pub layout: Option<String>,
38
39    /// Whether or not to use inline SVG when building an HTML target
40    ///
41    /// Default is 'true'
42    #[serde(default = "default::inline")]
43    pub inline: bool,
44
45    /// Custom font path
46    ///
47    /// Only ttf fonts are valid
48    pub fonts: Option<Fonts>,
49
50    pub theme_id: Option<String>,
51    pub dark_theme_id: Option<String>,
52}
53
54impl Default for Config {
55    fn default() -> Self {
56        Self {
57            path: default::bin_path(),
58            layout: None,
59            output_dir: default::output_dir(),
60            inline: default::inline(),
61            fonts: None,
62            theme_id: None,
63            dark_theme_id: None,
64        }
65    }
66}
67
68#[cfg(test)]
69mod tests {
70    use std::path::PathBuf;
71
72    use test_case::test_case;
73
74    use super::Config;
75
76    #[test_case(""; "empty")]
77    #[test_case(
78        r#"
79path = "d2"
80layout = "dagre"
81output-dir = "d2"
82"#
83        ; "defaults"
84    )]
85    fn compatible(input: &str) {
86        let _config: Config = toml::from_str(input).expect("config is not compatible");
87    }
88
89    #[test_case("" => Config::default(); "default")]
90    #[test_case(
91        r#"
92path = "/custom/bin/d2"
93layout = "elk"
94output-dir = "d2-img"
95"#
96    => Config {
97        path: PathBuf::from("/custom/bin/d2"),
98        layout: Some(String::from("elk")),
99        inline: true,
100        output_dir: PathBuf::from("d2-img"),
101        fonts: None,
102        theme_id: None,
103        dark_theme_id:None,
104    }
105        ; "custom"
106    )]
107    fn parse(input: &str) -> Config {
108        toml::from_str(input).unwrap()
109    }
110}