1use contextual::{Contextual, WithContext};
2use std::collections::HashSet;
3use std::fmt;
4
5use super::{Options, Size};
6
7pub trait PrintWithContext<C> {
8 fn contextual_fmt_with(
9 &self,
10 context: &C,
11 f: &mut fmt::Formatter,
12 options: &Options,
13 indent: usize,
14 ) -> fmt::Result;
15}
16
17impl<'a, T: PrintWithContext<C> + ?Sized, C> PrintWithContext<C> for &'a T {
18 fn contextual_fmt_with(
19 &self,
20 context: &C,
21 f: &mut fmt::Formatter,
22 options: &Options,
23 indent: usize,
24 ) -> fmt::Result {
25 T::contextual_fmt_with(*self, context, f, options, indent)
26 }
27}
28
29impl<'c, T: PrintWithContext<C>, C> super::Print for Contextual<T, &'c C> {
30 fn fmt_with(&self, f: &mut fmt::Formatter, options: &Options, indent: usize) -> fmt::Result {
31 self.0.contextual_fmt_with(self.1, f, options, indent)
32 }
33}
34
35pub trait PrintWithSizeAndContext<C> {
36 fn contextual_fmt_with_size(
37 &self,
38 context: &C,
39 f: &mut std::fmt::Formatter,
40 options: &Options,
41 indent: usize,
42 sizes: &[Size],
43 index: &mut usize,
44 ) -> std::fmt::Result;
45}
46
47impl<'a, T: PrintWithSizeAndContext<C> + ?Sized, C> PrintWithSizeAndContext<C> for &'a T {
48 fn contextual_fmt_with_size(
49 &self,
50 context: &C,
51 f: &mut std::fmt::Formatter,
52 options: &Options,
53 indent: usize,
54 sizes: &[Size],
55 index: &mut usize,
56 ) -> std::fmt::Result {
57 T::contextual_fmt_with_size(*self, context, f, options, indent, sizes, index)
58 }
59}
60
61impl<'c, T: PrintWithSizeAndContext<C>, C> super::PrintWithSize for Contextual<T, &'c C> {
62 fn fmt_with_size(
63 &self,
64 f: &mut std::fmt::Formatter,
65 options: &Options,
66 indent: usize,
67 sizes: &[Size],
68 index: &mut usize,
69 ) -> std::fmt::Result {
70 self.0
71 .contextual_fmt_with_size(self.1, f, options, indent, sizes, index)
72 }
73}
74
75pub trait PrecomputeSizeWithContext<C> {
76 fn contextual_pre_compute_size(
77 &self,
78 context: &C,
79 options: &Options,
80 sizes: &mut Vec<Size>,
81 ) -> Size;
82}
83
84impl<'a, T: PrecomputeSizeWithContext<C> + ?Sized, C> PrecomputeSizeWithContext<C> for &'a T {
85 fn contextual_pre_compute_size(
86 &self,
87 context: &C,
88 options: &Options,
89 sizes: &mut Vec<Size>,
90 ) -> Size {
91 T::contextual_pre_compute_size(*self, context, options, sizes)
92 }
93}
94
95impl<'c, T: PrecomputeSizeWithContext<C>, C> super::PrecomputeSize for Contextual<T, &'c C> {
96 fn pre_compute_size(&self, options: &Options, sizes: &mut Vec<Size>) -> Size {
97 self.0.contextual_pre_compute_size(self.1, options, sizes)
98 }
99}
100
101impl<T: PrecomputeSizeWithContext<C>, M, C> PrecomputeSizeWithContext<C> for locspan::Meta<T, M> {
102 fn contextual_pre_compute_size(
103 &self,
104 context: &C,
105 options: &Options,
106 sizes: &mut Vec<Size>,
107 ) -> Size {
108 self.0.contextual_pre_compute_size(context, options, sizes)
109 }
110}
111
112impl<T: PrintWithSizeAndContext<C>, M, C> PrintWithSizeAndContext<C> for locspan::Meta<T, M> {
113 fn contextual_fmt_with_size(
114 &self,
115 context: &C,
116 f: &mut std::fmt::Formatter,
117 options: &Options,
118 indent: usize,
119 sizes: &[Size],
120 index: &mut usize,
121 ) -> std::fmt::Result {
122 self.0
123 .contextual_fmt_with_size(context, f, options, indent, sizes, index)
124 }
125}
126
127impl<T: PrintWithContext<C>, M, C> PrintWithContext<C> for locspan::Meta<T, M> {
128 fn contextual_fmt_with(
129 &self,
130 context: &C,
131 f: &mut std::fmt::Formatter,
132 options: &Options,
133 indent: usize,
134 ) -> std::fmt::Result {
135 self.0.contextual_fmt_with(context, f, options, indent)
136 }
137}
138
139impl<T: PrecomputeSizeWithContext<C>, C> PrecomputeSizeWithContext<C> for locspan::Stripped<T> {
140 fn contextual_pre_compute_size(
141 &self,
142 context: &C,
143 options: &Options,
144 sizes: &mut Vec<Size>,
145 ) -> Size {
146 self.0.contextual_pre_compute_size(context, options, sizes)
147 }
148}
149
150impl<T: PrintWithSizeAndContext<C>, C> PrintWithSizeAndContext<C> for locspan::Stripped<T> {
151 fn contextual_fmt_with_size(
152 &self,
153 context: &C,
154 f: &mut std::fmt::Formatter,
155 options: &Options,
156 indent: usize,
157 sizes: &[Size],
158 index: &mut usize,
159 ) -> std::fmt::Result {
160 self.0
161 .contextual_fmt_with_size(context, f, options, indent, sizes, index)
162 }
163}
164
165impl<T: PrintWithContext<C>, C> PrintWithContext<C> for locspan::Stripped<T> {
166 fn contextual_fmt_with(
167 &self,
168 context: &C,
169 f: &mut std::fmt::Formatter,
170 options: &Options,
171 indent: usize,
172 ) -> std::fmt::Result {
173 self.0.contextual_fmt_with(context, f, options, indent)
174 }
175}
176
177impl<T: PrecomputeSizeWithContext<C>, C> PrecomputeSizeWithContext<C> for [T] {
178 fn contextual_pre_compute_size(
179 &self,
180 context: &C,
181 options: &Options,
182 sizes: &mut Vec<Size>,
183 ) -> Size {
184 super::pre_compute_array_size(self.iter().map(|i| i.with(context)), options, sizes)
185 }
186}
187
188impl<T: PrintWithSizeAndContext<C>, C> PrintWithSizeAndContext<C> for [T] {
189 fn contextual_fmt_with_size(
190 &self,
191 context: &C,
192 f: &mut std::fmt::Formatter,
193 options: &Options,
194 indent: usize,
195 sizes: &[Size],
196 index: &mut usize,
197 ) -> std::fmt::Result {
198 super::print_array(
199 self.iter().map(|i| i.with(context)),
200 f,
201 options,
202 indent,
203 sizes,
204 index,
205 )
206 }
207}
208
209impl<T: PrecomputeSizeWithContext<C>, C> PrecomputeSizeWithContext<C> for HashSet<T> {
210 fn contextual_pre_compute_size(
211 &self,
212 context: &C,
213 options: &Options,
214 sizes: &mut Vec<Size>,
215 ) -> Size {
216 super::pre_compute_array_size(self.iter().map(|i| i.with(context)), options, sizes)
217 }
218}
219
220impl<T: PrintWithSizeAndContext<C>, C> PrintWithSizeAndContext<C> for HashSet<T> {
221 fn contextual_fmt_with_size(
222 &self,
223 context: &C,
224 f: &mut std::fmt::Formatter,
225 options: &Options,
226 indent: usize,
227 sizes: &[Size],
228 index: &mut usize,
229 ) -> std::fmt::Result {
230 super::print_array(
231 self.iter().map(|i| i.with(context)),
232 f,
233 options,
234 indent,
235 sizes,
236 index,
237 )
238 }
239}