1use crate::{PrettyPrint, PrettyTree};
2use alloc::{borrow::Cow, rc::Rc};
3use color_ansi::AnsiStyle;
4use core::fmt::{Debug, Formatter};
5
6pub 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 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 pub fn get_width(&self) -> usize {
53 self.width
54 }
55 pub fn set_width(&mut self, width: usize) {
57 self.width = width;
58 }
59 pub fn text<S>(&self, text: S) -> PrettyTree
61 where
62 S: Into<Cow<'static, str>>,
63 {
64 PrettyTree::text(text)
65 }
66 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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}