styx_format/options.rs
1//! Formatting options for Styx serialization.
2
3/// Restrict formatting to a kind.
4#[derive(Debug, Clone, Eq, PartialEq)]
5pub enum ForceStyle {
6 None,
7
8 /// Force all objects to use comma separators
9 Inline,
10
11 /// Force all objects to use newline separators
12 Multiline,
13}
14
15/// Options for Styx serialization.
16#[derive(Debug, Clone)]
17pub struct FormatOptions {
18 /// Indentation string (default: " " - 4 spaces)
19 pub indent: &'static str,
20
21 /// Max line width before wrapping (default: 80)
22 pub max_width: usize,
23
24 /// Minimum available width to even consider inline (default: 30)
25 /// If depth eats into max_width below this, force multi-line
26 pub min_inline_width: usize,
27
28 /// Inline objects with ≤ N entries (default: 4)
29 pub inline_object_threshold: usize,
30
31 /// Inline sequences with ≤ N items (default: 8)
32 pub inline_sequence_threshold: usize,
33
34 /// Use heredocs for strings with > N lines (default: 2)
35 pub heredoc_line_threshold: usize,
36
37 pub force_style: ForceStyle,
38}
39
40impl Default for FormatOptions {
41 fn default() -> Self {
42 Self {
43 indent: " ",
44 max_width: 80,
45 min_inline_width: 30,
46 inline_object_threshold: 4,
47 inline_sequence_threshold: 8,
48 heredoc_line_threshold: 2,
49 force_style: ForceStyle::None,
50 }
51 }
52}
53
54impl FormatOptions {
55 /// Create new default options.
56 pub fn new() -> Self {
57 Self::default()
58 }
59
60 /// Force all output to be multi-line (newline separators).
61 pub fn multiline(mut self) -> Self {
62 self.force_style = ForceStyle::Multiline;
63 self
64 }
65
66 /// Force all output to be inline (comma separators, single line).
67 pub fn inline(mut self) -> Self {
68 self.force_style = ForceStyle::Inline;
69 self
70 }
71
72 /// Set a custom indentation string.
73 pub fn indent(mut self, indent: &'static str) -> Self {
74 self.indent = indent;
75 self
76 }
77
78 /// Set max line width.
79 pub fn max_width(mut self, width: usize) -> Self {
80 self.max_width = width;
81 self
82 }
83}