async_codegen/rust/
syntax.rs

1/*
2 * Copyright © 2025 Anand Beh
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use super::*;
18use crate::common::{SeparatedSeqAccept, Str};
19use crate::{Output, SequenceAccept, Writable, WritableSeq};
20
21impl<O, Lints> Writable<O> for AllowLints<Lints>
22where
23    O: Output,
24    Lints: WritableSeq<O>,
25{
26    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
27        let mut acceptor = SeparatedSeqAccept::new(output, Str("allow("), Str(","));
28        self.0.for_each(&mut acceptor).await?;
29        if acceptor.wrote_any {
30            output.write(")").await?;
31        }
32        Ok(())
33    }
34}
35
36impl<O> Writable<O> for ModPub
37where
38    O: Output,
39{
40    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
41        output.write("pub").await
42    }
43}
44
45impl<O, ABI> Writable<O> for ModExtern<ABI>
46where
47    O: Output,
48    ABI: Writable<O>,
49{
50    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
51        output.write("extern \"").await?;
52        self.0.write_to(output).await?;
53        output.write("\"").await
54    }
55}
56
57impl<O: Output, Expr> Writable<O> for UnsafeBlock<Expr>
58where
59    Expr: Writable<O>,
60{
61    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
62        o.write("unsafe {\n").await?;
63        self.0.write_to(o).await?;
64        o.write("\n}").await?;
65        Ok(())
66    }
67}
68
69impl<O, InputVars, Expr> Writable<O> for Closure<InputVars, Expr>
70where
71    O: Output,
72    InputVars: WritableSeq<O>,
73    Expr: Writable<O>,
74{
75    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
76        o.write("|").await?;
77        self.input_vars
78            .for_each(&mut SeparatedSeqAccept::comma_separated(o))
79            .await?;
80        o.write("| {\n").await?;
81        self.inside_block.write_to(o).await?;
82        o.write("\n}").await?;
83        Ok(())
84    }
85}
86
87impl<O: Output, Recv, FuncName, Args> Writable<O> for FunctionCall<Recv, FuncName, Args>
88where
89    Recv: Writable<O>,
90    FuncName: Writable<O>,
91    Args: WritableSeq<O>,
92{
93    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
94        self.receiver.write_to(o).await?;
95        o.write(if self.is_assoc { "::" } else { "." }).await?;
96        self.function.write_to(o).await
97    }
98}
99
100impl<O: Output, Variable, Expr> Writable<O> for LetStmt<Variable, Expr>
101where
102    Variable: Writable<O>,
103    Expr: Writable<O>,
104{
105    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
106        o.write("let ").await?;
107        self.0.write_to(o).await?;
108        o.write(" = ").await?;
109        self.1.write_to(o).await?;
110        o.write(";\n").await?;
111        Ok(())
112    }
113}
114
115impl<O: Output, Cont, Item> Writable<O> for AssociatedItem<Cont, Item>
116where
117    Cont: Writable<O>,
118    Item: Writable<O>,
119{
120    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
121        self.0.write_to(o).await?;
122        o.write("::").await?;
123        self.1.write_to(o).await
124    }
125}
126
127impl<O: Output, Expr> Writable<O> for QuestionMarkAfter<Expr>
128where
129    Expr: Writable<O>,
130{
131    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
132        self.0.write_to(o).await?;
133        o.write("?").await?;
134        Ok(())
135    }
136}
137
138impl<O: Output, Expr> Writable<O> for OkResultOf<Expr>
139where
140    Expr: Writable<O>,
141{
142    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
143        o.write("Ok(").await?;
144        self.0.write_to(o).await?;
145        o.write(")").await?;
146        Ok(())
147    }
148}
149
150impl<O: Output, Type, Trait> Writable<O> for TypeAsTrait<Type, Trait>
151where
152    Type: Writable<O>,
153    Trait: Writable<O>,
154{
155    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
156        o.write("<").await?;
157        self.0.write_to(o).await?;
158        o.write(" as ").await?;
159        self.1.write_to(o).await?;
160        o.write(">").await?;
161        Ok(())
162    }
163}
164
165impl<O, Attr, Mods, Name, Args, Return, Body> Writable<O>
166    for FunctionDef<Attr, Mods, Name, Args, Return, Body>
167where
168    O: Output,
169    Attr: WritableSeq<O>,
170    Mods: WritableSeq<O>,
171    Name: Writable<O>,
172    Args: WritableSeq<O>,
173    Return: Writable<O>,
174    Body: Writable<O>,
175{
176    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
177        self.attr.for_each(&mut AttributesAccept(o)).await?;
178        o.write("\n").await?;
179        self.mods.for_each(&mut ModsAccept(o)).await?;
180        o.write("fn ").await?;
181        self.decl.write_to(o).await?;
182        o.write(" -> ").await?;
183        self.return_type.write_to(o).await?;
184        self.body.write_to(o).await?;
185        o.write("\n").await?;
186        Ok(())
187    }
188}
189
190impl<O, Name, Args> Writable<O> for Function<Name, Args>
191where
192    O: Output,
193    Name: Writable<O>,
194    Args: WritableSeq<O>,
195{
196    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
197        self.name.write_to(output).await?;
198        output.write("(").await?;
199        self.args
200            .for_each(&mut SeparatedSeqAccept::comma_separated(output))
201            .await?;
202        output.write(")").await
203    }
204}
205
206impl<O: Output> Writable<O> for FunctionBodyDeclare {
207    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
208        output.write(";").await
209    }
210}
211
212impl<O: Output, Type, Value> Writable<O> for AssociatedTypeEquals<Type, Value>
213where
214    Type: Writable<O>,
215    Value: Writable<O>,
216{
217    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
218        self.0.write_to(o).await?;
219        o.write("=").await?;
220        self.1.write_to(o).await
221    }
222}
223
224impl<O: Output, Type> Writable<O> for DynOf<Type>
225where
226    Type: Writable<O>,
227{
228    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
229        o.write("dyn ").await?;
230        self.0.write_to(o).await
231    }
232}
233
234impl<O: Output, Type> Writable<O> for RefOf<Type>
235where
236    Type: Writable<O>,
237{
238    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
239        o.write("&").await?;
240        self.0.write_to(o).await
241    }
242}
243
244impl<O: Output, Type> Writable<O> for ImplOf<Type>
245where
246    Type: Writable<O>,
247{
248    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
249        o.write("impl ").await?;
250        self.0.write_to(o).await
251    }
252}
253
254impl<O: Output, Type> Writable<O> for LifetimedRefOf<'_, Type>
255where
256    Type: Writable<O>,
257{
258    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
259        o.write("&'").await?;
260        o.write(self.0).await?;
261        o.write(" ").await?;
262        self.1.write_to(o).await
263    }
264}
265
266impl<O: Output, VarName, Value> Writable<O> for AssociatedTypeDef<VarName, Value>
267where
268    VarName: Writable<O>,
269    Value: Writable<O>,
270{
271    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
272        o.write("\ntype ").await?;
273        self.0.write_to(o).await?;
274        o.write(" = ").await?;
275        self.1.write_to(o).await?;
276        o.write(";\n").await?;
277        Ok(())
278    }
279}
280
281impl<O, Attr, Mods, Name, TypeVars, SuperTraits, Body> Writable<O>
282    for TraitDef<Attr, Mods, Name, TypeVars, SuperTraits, Body>
283where
284    O: Output,
285    Attr: WritableSeq<O>,
286    Mods: WritableSeq<O>,
287    Name: Writable<O>,
288    TypeVars: WritableSeq<O>,
289    SuperTraits: WritableSeq<O>,
290    Body: Writable<O>,
291{
292    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
293        self.attr.for_each(&mut AttributesAccept(o)).await?;
294        o.write("\n").await?;
295        self.mods.for_each(&mut ModsAccept(o)).await?;
296        o.write("trait ").await?;
297        self.name.write_to(o).await?;
298        TypeArgAccept::new(o)
299            .write_inside_diamond(&self.type_variables)
300            .await?;
301        self.super_traits
302            .for_each(&mut type_arg_bound_accept(o))
303            .await?;
304        o.write(" {").await?;
305        self.body.write_to(o).await?;
306        o.write("}").await?;
307        Ok(())
308    }
309}
310
311impl<O, TypeVars, Trait, Recv, Body> Writable<O> for TraitImpl<TypeVars, Trait, Recv, Body>
312where
313    O: Output,
314    TypeVars: WritableSeq<O>,
315    Trait: Writable<O>,
316    Recv: Writable<O>,
317    Body: Writable<O>,
318{
319    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
320        o.write("impl").await?;
321        TypeArgAccept::new(o)
322            .write_inside_diamond(&self.type_variables)
323            .await?;
324        o.write(" ").await?;
325        self.the_trait.write_to(o).await?;
326        o.write(" for ").await?;
327        self.receiver.write_to(o).await?;
328        o.write(" {").await?;
329        self.body.write_to(o).await?;
330        o.write("}").await?;
331        Ok(())
332    }
333}
334
335impl<O, Name, TypeArgs> Writable<O> for Parameterized<Name, TypeArgs>
336where
337    O: Output,
338    Name: Writable<O>,
339    TypeArgs: WritableSeq<O>,
340{
341    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
342        self.name.write_to(o).await?;
343        TypeArgAccept::new(o)
344            .write_inside_diamond(&self.type_args)
345            .await
346    }
347}
348
349impl<O, TypeVar, Bounds> Writable<O> for BoundedTypeVar<TypeVar, Bounds>
350where
351    O: Output,
352    TypeVar: Writable<O>,
353    Bounds: WritableSeq<O>,
354{
355    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
356        self.0.write_to(output).await?;
357        self.1.for_each(&mut type_arg_bound_accept(output)).await
358    }
359}
360
361impl<O, Name, Type> Writable<O> for FunctionParam<Name, Type>
362where
363    O: Output,
364    Name: Writable<O>,
365    Type: Writable<O>,
366{
367    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
368        self.0.write_to(output).await?;
369        output.write(": ").await?;
370        self.1.write_to(output).await
371    }
372}
373
374//
375// Sequence acceptors
376//
377
378impl<'o, O: Output> SequenceAccept<O> for AttributesAccept<'o, O> {
379    async fn accept<W: Writable<O> + ?Sized>(&mut self, writable: &W) -> Result<(), O::Error> {
380        self.0.write("#[").await?;
381        writable.write_to(&mut self.0).await?;
382        self.0.write("]\n").await?;
383        Ok(())
384    }
385}
386
387struct ModsAccept<'o, O>(&'o mut O);
388
389impl<'o, O: Output> SequenceAccept<O> for ModsAccept<'o, O> {
390    async fn accept<W>(&mut self, writable: &W) -> Result<(), O::Error>
391    where
392        W: Writable<O>,
393    {
394        writable.write_to(&mut self.0).await?;
395        self.0.write(" ").await
396    }
397}
398
399struct TypeArgAccept<'o, O> {
400    inner: SeparatedSeqAccept<'o, O, Str<&'static str>, Str<&'static str>>,
401}
402
403impl<'o, O> TypeArgAccept<'o, O> {
404    fn new(output: &'o mut O) -> Self {
405        Self {
406            inner: SeparatedSeqAccept::new(output, Str("<"), Str(", ")),
407        }
408    }
409}
410
411impl<'o, O> TypeArgAccept<'o, O>
412where
413    O: Output,
414{
415    async fn write_inside_diamond<TypeArgs>(&mut self, type_args: &TypeArgs) -> Result<(), O::Error>
416    where
417        TypeArgs: WritableSeq<O>,
418    {
419        type_args.for_each(&mut self.inner).await?;
420        if self.inner.wrote_any {
421            self.inner.output.write(">").await?;
422        }
423        Ok(())
424    }
425}
426
427fn type_arg_bound_accept<'o, O>(
428    output: &'o mut O,
429) -> SeparatedSeqAccept<'o, O, Str<&'static str>, Str<&'static str>> {
430    SeparatedSeqAccept::new(output, Str(": "), Str(" + "))
431}