1use super::{Output, SequenceAccept, Writable, WritableSeq};
27
28#[derive(Copy, Clone, Debug)]
30pub struct NoOp;
31
32impl<O> Writable<O> for NoOp
33where
34 O: Output,
35{
36 async fn write_to(&self, _: &mut O) -> Result<(), O::Error> {
37 Ok(())
38 }
39}
40
41#[derive(Copy, Clone, Debug)]
43pub struct NoOpSeq;
44
45impl<O> WritableSeq<O> for NoOpSeq
46where
47 O: Output,
48{
49 async fn for_each<S: SequenceAccept<O>>(&self, _: &mut S) -> Result<(), O::Error> {
50 Ok(())
51 }
52}
53
54#[derive(Clone, Debug)]
56pub struct Combined<W1, W2>(pub W1, pub W2);
57
58impl<O, W1, W2> Writable<O> for Combined<W1, W2>
59where
60 O: Output,
61 W1: Writable<O>,
62 W2: Writable<O>,
63{
64 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
65 self.0.write_to(output).await?;
66 self.1.write_to(output).await
67 }
68}
69
70#[derive(Clone, Debug)]
72pub struct CombinedSeq<S1, S2>(pub S1, pub S2);
73
74impl<O, S1, S2> WritableSeq<O> for CombinedSeq<S1, S2>
75where
76 O: Output,
77 S1: WritableSeq<O>,
78 S2: WritableSeq<O>,
79{
80 async fn for_each<S>(&self, sink: &mut S) -> Result<(), O::Error>
81 where
82 S: SequenceAccept<O>,
83 {
84 self.0.for_each(sink).await?;
85 self.1.for_each(sink).await
86 }
87}
88
89#[derive(Clone, Debug)]
92pub struct SingularSeq<W>(pub W);
93
94impl<O, W> WritableSeq<O> for SingularSeq<W>
95where
96 O: Output,
97 W: Writable<O>,
98{
99 async fn for_each<S>(&self, sink: &mut S) -> Result<(), O::Error>
100 where
101 S: SequenceAccept<O>,
102 {
103 sink.accept(&self.0).await
104 }
105}
106
107#[derive(Clone, Debug)]
110pub struct RepeatSeq<W>(pub usize, pub W);
111
112impl<O, W> WritableSeq<O> for RepeatSeq<W>
113where
114 O: Output,
115 W: Writable<O>,
116{
117 async fn for_each<S>(&self, sink: &mut S) -> Result<(), O::Error>
118 where
119 S: SequenceAccept<O>,
120 {
121 for _ in 0..self.0 {
122 sink.accept(&self.1).await?;
123 }
124 Ok(())
125 }
126}
127
128#[derive(Debug)]
130pub struct ArrSeq<'a, W>(pub &'a [W]);
131
132impl<'a, W> Clone for ArrSeq<'a, W> {
133 fn clone(&self) -> Self {
134 Self(self.0)
135 }
136}
137
138impl<O, W> WritableSeq<O> for ArrSeq<'_, W>
139where
140 O: Output,
141 W: Writable<O>,
142{
143 async fn for_each<S>(&self, sink: &mut S) -> Result<(), O::Error>
144 where
145 S: SequenceAccept<O>,
146 {
147 for elem in self.0 {
148 sink.accept(elem).await?;
149 }
150 Ok(())
151 }
152}
153
154#[derive(Copy, Clone, Debug)]
157pub struct Str<STR>(pub STR);
158
159impl<O, STR> Writable<O> for Str<STR>
160where
161 O: Output,
162 STR: AsRef<str>,
163{
164 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
165 output.write(self.0.as_ref()).await
166 }
167}
168
169#[derive(Clone, Debug)]
171pub struct StrArrSeq<'a>(pub &'a [&'a str]);
172
173impl<O> WritableSeq<O> for StrArrSeq<'_>
174where
175 O: Output,
176{
177 async fn for_each<S>(&self, sink: &mut S) -> Result<(), O::Error>
178 where
179 S: SequenceAccept<O>,
180 {
181 for elem in self.0 {
182 sink.accept(&Str(*elem)).await?;
183 }
184 Ok(())
185 }
186}
187
188#[derive(Debug)]
191pub struct SeparatedSeqAccept<'o, O, Intro, Sep> {
192 pub output: &'o mut O,
194 pub intro: Intro,
196 pub separator: Sep,
198 pub wrote_any: bool,
200}
201
202impl<'o, O, Intro, Sep> SeparatedSeqAccept<'o, O, Intro, Sep> {
203 pub fn new(output: &'o mut O, intro: Intro, separator: Sep) -> Self {
204 Self {
205 output,
206 intro,
207 separator,
208 wrote_any: false,
209 }
210 }
211}
212
213impl<'o, O, Sep> SeparatedSeqAccept<'o, O, NoOp, Sep> {
214 pub fn no_intro(output: &'o mut O, separator: Sep) -> Self {
215 Self::new(output, NoOp, separator)
216 }
217}
218
219impl<'o, O> SeparatedSeqAccept<'o, O, NoOp, Str<&'static str>> {
220 pub fn comma_separated(output: &'o mut O) -> Self {
222 Self::new(output, NoOp, Str(", "))
223 }
224}
225
226impl<'o, O, Intro, Sep> SequenceAccept<O> for SeparatedSeqAccept<'o, O, Intro, Sep>
227where
228 O: Output,
229 Intro: Writable<O>,
230 Sep: Writable<O>,
231{
232 async fn accept<W>(&mut self, writable: &W) -> Result<(), O::Error>
233 where
234 W: Writable<O>,
235 {
236 if self.wrote_any {
237 self.separator.write_to(self.output).await?;
238 } else {
239 self.intro.write_to(self.output).await?;
240 self.wrote_any = true;
241 }
242 writable.write_to(self.output).await
243 }
244}
245
246#[derive(Debug)]
249pub struct SurroundingSeqAccept<'o, O, Before, After> {
250 pub output: &'o mut O,
251 pub before: Before,
252 pub after: After,
253}
254
255impl<'o, O, Before, After> SurroundingSeqAccept<'o, O, Before, After> {
256 pub fn new(output: &'o mut O, before: Before, after: After) -> Self {
257 Self {
258 output,
259 before,
260 after,
261 }
262 }
263}
264
265impl<'o, O, Before, After> SequenceAccept<O> for SurroundingSeqAccept<'o, O, Before, After>
266where
267 O: Output,
268 Before: Writable<O>,
269 After: Writable<O>,
270{
271 async fn accept<W>(&mut self, writable: &W) -> Result<(), O::Error>
272 where
273 W: Writable<O>,
274 {
275 self.before.write_to(self.output).await?;
276 writable.write_to(self.output).await?;
277 self.after.write_to(self.output).await
278 }
279}
280
281#[derive(Debug)]
284pub struct SequenceViaConfig<'t, T, Config> {
285 pub data: &'t [T],
286 pub config: Config,
287}
288
289impl<'t, T, Config> Clone for SequenceViaConfig<'t, T, Config>
290where
291 Config: Clone,
292{
293 fn clone(&self) -> Self {
294 Self {
295 data: self.data,
296 config: self.config.clone(),
297 }
298 }
299}
300
301pub trait SequenceConfig<T, O>
303where
304 O: Output,
305{
306 async fn write_datum(&self, datum: &T, output: &mut O) -> Result<(), O::Error>;
308}
309
310impl<'t, T, Config, O> WritableSeq<O> for SequenceViaConfig<'t, T, Config>
311where
312 O: Output,
313 Config: SequenceConfig<T, O>,
314{
315 async fn for_each<S>(&self, sink: &mut S) -> Result<(), O::Error>
316 where
317 S: SequenceAccept<O>,
318 {
319 struct WriteSingle<'t, T, Config> {
320 datum: &'t T,
321 config: &'t Config,
322 }
323 impl<'t, T, Config, O> Writable<O> for WriteSingle<'t, T, Config>
324 where
325 O: Output,
326 Config: SequenceConfig<T, O>,
327 {
328 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
329 self.config.write_datum(self.datum, output).await
330 }
331 }
332 for datum in self.data {
333 sink.accept(&WriteSingle {
334 datum,
335 config: &self.config,
336 })
337 .await?;
338 }
339 Ok(())
340 }
341}
342
343pub struct BoxedIndirection<W>(pub W);
348
349impl<O, W> Writable<O> for BoxedIndirection<W>
350where
351 O: Output,
352 W: Writable<O>,
353{
354 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
355 Box::pin(self.0.write_to(output)).await
356 }
357}
358
359impl<O, W> WritableSeq<O> for BoxedIndirection<W>
360where
361 O: Output,
362 W: WritableSeq<O>,
363{
364 async fn for_each<S>(&self, sink: &mut S) -> Result<(), O::Error>
365 where
366 S: SequenceAccept<O>,
367 {
368 Box::pin(self.0.for_each(sink)).await
369 }
370}