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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use std::fmt::Write as _;
/// Formatting configuration for use with [`KdlDocument::autoformat_config`](`crate::KdlDocument::autoformat_config`)
/// and [`KdlNode::autoformat_config`](`crate::KdlNode::autoformat_config`).
#[non_exhaustive]
#[derive(Debug)]
pub struct FormatConfig<'a> {
/// How deeply to indent the overall node or document,
/// in repetitions of [`indent`](`FormatConfig::indent`).
/// Defaults to `0`.
pub indent_level: usize,
/// The indentation to use at each level. Defaults to four spaces.
pub indent: &'a str,
/// Whether to remove comments. Defaults to `false`.
pub no_comments: bool,
/// Whether to keep individual entry formatting.
pub entry_autoformate_keep: bool,
}
/// See field documentation for defaults.
impl Default for FormatConfig<'_> {
fn default() -> Self {
Self::builder().build()
}
}
impl FormatConfig<'_> {
/// Creates a new [`FormatConfigBuilder`] with default configuration.
pub const fn builder() -> FormatConfigBuilder<'static> {
FormatConfigBuilder::new()
}
}
/// A [`FormatConfig`] builder.
///
/// Note that setters can be repeated.
#[derive(Debug, Default)]
pub struct FormatConfigBuilder<'a>(FormatConfig<'a>);
impl<'a> FormatConfigBuilder<'a> {
/// Creates a new [`FormatConfig`] builder with default configuration.
pub const fn new() -> Self {
Self(FormatConfig {
indent_level: 0,
indent: " ",
no_comments: false,
entry_autoformate_keep: false,
})
}
/// How deeply to indent the overall node or document,
/// in repetitions of [`indent`](`FormatConfig::indent`).
/// Defaults to `0` iff not specified.
pub const fn maybe_indent_level(mut self, indent_level: Option<usize>) -> Self {
if let Some(indent_level) = indent_level {
self.0.indent_level = indent_level;
}
self
}
/// How deeply to indent the overall node or document,
/// in repetitions of [`indent`](`FormatConfig::indent`).
/// Defaults to `0` iff not specified.
pub const fn indent_level(mut self, indent_level: usize) -> Self {
self.0.indent_level = indent_level;
self
}
/// The indentation to use at each level.
/// Defaults to four spaces iff not specified.
pub const fn maybe_indent<'b, 'c>(self, indent: Option<&'b str>) -> FormatConfigBuilder<'c>
where
'a: 'b,
'b: 'c,
{
if let Some(indent) = indent {
self.indent(indent)
} else {
self
}
}
/// The indentation to use at each level.
/// Defaults to four spaces if not specified.
pub const fn indent(self, indent: &str) -> FormatConfigBuilder<'_> {
FormatConfigBuilder(FormatConfig { indent, ..self.0 })
}
/// Whether to remove comments.
/// Defaults to `false` iff not specified.
pub const fn maybe_no_comments(mut self, no_comments: Option<bool>) -> Self {
if let Some(no_comments) = no_comments {
self.0.no_comments = no_comments;
}
self
}
/// Whether to remove comments.
/// Defaults to `false` iff not specified.
pub const fn no_comments(mut self, no_comments: bool) -> Self {
self.0.no_comments = no_comments;
self
}
/// Builds the [`FormatConfig`].
pub const fn build(self) -> FormatConfig<'a> {
self.0
}
}
pub(crate) fn autoformat_leading(leading: &mut String, config: &FormatConfig<'_>) {
let mut result = String::new();
if !config.no_comments {
let input = leading.trim();
if !input.is_empty() {
for line in input.lines() {
let trimmed = line.trim();
if !trimmed.is_empty() {
for _ in 0..config.indent_level {
result.push_str(config.indent);
}
writeln!(result, "{trimmed}").unwrap();
}
}
}
}
for _ in 0..config.indent_level {
result.push_str(config.indent);
}
*leading = result;
}
pub(crate) fn autoformat_trailing(decor: &mut String, no_comments: bool) {
if decor.is_empty() {
return;
}
*decor = decor.trim().to_string();
let mut result = String::new();
if !decor.is_empty() && !no_comments {
if decor.trim_start() == &decor[..] {
write!(result, " ").unwrap();
}
for comment in decor.lines() {
writeln!(result, "{comment}").unwrap();
}
}
*decor = result;
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn builder() -> miette::Result<()> {
let built = FormatConfig::builder()
.indent_level(12)
.indent(" \t")
.no_comments(true)
.build();
assert!(matches!(
built,
FormatConfig {
indent_level: 12,
indent: " \t",
no_comments: true,
entry_autoformate_keep: false,
}
));
Ok(())
}
}