1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use std::fmt;

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
pub struct RenderOpts {
    /// width of each indent
    pub indent_width: usize,
    /// current indent depth
    pub ident_count: usize,
}

impl Default for RenderOpts {
    fn default() -> Self {
        Self {
            indent_width: 2,
            ident_count: 0,
        }
    }
}

impl RenderOpts {
    /// Indent
    ///
    /// This will clone Self, and increment self.ident_count.
    pub fn indent(&self) -> Self {
        Self {
            indent_width: self.indent_width,
            ident_count: self.ident_count + 1,
        }
    }

    /// Outdent
    ///
    /// This will clone Self, and decrement self.ident_count.
    pub fn outdent(&self) -> Self {
        Self {
            indent_width: self.indent_width,
            ident_count: self.ident_count - 1,
        }
    }

    /// Get the actual characters
    pub fn spaces(&self) -> String {
        let space_count = self.indent_width * self.ident_count;
        " ".repeat(space_count)
    }
}

pub trait Render {
    fn render(&self, f: &mut fmt::Formatter<'_>, options: &RenderOpts) -> fmt::Result;
}