Skip to main content

html_helpers/slimmer/
slim_options.rs

1// region:    --- Types
2
3/// Options for the `slim` function (indentation, etc.).
4#[derive(Clone, Copy, Debug, Default)]
5pub struct SlimOptions {
6	/// Whether to use tabs instead of spaces for indentation.
7	pub indent_with_tabs: bool,
8	/// Number of spaces per indentation level, or `None` for flat output.
9	pub indent: Option<u8>,
10}
11
12// endregion: --- Types
13
14// region:    --- Constructors & Fluid API
15
16impl SlimOptions {
17	/// Set the number of spaces for indentation (enables formatting).
18	pub fn with_indent(mut self, spaces: u8) -> Self {
19		self.indent = Some(spaces);
20		self
21	}
22
23	/// Use tabs instead of spaces for indentation.
24	pub fn with_indent_with_tabs(mut self, tabs: bool) -> Self {
25		self.indent_with_tabs = tabs;
26		self
27	}
28}
29
30// endregion: --- Constructors & Fluid API
31
32// region:    --- Tests
33
34#[cfg(test)]
35mod tests {
36	type TestResult<T> = core::result::Result<T, Box<dyn std::error::Error>>;
37
38	use super::*;
39
40	#[test]
41	fn test_slim_options_default() -> TestResult<()> {
42		// -- Setup & Fixtures
43		// -- Exec
44		let opts = SlimOptions::default();
45
46		// -- Check
47		assert!(!opts.indent_with_tabs, "indent_with_tabs should default to false");
48		assert!(opts.indent.is_none(), "indent should default to None");
49
50		Ok(())
51	}
52
53	#[test]
54	fn test_slim_options_with_indent() -> TestResult<()> {
55		// -- Setup & Fixtures
56		// -- Exec
57		let opts = SlimOptions::default().with_indent(4);
58
59		// -- Check
60		assert_eq!(opts.indent, Some(4));
61		assert!(!opts.indent_with_tabs);
62
63		Ok(())
64	}
65
66	#[test]
67	fn test_slim_options_with_indent_with_tabs() -> TestResult<()> {
68		// -- Setup & Fixtures
69		// -- Exec
70		let opts = SlimOptions::default().with_indent_with_tabs(true);
71
72		// -- Check
73		assert!(opts.indent_with_tabs);
74		assert!(opts.indent.is_none());
75
76		Ok(())
77	}
78
79	#[test]
80	fn test_slim_options_combined() -> TestResult<()> {
81		// -- Setup & Fixtures
82		// -- Exec
83		let opts = SlimOptions::default().with_indent(2).with_indent_with_tabs(true);
84
85		// -- Check
86		assert_eq!(opts.indent, Some(2));
87		assert!(opts.indent_with_tabs);
88
89		Ok(())
90	}
91}
92
93// endregion: --- Tests