Skip to main content

prettyless/render/
options.rs

1/// Configuration for a render pass.
2///
3/// Carries everything a render call needs beyond the document itself, so new controls
4/// can be added without introducing another family of methods. Pass one to any render
5/// method of [`crate::Doc`]:
6///
7/// ```
8/// use prettyless::{Doc, BoxDoc, LineEnding, RenderOptions};
9/// let doc = BoxDoc::text("a").append(Doc::line()).append(Doc::text("b"));
10/// let options = RenderOptions::new(1).with_line_ending(LineEnding::Crlf);
11///
12/// let mut out = String::new();
13/// doc.render_fmt_with(options, &mut out).unwrap();
14/// assert_eq!(out, "a\r\nb");
15/// ```
16#[derive(Clone, Copy, Debug)]
17#[must_use]
18#[non_exhaustive]
19pub struct RenderOptions {
20    /// The target line width.
21    ///
22    /// Layout aims to keep lines within this width, but unbreakable text and line
23    /// suffixes can exceed it.
24    pub width: usize,
25    /// The terminator emitted for structural breaks.
26    pub line_ending: LineEnding,
27    /// When a structural break writes its indentation.
28    pub indentation_policy: IndentationPolicy,
29}
30
31impl RenderOptions {
32    /// Creates options with a target line width of `width`, [`LineEnding::Lf`] breaks,
33    /// and [`IndentationPolicy::Eager`] indentation.
34    #[inline]
35    pub const fn new(width: usize) -> Self {
36        Self {
37            width,
38            line_ending: LineEnding::Lf,
39            indentation_policy: IndentationPolicy::Eager,
40        }
41    }
42
43    /// Replaces the target line width, keeping the other options.
44    #[inline]
45    pub const fn with_width(mut self, width: usize) -> Self {
46        self.width = width;
47        self
48    }
49
50    /// Sets the terminator emitted for structural breaks.
51    #[inline]
52    pub const fn with_line_ending(mut self, ending: LineEnding) -> Self {
53        self.line_ending = ending;
54        self
55    }
56
57    /// Sets when a structural break writes its indentation.
58    #[inline]
59    pub const fn with_indentation_policy(mut self, policy: IndentationPolicy) -> Self {
60        self.indentation_policy = policy;
61        self
62    }
63}
64
65/// The line terminator emitted for document line breaks when rendering.
66///
67/// Selected through [`RenderOptions`]. It applies to every structural document break,
68/// including breaks emitted inside line suffixes and union branches. Text is written
69/// verbatim, so newlines embedded in text (`Doc::text("a\nb")`) and the terminators
70/// inside a [`Doc::MultilineText`](crate::Doc::MultilineText) payload are never
71/// normalized, and the terminator length never affects fitting, indentation, or
72/// contextual column values. [`LineEnding::Lf`] is the default.
73#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
74pub enum LineEnding {
75    /// A single line feed, `\n`.
76    #[default]
77    Lf,
78    /// Carriage return + line feed, `\r\n`.
79    Crlf,
80}
81
82impl LineEnding {
83    /// The terminator string.
84    #[inline]
85    pub fn as_str(self) -> &'static str {
86        match self {
87            LineEnding::Lf => "\n",
88            LineEnding::Crlf => "\r\n",
89        }
90    }
91}
92
93/// When a structural break writes its indentation.
94///
95/// Selected through [`RenderOptions`]. The policy changes only *when* the
96/// indentation of a break is written; the break itself, the indentation it
97/// selects, blank-line counts, fitting, and contextual columns are unaffected.
98/// [`IndentationPolicy::Eager`] is the default.
99#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
100pub enum IndentationPolicy {
101    /// Writes a break's indentation immediately after its terminator.
102    ///
103    /// Blank lines and a trailing break therefore carry trailing spaces. This is
104    /// the behavior of upstream `pretty` and the default.
105    #[default]
106    Eager,
107    /// Postpones a break's indentation until content on the new line commits it.
108    ///
109    /// Blank lines and a trailing break emit no indentation. Nonempty text and a
110    /// line suffix commit pending indentation; empty text does not. A later break
111    /// replaces pending indentation and the end of output discards it.
112    Deferred,
113}