1use crate::{PrettyPrint, PrettyTree, Text};
2use alloc::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<'a, S, T>(&self, text: S) -> PrettyTree<'a, T>
61 where
62 S: Into<T>,
63 {
64 PrettyTree::text(text)
65 }
66 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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}