Skip to main content

logparse_pretty_print/providers/
mod.rs

1use crate::{PrettyPrint, PrettyTree, Text};
2use alloc::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<'a, S, T>(&self, text: S) -> PrettyTree<'a, T>
61    where
62        S: Into<T>,
63    {
64        PrettyTree::text(text)
65    }
66    /// Gets the width of the document.
67    pub fn custom<'a, S, T: Text<'a>>(&self, text: S, style: Rc<AnsiStyle>) -> PrettyTree<'a, T>
68    where
69        S: Into<T>,
70    {
71        PrettyTree::text(text).annotate(style)
72    }
73    /// Allocate a document containing the given text.
74    pub fn keyword<'a, S, T: Text<'a>>(&self, text: S) -> PrettyTree<'a, T>
75    where
76        S: Into<T>,
77    {
78        PrettyTree::text(text).annotate(self.keyword.clone())
79    }
80    /// Allocate a document containing the given text.
81    pub fn identifier<'a, S, T: Text<'a>>(&self, text: S) -> PrettyTree<'a, T>
82    where
83        S: Into<T>,
84    {
85        PrettyTree::text(text).annotate(self.operator.clone())
86    }
87    /// Allocate a document containing the given text.
88    pub fn generic<'a, S, T: Text<'a>>(&self, text: S) -> PrettyTree<'a, T>
89    where
90        S: Into<T>,
91    {
92        PrettyTree::text(text).annotate(self.macros.clone())
93    }
94
95    /// Allocate a document containing the given text.
96    pub fn variable<'a, S, T: Text<'a>>(&self, text: S, mutable: bool) -> PrettyTree<'a, T>
97    where
98        S: Into<T>,
99    {
100        if mutable {
101            PrettyTree::text(text).annotate(self.local_mut.clone())
102        } else {
103            PrettyTree::text(text).annotate(self.local.clone())
104        }
105    }
106    /// Allocate a document containing the given text.
107    pub fn argument<'a, S, T: Text<'a>>(&self, text: S, mutable: bool) -> PrettyTree<'a, T>
108    where
109        S: Into<T>,
110    {
111        if mutable {
112            PrettyTree::text(text).annotate(self.argument_mut.clone())
113        } else {
114            PrettyTree::text(text).annotate(self.argument.clone())
115        }
116    }
117    /// Allocate a document containing the given text.
118    pub fn operator<'a, S, T: Text<'a>>(&self, text: S) -> PrettyTree<'a, T>
119    where
120        S: Into<T>,
121    {
122        PrettyTree::text(text).annotate(self.operator.clone())
123    }
124    /// Allocate a document containing the given text.
125    pub fn string<'a, S, T: Text<'a>>(&self, text: S) -> PrettyTree<'a, T>
126    where
127        S: Into<T>,
128    {
129        PrettyTree::text(text).annotate(self.string.clone())
130    }
131    /// Allocate a document containing the given text.
132    pub fn annotation<'a, S, T: Text<'a>>(&self, text: S) -> PrettyTree<'a, T>
133    where
134        S: Into<T>,
135    {
136        PrettyTree::text(text).annotate(self.macros.clone())
137    }
138    /// Allocate a document containing the given text.
139    pub fn number<'a, S, T: Text<'a>>(&self, text: S) -> PrettyTree<'a, T>
140    where
141        S: Into<T>,
142    {
143        PrettyTree::text(text).annotate(self.number.clone())
144    }
145    /// Allocate a document containing the given text.
146    pub fn structure<'a, S, T: Text<'a>>(&self, text: S) -> PrettyTree<'a, T>
147    where
148        S: Into<T>,
149    {
150        PrettyTree::text(text).annotate(self.structure.clone())
151    }
152    /// Allocate a document containing the given text.
153    pub fn variant<'a, S, T: Text<'a>>(&self, text: S) -> PrettyTree<'a, T>
154    where
155        S: Into<T>,
156    {
157        PrettyTree::text(text).annotate(self.variant.clone())
158    }
159
160    /// Allocate a document containing the given text.
161    pub fn interface<'a, S, T: Text<'a>>(&self, text: S) -> PrettyTree<'a, T>
162    where
163        S: Into<T>,
164    {
165        PrettyTree::text(text).annotate(self.interface.clone())
166    }
167}
168
169impl PrettyProvider {
170    /// Allocate a document containing the given text.
171    ///
172    /// # Examples
173    ///
174    /// ```
175    /// # use pretty_print::PrettyProvider;
176    /// let theme = PrettyProvider::new(80);
177    /// theme.join(vec!["a", "b", "c"], ", ");
178    /// ```
179    pub fn join<'a, I, T1, T2, T: Text<'a>>(&self, iter: I, joint: T2) -> PrettyTree<'a, T>
180    where
181        I: IntoIterator<Item = T1>,
182        T1: PrettyPrint<'a, T>,
183        T2: PrettyPrint<'a, T>,
184    {
185        PrettyTree::join(iter.into_iter().map(|x| x.pretty(self)), joint.pretty(self))
186    }
187    /// Allocate a document containing the given text.
188    ///
189    /// # Examples
190    ///
191    /// ```
192    /// # use pretty_print::PrettyProvider;
193    /// let theme = PrettyProvider::new(80);
194    /// theme.join(&["a", "b", "c"], ", ");
195    /// ```
196    pub fn join_slice<'a, I, U, T: Text<'a>>(&self, iter: &[I], joint: U) -> PrettyTree<'a, T>
197    where
198        I: PrettyPrint<'a, T>,
199        U: PrettyPrint<'a, T>,
200    {
201        PrettyTree::join(iter.iter().map(|s| s.pretty(self)), joint.pretty(self))
202    }
203    /// Allocate a document containing the given text.
204    ///
205    /// # Examples
206    ///
207    /// ```
208    /// # use pretty_print::PrettyProvider;
209    /// let theme = PrettyProvider::new(80);
210    /// theme.concat(vec!["1", "2", "3"]);
211    /// ```
212    pub fn concat<'a, I, U, T: Text<'a>>(&self, iter: I) -> PrettyTree<'a, T>
213    where
214        I: IntoIterator<Item = U>,
215        U: PrettyPrint<'a, T>,
216    {
217        PrettyTree::concat(iter.into_iter().map(|x| x.pretty(self)))
218    }
219    /// Allocate a document containing the given text.
220    ///
221    /// # Examples
222    ///
223    /// ```
224    /// # use pretty_print::PrettyProvider;
225    /// let theme = PrettyProvider::new(80);
226    /// theme.concat_slice(&["1", "2", "3"]);
227    /// ```
228    pub fn concat_slice<'a, U, T: Text<'a>>(&self, iter: &[U]) -> PrettyTree<'a, T>
229    where
230        U: PrettyPrint<'a, T>,
231    {
232        PrettyTree::concat(iter.iter().map(|s| s.pretty(self)))
233    }
234}