wit_encoder/
render.rs

1use std::fmt;
2
3#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
4#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
5#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
6pub struct RenderOpts {
7    /// width of each indent
8    pub indent_width: usize,
9    /// current indent depth
10    pub ident_count: usize,
11}
12
13impl Default for RenderOpts {
14    fn default() -> Self {
15        Self {
16            indent_width: 2,
17            ident_count: 0,
18        }
19    }
20}
21
22impl RenderOpts {
23    /// Indent
24    ///
25    /// This will clone Self, and increment self.ident_count.
26    pub fn indent(&self) -> Self {
27        Self {
28            indent_width: self.indent_width,
29            ident_count: self.ident_count + 1,
30        }
31    }
32
33    /// Outdent
34    ///
35    /// This will clone Self, and decrement self.ident_count.
36    pub fn outdent(&self) -> Self {
37        Self {
38            indent_width: self.indent_width,
39            ident_count: self.ident_count - 1,
40        }
41    }
42
43    /// Get the actual characters
44    pub fn spaces(&self) -> String {
45        let space_count = self.indent_width * self.ident_count;
46        " ".repeat(space_count)
47    }
48}
49
50pub trait Render {
51    fn render(&self, f: &mut fmt::Formatter<'_>, options: &RenderOpts) -> fmt::Result;
52}