oak_pretty_print/config/
mod.rs1use alloc::borrow::Cow;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)]
5#[serde(rename_all = "snake_case")]
6pub enum IndentStyle {
7 Spaces(u8),
9 Tabs,
11}
12
13impl Default for IndentStyle {
14 fn default() -> Self {
15 IndentStyle::Spaces(4)
16 }
17}
18
19#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)]
21#[serde(rename_all = "snake_case")]
22pub enum LineEnding {
23 Unix,
25 Windows,
27 Auto,
29}
30
31impl Default for LineEnding {
32 fn default() -> Self {
33 LineEnding::Auto
34 }
35}
36
37#[derive(Debug, Clone, serde::Serialize)]
39#[serde(rename_all = "camelCase")]
40pub struct FormatConfig {
41 pub indent_style: IndentStyle,
43 pub indent_text: Cow<'static, str>,
45 pub line_ending: LineEnding,
47 pub max_width: usize,
49 pub insert_final_newline: bool,
51 pub trim_trailing_whitespace: bool,
53 pub preserve_blank_lines: bool,
55 pub max_blank_lines: usize,
57 pub format_comments: bool,
59 pub format_strings: bool,
61 pub indent_size: usize,
63}
64
65impl Default for FormatConfig {
66 fn default() -> Self {
67 let indent_style = IndentStyle::default();
68 let (indent_text, indent_size) = match indent_style {
69 IndentStyle::Spaces(count) => (" ".repeat(count as usize).into(), count as usize),
70 IndentStyle::Tabs => ("\t".into(), 4), };
72
73 Self {
74 indent_style,
75 indent_text,
76 line_ending: LineEnding::default(),
77 max_width: 100,
78 insert_final_newline: true,
79 trim_trailing_whitespace: true,
80 preserve_blank_lines: true,
81 max_blank_lines: 2,
82 format_comments: true,
83 format_strings: false,
84 indent_size,
85 }
86 }
87}
88
89impl FormatConfig {
90 pub fn new() -> Self {
92 Self::default()
93 }
94
95 pub fn with_indent_style(mut self, style: IndentStyle) -> Self {
97 self.indent_style = style;
98 let (indent_text, indent_size) = match style {
99 IndentStyle::Spaces(count) => (" ".repeat(count as usize).into(), count as usize),
100 IndentStyle::Tabs => ("\t".into(), 4),
101 };
102 self.indent_text = indent_text;
103 self.indent_size = indent_size;
104 self
105 }
106
107 pub fn with_line_ending(mut self, ending: LineEnding) -> Self {
109 self.line_ending = ending;
110 self
111 }
112
113 pub fn with_max_width(mut self, length: usize) -> Self {
115 self.max_width = length;
116 self
117 }
118
119 pub fn line_ending_string(&self) -> &'static str {
121 match self.line_ending {
122 LineEnding::Unix => "\n",
123 LineEnding::Windows => "\r\n",
124 LineEnding::Auto => {
125 #[cfg(windows)]
127 return "\r\n";
128 #[cfg(not(windows))]
129 return "\n";
130 }
131 }
132 }
133}