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::context::ContextProvides;
20use crate::{Output, SequenceAccept, Writable, WritableSeq};
21
22impl<O, Cond, Attr> Writable<O> for CfgAttr<Cond, Attr>
23where
24    O: Output,
25    Cond: Writable<O>,
26    Attr: Writable<O>,
27{
28    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
29        output.write("cfg_attr(").await?;
30        self.0.write_to(output).await?;
31        output.write(", ").await?;
32        self.1.write_to(output).await?;
33        output.write(")").await
34    }
35}
36
37impl<O> Writable<O> for NoMangle
38where
39    O: Output,
40    O::Ctx: ContextProvides<Edition>,
41{
42    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
43        let edition = output.get_ctx();
44        output
45            .write(if edition >= &Edition::Rust2024 {
46                "unsafe(no_mangle)"
47            } else {
48                "no_mangle"
49            })
50            .await
51    }
52}
53
54impl<O, Lints> Writable<O> for AllowLints<Lints>
55where
56    O: Output,
57    Lints: WritableSeq<O>,
58{
59    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
60        let mut acceptor = SeparatedSeqAccept::new(output, Str("allow("), Str(","));
61        self.0.for_each(&mut acceptor).await?;
62        if acceptor.wrote_any {
63            output.write(")").await?;
64        }
65        Ok(())
66    }
67}
68
69impl<O, Msg, Since> Writable<O> for Deprecated<Msg, Since>
70where
71    O: Output,
72    Msg: Writable<O>,
73    Since: Writable<O>,
74{
75    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
76        match self {
77            Self::Basic => output.write("deprecated").await,
78            Self::Message(msg) => {
79                output.write("deprecated = \"").await?;
80                msg.write_to(output).await?;
81                output.write("\"").await
82            }
83            Self::Full { since, note } => {
84                output.write("deprecated(since = \"").await?;
85                since.write_to(output).await?;
86                output.write("\", note = \"").await?;
87                note.write_to(output).await?;
88                output.write("\")").await
89            }
90        }
91    }
92}
93
94impl<O> Writable<O> for MustUse
95where
96    O: Output,
97{
98    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
99        output.write("must_use").await
100    }
101}
102
103impl<O> Writable<O> for ModPub
104where
105    O: Output,
106{
107    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
108        output.write("pub").await
109    }
110}
111
112impl<O, Abi> Writable<O> for ModExtern<Abi>
113where
114    O: Output,
115    Abi: Writable<O>,
116{
117    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
118        output.write("extern \"").await?;
119        self.0.write_to(output).await?;
120        output.write("\"").await
121    }
122}
123
124impl<O, Attr, Abi, Body> Writable<O> for ExternBlock<Attr, Abi, Body>
125where
126    O: Output,
127    Attr: WritableSeq<O>,
128    Abi: Writable<O>,
129    Body: Writable<O>,
130{
131    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
132        self.attr.for_each(&mut AttributesAccept(output)).await?;
133        ModExtern(&self.abi).write_to(output).await?;
134        output.write(" {").await?;
135        self.body.write_to(output).await?;
136        output.write("}").await
137    }
138}
139
140impl<O: Output, Expr> Writable<O> for UnsafeBlock<Expr>
141where
142    Expr: Writable<O>,
143{
144    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
145        o.write("unsafe {\n").await?;
146        self.0.write_to(o).await?;
147        o.write("\n}").await?;
148        Ok(())
149    }
150}
151
152impl<O, InputVars, Expr> Writable<O> for Closure<InputVars, Expr>
153where
154    O: Output,
155    InputVars: WritableSeq<O>,
156    Expr: Writable<O>,
157{
158    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
159        o.write("|").await?;
160        self.input_vars
161            .for_each(&mut SeparatedSeqAccept::comma_separated(o))
162            .await?;
163        o.write("| {\n").await?;
164        self.inside_block.write_to(o).await?;
165        o.write("\n}").await?;
166        Ok(())
167    }
168}
169
170impl<O: Output, Recv, FuncName, Args> Writable<O> for FunctionCall<Recv, FuncName, Args>
171where
172    Recv: Writable<O>,
173    FuncName: Writable<O>,
174    Args: WritableSeq<O>,
175{
176    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
177        self.receiver.write_to(o).await?;
178        o.write(if self.is_assoc { "::" } else { "." }).await?;
179        self.function.write_to(o).await
180    }
181}
182
183impl<O: Output, Variable, Expr> Writable<O> for LetStmt<Variable, Expr>
184where
185    Variable: Writable<O>,
186    Expr: Writable<O>,
187{
188    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
189        o.write("let ").await?;
190        self.0.write_to(o).await?;
191        o.write(" = ").await?;
192        self.1.write_to(o).await?;
193        o.write(";\n").await?;
194        Ok(())
195    }
196}
197
198impl<O: Output, Cont, Item> Writable<O> for AssociatedItem<Cont, Item>
199where
200    Cont: Writable<O>,
201    Item: Writable<O>,
202{
203    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
204        self.0.write_to(o).await?;
205        o.write("::").await?;
206        self.1.write_to(o).await
207    }
208}
209
210impl<O: Output, Expr> Writable<O> for QuestionMarkAfter<Expr>
211where
212    Expr: Writable<O>,
213{
214    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
215        self.0.write_to(o).await?;
216        o.write("?").await?;
217        Ok(())
218    }
219}
220
221impl<O: Output, Expr> Writable<O> for OkResultOf<Expr>
222where
223    Expr: Writable<O>,
224{
225    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
226        o.write("Ok(").await?;
227        self.0.write_to(o).await?;
228        o.write(")").await?;
229        Ok(())
230    }
231}
232
233impl<O: Output, Type, Trait> Writable<O> for TypeAsTrait<Type, Trait>
234where
235    Type: Writable<O>,
236    Trait: 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        o.write(" as ").await?;
242        self.1.write_to(o).await?;
243        o.write(">").await?;
244        Ok(())
245    }
246}
247
248impl<O, Attr, Mods, Name, Args, Return, Body> Writable<O>
249    for FunctionDef<Attr, Mods, Name, Args, Return, Body>
250where
251    O: Output,
252    Attr: WritableSeq<O>,
253    Mods: WritableSeq<O>,
254    Name: Writable<O>,
255    Args: WritableSeq<O>,
256    Return: Writable<O>,
257    Body: Writable<O>,
258{
259    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
260        self.attr.for_each(&mut AttributesAccept(o)).await?;
261        self.mods.for_each(&mut ModsAccept(o)).await?;
262        o.write("fn ").await?;
263        self.decl.write_to(o).await?;
264        o.write(" -> ").await?;
265        self.return_type.write_to(o).await?;
266        self.body.write_to(o).await?;
267        o.write("\n").await?;
268        Ok(())
269    }
270}
271
272impl<O, Name, Args> Writable<O> for Function<Name, Args>
273where
274    O: Output,
275    Name: Writable<O>,
276    Args: WritableSeq<O>,
277{
278    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
279        self.name.write_to(output).await?;
280        output.write("(").await?;
281        self.args
282            .for_each(&mut SeparatedSeqAccept::comma_separated(output))
283            .await?;
284        output.write(")").await
285    }
286}
287
288impl<O: Output> Writable<O> for FunctionBodyDeclare {
289    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
290        output.write(";").await
291    }
292}
293
294impl<O: Output, Type, Value> Writable<O> for AssociatedTypeEquals<Type, Value>
295where
296    Type: Writable<O>,
297    Value: Writable<O>,
298{
299    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
300        self.0.write_to(o).await?;
301        o.write("=").await?;
302        self.1.write_to(o).await
303    }
304}
305
306impl<O: Output, Type> Writable<O> for DynOf<Type>
307where
308    Type: Writable<O>,
309{
310    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
311        o.write("dyn ").await?;
312        self.0.write_to(o).await
313    }
314}
315
316impl<O: Output, Type> Writable<O> for RefOf<Type>
317where
318    Type: Writable<O>,
319{
320    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
321        o.write("&").await?;
322        self.0.write_to(o).await
323    }
324}
325
326impl<O: Output, Type> Writable<O> for ImplOf<Type>
327where
328    Type: Writable<O>,
329{
330    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
331        o.write("impl ").await?;
332        self.0.write_to(o).await
333    }
334}
335
336impl<O: Output, Type> Writable<O> for LifetimedRefOf<'_, Type>
337where
338    Type: Writable<O>,
339{
340    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
341        o.write("&'").await?;
342        o.write(self.0).await?;
343        o.write(" ").await?;
344        self.1.write_to(o).await
345    }
346}
347
348impl<O: Output, VarName, Value> Writable<O> for AssociatedTypeDef<VarName, Value>
349where
350    VarName: Writable<O>,
351    Value: Writable<O>,
352{
353    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
354        o.write("\ntype ").await?;
355        self.0.write_to(o).await?;
356        o.write(" = ").await?;
357        self.1.write_to(o).await?;
358        o.write(";\n").await?;
359        Ok(())
360    }
361}
362
363impl<O, Attr, Mods, Name, TypeVars, SuperTraits, Body> Writable<O>
364    for TraitDef<Attr, Mods, Name, TypeVars, SuperTraits, Body>
365where
366    O: Output,
367    Attr: WritableSeq<O>,
368    Mods: WritableSeq<O>,
369    Name: Writable<O>,
370    TypeVars: WritableSeq<O>,
371    SuperTraits: WritableSeq<O>,
372    Body: Writable<O>,
373{
374    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
375        self.attr.for_each(&mut AttributesAccept(o)).await?;
376        self.mods.for_each(&mut ModsAccept(o)).await?;
377        o.write("trait ").await?;
378        self.name.write_to(o).await?;
379        TypeArgAccept::new(o)
380            .write_inside_diamond(&self.type_variables)
381            .await?;
382        self.super_traits
383            .for_each(&mut type_arg_bound_accept(o))
384            .await?;
385        o.write(" {").await?;
386        self.body.write_to(o).await?;
387        o.write("}").await?;
388        Ok(())
389    }
390}
391
392impl<O, TypeVars, Trait, Recv, Body> Writable<O> for TraitImpl<TypeVars, Trait, Recv, Body>
393where
394    O: Output,
395    TypeVars: WritableSeq<O>,
396    Trait: Writable<O>,
397    Recv: Writable<O>,
398    Body: Writable<O>,
399{
400    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
401        o.write("impl").await?;
402        TypeArgAccept::new(o)
403            .write_inside_diamond(&self.type_variables)
404            .await?;
405        o.write(" ").await?;
406        self.the_trait.write_to(o).await?;
407        o.write(" for ").await?;
408        self.receiver.write_to(o).await?;
409        o.write(" {").await?;
410        self.body.write_to(o).await?;
411        o.write("}").await?;
412        Ok(())
413    }
414}
415
416impl<O, Name, TypeArgs> Writable<O> for Parameterized<Name, TypeArgs>
417where
418    O: Output,
419    Name: Writable<O>,
420    TypeArgs: WritableSeq<O>,
421{
422    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
423        self.name.write_to(o).await?;
424        TypeArgAccept::new(o)
425            .write_inside_diamond(&self.type_args)
426            .await
427    }
428}
429
430impl<O, TypeVar, Bounds> Writable<O> for BoundedTypeVar<TypeVar, Bounds>
431where
432    O: Output,
433    TypeVar: Writable<O>,
434    Bounds: WritableSeq<O>,
435{
436    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
437        self.0.write_to(output).await?;
438        self.1.for_each(&mut type_arg_bound_accept(output)).await
439    }
440}
441
442impl<O> Writable<O> for Lifetime<'_>
443where
444    O: Output,
445{
446    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
447        output.write("'").await?;
448        output.write(self.0).await
449    }
450}
451
452impl<O, Name, Type> Writable<O> for FunctionParam<Name, Type>
453where
454    O: Output,
455    Name: Writable<O>,
456    Type: Writable<O>,
457{
458    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
459        self.0.write_to(output).await?;
460        output.write(": ").await?;
461        self.1.write_to(output).await
462    }
463}
464
465//
466// Sequence acceptors
467//
468
469impl<'o, O: Output> SequenceAccept<O> for AttributesAccept<'o, O> {
470    async fn accept<W: Writable<O> + ?Sized>(&mut self, writable: &W) -> Result<(), O::Error> {
471        self.0.write("#[").await?;
472        writable.write_to(&mut self.0).await?;
473        self.0.write("]\n").await?;
474        Ok(())
475    }
476}
477
478struct ModsAccept<'o, O>(&'o mut O);
479
480impl<'o, O: Output> SequenceAccept<O> for ModsAccept<'o, O> {
481    async fn accept<W>(&mut self, writable: &W) -> Result<(), O::Error>
482    where
483        W: Writable<O>,
484    {
485        writable.write_to(&mut self.0).await?;
486        self.0.write(" ").await
487    }
488}
489
490struct TypeArgAccept<'o, O> {
491    inner: SeparatedSeqAccept<'o, O, Str<&'static str>, Str<&'static str>>,
492}
493
494impl<'o, O> TypeArgAccept<'o, O> {
495    fn new(output: &'o mut O) -> Self {
496        Self {
497            inner: SeparatedSeqAccept::new(output, Str("<"), Str(", ")),
498        }
499    }
500}
501
502impl<'o, O> TypeArgAccept<'o, O>
503where
504    O: Output,
505{
506    async fn write_inside_diamond<TypeArgs>(&mut self, type_args: &TypeArgs) -> Result<(), O::Error>
507    where
508        TypeArgs: WritableSeq<O>,
509    {
510        type_args.for_each(&mut self.inner).await?;
511        if self.inner.wrote_any {
512            self.inner.output.write(">").await?;
513        }
514        Ok(())
515    }
516}
517
518fn type_arg_bound_accept<'o, O>(
519    output: &'o mut O,
520) -> SeparatedSeqAccept<'o, O, Str<&'static str>, Str<&'static str>> {
521    SeparatedSeqAccept::new(output, Str(": "), Str(" + "))
522}