pretty_print/providers/
mod.rs

1use crate::{PrettyPrint, PrettyTree};
2use alloc::{borrow::Cow, rc::Rc};
3use color_ansi::AnsiStyle;
4use core::fmt::{Debug, Formatter};
5
6/// Represents a pretty-printable tree provider.
7pub struct PrettyProvider {
8    width: usize,
9    keyword: Rc<AnsiStyle>,
10    string: Rc<AnsiStyle>,
11    number: Rc<AnsiStyle>,
12    macros: Rc<AnsiStyle>,
13    argument: Rc<AnsiStyle>,
14    argument_mut: Rc<AnsiStyle>,
15    local: Rc<AnsiStyle>,
16    local_mut: Rc<AnsiStyle>,
17    operator: Rc<AnsiStyle>,
18    structure: Rc<AnsiStyle>,
19    variant: Rc<AnsiStyle>,
20    interface: Rc<AnsiStyle>,
21}
22
23impl Debug for PrettyProvider {
24    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
25        f.debug_struct("PrettyProvider").finish()
26    }
27}
28
29impl PrettyProvider {
30    /// Creates a new pretty-printable tree provider.
31    pub fn new(width: usize) -> Self {
32        Self {
33            width,
34            keyword: AnsiStyle::rgb(197, 119, 207).into(),
35            string: AnsiStyle::rgb(152, 195, 121).into(),
36            number: AnsiStyle::rgb(206, 153, 100).into(),
37            macros: AnsiStyle::rgb(87, 182, 194).into(),
38            argument: AnsiStyle::rgb(239, 112, 117).into(),
39            argument_mut: AnsiStyle::rgb(239, 112, 117).with_underline().into(),
40            local: AnsiStyle::rgb(152, 195, 121).into(),
41            local_mut: AnsiStyle::rgb(152, 195, 121).with_underline().into(),
42            operator: AnsiStyle::rgb(90, 173, 238).into(),
43            structure: AnsiStyle::rgb(197, 119, 207).into(),
44            variant: AnsiStyle::rgb(239, 112, 117).into(),
45            interface: AnsiStyle::rgb(197, 119, 207).into(),
46        }
47    }
48}
49
50impl PrettyProvider {
51    /// Gets the width of the document.
52    pub fn get_width(&self) -> usize {
53        self.width
54    }
55    /// Sets the width of the document.
56    pub fn set_width(&mut self, width: usize) {
57        self.width = width;
58    }
59    /// Gets the width of the document.
60    pub fn text<S>(&self, text: S) -> PrettyTree
61    where
62        S: Into<Cow<'static, str>>,
63    {
64        PrettyTree::text(text)
65    }
66    /// Gets the width of the document.
67    pub fn custom<S>(&self, text: S, style: Rc<AnsiStyle>) -> PrettyTree
68    where
69        S: Into<Cow<'static, str>>,
70    {
71        PrettyTree::text(text).annotate(style)
72    }
73    /// Allocate a document containing the given text.
74    pub fn keyword<S>(&self, text: S) -> PrettyTree
75    where
76        S: Into<Cow<'static, str>>,
77    {
78        PrettyTree::text(text).annotate(self.keyword.clone())
79    }
80    /// Allocate a document containing the given text.
81    pub fn identifier<S>(&self, text: S) -> PrettyTree
82    where
83        S: Into<Cow<'static, str>>,
84    {
85        PrettyTree::text(text).annotate(self.operator.clone())
86    }
87    /// Allocate a document containing the given text.
88    pub fn generic<S>(&self, text: S) -> PrettyTree
89    where
90        S: Into<Cow<'static, str>>,
91    {
92        PrettyTree::text(text).annotate(self.macros.clone())
93    }
94
95    /// Allocate a document containing the given text.
96    pub fn variable<S>(&self, text: S, mutable: bool) -> PrettyTree
97    where
98        S: Into<Cow<'static, str>>,
99    {
100        if mutable {
101            PrettyTree::text(text).annotate(self.local_mut.clone())
102        }
103        else {
104            PrettyTree::text(text).annotate(self.local.clone())
105        }
106    }
107    /// Allocate a document containing the given text.
108    pub fn argument<S>(&self, text: S, mutable: bool) -> PrettyTree
109    where
110        S: Into<Cow<'static, str>>,
111    {
112        if mutable {
113            PrettyTree::text(text).annotate(self.argument_mut.clone())
114        }
115        else {
116            PrettyTree::text(text).annotate(self.argument.clone())
117        }
118    }
119    /// Allocate a document containing the given text.
120    pub fn operator<S>(&self, text: S) -> PrettyTree
121    where
122        S: Into<Cow<'static, str>>,
123    {
124        PrettyTree::text(text).annotate(self.operator.clone())
125    }
126    /// Allocate a document containing the given text.
127    pub fn string<S>(&self, text: S) -> PrettyTree
128    where
129        S: Into<Cow<'static, str>>,
130    {
131        PrettyTree::text(text).annotate(self.string.clone())
132    }
133    /// Allocate a document containing the given text.
134    pub fn annotation<S>(&self, text: S) -> PrettyTree
135    where
136        S: Into<Cow<'static, str>>,
137    {
138        PrettyTree::text(text).annotate(self.macros.clone())
139    }
140    /// Allocate a document containing the given text.
141    pub fn number<S>(&self, text: S) -> PrettyTree
142    where
143        S: Into<Cow<'static, str>>,
144    {
145        PrettyTree::text(text).annotate(self.number.clone())
146    }
147    /// Allocate a document containing the given text.
148    pub fn structure<S>(&self, text: S) -> PrettyTree
149    where
150        S: Into<Cow<'static, str>>,
151    {
152        PrettyTree::text(text).annotate(self.structure.clone())
153    }
154    /// Allocate a document containing the given text.
155    pub fn variant<S>(&self, text: S) -> PrettyTree
156    where
157        S: Into<Cow<'static, str>>,
158    {
159        PrettyTree::text(text).annotate(self.variant.clone())
160    }
161
162    /// Allocate a document containing the given text.
163    pub fn interface<S>(&self, text: S) -> PrettyTree
164    where
165        S: Into<Cow<'static, str>>,
166    {
167        PrettyTree::text(text).annotate(self.interface.clone())
168    }
169}
170
171impl PrettyProvider {
172    /// Allocate a document containing the given text.
173    ///
174    /// # Examples
175    ///
176    /// ```
177    /// # use pretty_print::PrettyProvider;
178    /// let theme = PrettyProvider::new(80);
179    /// theme.join(vec!["a", "b", "c"], ", ");
180    /// ```
181    pub fn join<I, T1, T2>(&self, iter: I, joint: T2) -> PrettyTree
182    where
183        I: IntoIterator<Item = T1>,
184        T1: PrettyPrint,
185        T2: PrettyPrint,
186    {
187        PrettyTree::join(iter.into_iter().map(|x| x.pretty(self)), joint.pretty(self))
188    }
189    /// Allocate a document containing the given text.
190    ///
191    /// # Examples
192    ///
193    /// ```
194    /// # use pretty_print::PrettyProvider;
195    /// let theme = PrettyProvider::new(80);
196    /// theme.join(&["a", "b", "c"], ", ");
197    /// ```
198    pub fn join_slice<I, T>(&self, iter: &[I], joint: T) -> PrettyTree
199    where
200        I: PrettyPrint,
201        T: PrettyPrint,
202    {
203        PrettyTree::join(iter.iter().map(|s| s.pretty(self)), joint.pretty(self))
204    }
205    /// Allocate a document containing the given text.
206    ///
207    /// # Examples
208    ///
209    /// ```
210    /// # use pretty_print::PrettyProvider;
211    /// let theme = PrettyProvider::new(80);
212    /// theme.concat(vec!["1", "2", "3"]);
213    /// ```
214    pub fn concat<I, T>(&self, iter: I) -> PrettyTree
215    where
216        I: IntoIterator<Item = T>,
217        T: PrettyPrint,
218    {
219        PrettyTree::concat(iter.into_iter().map(|x| x.pretty(self)))
220    }
221    /// Allocate a document containing the given text.
222    ///
223    /// # Examples
224    ///
225    /// ```
226    /// # use pretty_print::PrettyProvider;
227    /// let theme = PrettyProvider::new(80);
228    /// theme.concat_slice(&["1", "2", "3"]);
229    /// ```
230    pub fn concat_slice<T>(&self, iter: &[T]) -> PrettyTree
231    where
232        T: PrettyPrint,
233    {
234        PrettyTree::concat(iter.iter().map(|s| s.pretty(self)))
235    }
236}