tiny_pretty/
options.rs

1#[derive(Clone, Debug, Default)]
2pub enum LineBreak {
3    #[default]
4    Lf,
5    Crlf,
6}
7
8#[derive(Clone, Debug, Default)]
9pub enum IndentKind {
10    #[default]
11    Space,
12    Tab,
13}
14
15#[derive(Clone, Debug)]
16/// Print control options, such as line break and indentation kind.
17pub struct PrintOptions {
18    /// Line break for each line. It can be "\n" (LF) or "\r\n" (CRLF).
19    ///
20    /// Default value is LF.
21    ///
22    /// ```
23    /// use tiny_pretty::{print, Doc, LineBreak, PrintOptions};
24    ///
25    /// let doc = Doc::list(vec![Doc::text("a"), Doc::hard_line(), Doc::text("b")]);
26    ///
27    /// assert_eq!("a\nb", &print(&doc, &PrintOptions {
28    ///     line_break: LineBreak::Lf,
29    ///     ..Default::default()
30    /// }));
31    ///
32    /// assert_eq!("a\r\nb", &print(&doc, &PrintOptions {
33    ///     line_break: LineBreak::Crlf,
34    ///     ..Default::default()
35    /// }));
36    /// ```
37    pub line_break: LineBreak,
38
39    /// To use space or tab for indentation.
40    ///
41    /// Note that when using tabs and calling [`nest`](crate::Doc::nest) with offset,
42    /// it doesn't mean it will print tabs with the number of offset.
43    /// Of course, it will print tabs as possible, however if `indent % tab_size != 0`,
44    /// it will print tabs first and then fill with spaces to match indentation.
45    /// Specifically, it prints `indent / tab_size` times tabs
46    /// then prints `indent % tab_size` times spaces.
47    /// See the documentation and examples of the [`tab_size`](PrintOptions::tab_size) option below.
48    ///
49    /// Default value is space.
50    ///
51    /// ```
52    /// use tiny_pretty::{print, Doc, IndentKind, PrintOptions};
53    ///
54    /// let doc = Doc::list(vec![Doc::text("a"), Doc::hard_line().nest(2), Doc::text("b")]);
55    ///
56    /// assert_eq!("a\n  b", &print(&doc, &PrintOptions {
57    ///     indent_kind: IndentKind::Space,
58    ///     ..Default::default()
59    /// }));
60    ///
61    /// assert_eq!("a\n\tb", &print(&doc, &PrintOptions {
62    ///     indent_kind: IndentKind::Tab,
63    ///     ..Default::default()
64    /// }));
65    /// ```
66    pub indent_kind: IndentKind,
67
68    /// The limitation that pretty printer should *(but not must)* avoid columns exceeding.
69    /// Pretty printer will try its best to keep column width less than this value,
70    /// but it may exceed for some cases, for example, a very very long single word.
71    ///
72    /// Default value is 80.
73    ///
74    /// ```
75    /// use tiny_pretty::{print, Doc, PrintOptions};
76    ///
77    /// let doc = Doc::list(vec![Doc::text("aaaa"), Doc::line_or_space(), Doc::text("bbbb")]).group();
78    /// assert_eq!("aaaa\nbbbb", &print(&doc, &PrintOptions {
79    ///     width: 5,
80    ///     ..Default::default()
81    /// }));
82    ///
83    /// assert_eq!("aaaa bbbb", &print(&doc, &PrintOptions {
84    ///     width: 20,
85    ///     ..Default::default()
86    /// }));
87    ///
88    /// let doc = Doc::list(vec![Doc::text("aaaaaaaa"), Doc::line_or_space(), Doc::text("bbbbbbbb")])
89    ///     .group();
90    /// assert_eq!("aaaaaaaa\nbbbbbbbb", &print(&doc, &PrintOptions {
91    ///     width: 5,
92    ///     ..Default::default()
93    /// }));
94    /// ```
95    pub width: usize,
96
97    /// Tab size is not indentation size.
98    /// If `indent_kind` is set to `Tab`, when indentation level satisfies the `tab_size`,
99    /// it will convert those spaces to tabs.
100    ///
101    /// If you're implementing a high-level formatter or pretty printer,
102    /// it's highly recommended to set this value as same as indentation size of your
103    /// formatter or pretty printer.
104    ///
105    /// Default value is 2. It can't be zero.
106    /// This option will be ignored when `indent_kind` is `Space`.
107    ///
108    /// ```
109    /// use tiny_pretty::{print, Doc, IndentKind, PrintOptions};
110    ///
111    /// let doc = Doc::list(vec![Doc::text("aaaa"), Doc::hard_line(), Doc::text("bbbb")])
112    ///     .group()
113    ///     .nest(8);
114    ///
115    /// assert_eq!("aaaa\n\t   bbbb", &print(&doc, &PrintOptions {
116    ///     indent_kind: IndentKind::Tab,
117    ///     tab_size: 5,
118    ///     ..Default::default()
119    /// }));
120    ///
121    /// assert_eq!("aaaa\n\t\tbbbb", &print(&doc, &PrintOptions {
122    ///     indent_kind: IndentKind::Tab,
123    ///     tab_size: 4,
124    ///     ..Default::default()
125    /// }));
126    ///
127    /// assert_eq!("aaaa\n        bbbb", &print(&doc, &PrintOptions {
128    ///     indent_kind: IndentKind::Space,
129    ///     tab_size: 5,
130    ///     ..Default::default()
131    /// }));
132    /// ```
133    pub tab_size: usize,
134}
135
136impl Default for PrintOptions {
137    fn default() -> Self {
138        Self {
139            line_break: Default::default(),
140            indent_kind: Default::default(),
141            width: 80,
142            tab_size: 2,
143        }
144    }
145}