Debian Codegraph @

crates/wit-encoder/src/render.rs

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