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 ModUnsafeExtern<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("unsafe 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    O::Ctx: ContextProvides<Edition>,
128    Attr: WritableSeq<O>,
129    Abi: Writable<O>,
130    Body: Writable<O>,
131{
132    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
133        self.attr
134            .for_each(&mut AttributesAccept::new(output))
135            .await?;
136        let edition = output.get_ctx();
137        if edition >= &Edition::Rust2024 {
138            output.write("unsafe ").await?;
139        }
140        output.write("extern \"").await?;
141        self.abi.write_to(output).await?;
142        output.write("\" {").await?;
143        self.body.write_to(output).await?;
144        output.write("}").await
145    }
146}
147
148impl<O: Output, Expr> Writable<O> for UnsafeBlock<Expr>
149where
150    Expr: Writable<O>,
151{
152    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
153        o.write("unsafe {\n").await?;
154        self.0.write_to(o).await?;
155        o.write("\n}").await?;
156        Ok(())
157    }
158}
159
160impl<O, InputVars, Expr> Writable<O> for Closure<InputVars, Expr>
161where
162    O: Output,
163    InputVars: WritableSeq<O>,
164    Expr: Writable<O>,
165{
166    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
167        o.write("|").await?;
168        self.input_vars
169            .for_each(&mut SeparatedSeqAccept::comma_separated(o))
170            .await?;
171        o.write("| {\n").await?;
172        self.inside_block.write_to(o).await?;
173        o.write("\n}").await?;
174        Ok(())
175    }
176}
177
178impl<O: Output, Recv, FuncName, Args> Writable<O> for FunctionCall<Recv, FuncName, Args>
179where
180    Recv: Writable<O>,
181    FuncName: Writable<O>,
182    Args: WritableSeq<O>,
183{
184    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
185        self.receiver.write_to(o).await?;
186        o.write(if self.is_assoc { "::" } else { "." }).await?;
187        self.function.write_to(o).await
188    }
189}
190
191impl<O: Output, Variable, Expr> Writable<O> for LetStmt<Variable, Expr>
192where
193    Variable: Writable<O>,
194    Expr: Writable<O>,
195{
196    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
197        o.write("let ").await?;
198        self.0.write_to(o).await?;
199        o.write(" = ").await?;
200        self.1.write_to(o).await?;
201        o.write(";\n").await?;
202        Ok(())
203    }
204}
205
206impl<O: Output, Cont, Item> Writable<O> for AssociatedItem<Cont, Item>
207where
208    Cont: Writable<O>,
209    Item: Writable<O>,
210{
211    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
212        self.0.write_to(o).await?;
213        o.write("::").await?;
214        self.1.write_to(o).await
215    }
216}
217
218impl<O: Output, Expr> Writable<O> for QuestionMarkAfter<Expr>
219where
220    Expr: Writable<O>,
221{
222    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
223        self.0.write_to(o).await?;
224        o.write("?").await?;
225        Ok(())
226    }
227}
228
229impl<O: Output, Expr> Writable<O> for OkResultOf<Expr>
230where
231    Expr: Writable<O>,
232{
233    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
234        o.write("Ok(").await?;
235        self.0.write_to(o).await?;
236        o.write(")").await?;
237        Ok(())
238    }
239}
240
241impl<O: Output, Type, Trait> Writable<O> for TypeAsTrait<Type, Trait>
242where
243    Type: Writable<O>,
244    Trait: Writable<O>,
245{
246    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
247        o.write("<").await?;
248        self.0.write_to(o).await?;
249        o.write(" as ").await?;
250        self.1.write_to(o).await?;
251        o.write(">").await?;
252        Ok(())
253    }
254}
255
256impl<O, Attr, Mods, Name, Args, Return, Body> Writable<O>
257    for FunctionDef<Attr, Mods, Name, Args, Return, Body>
258where
259    O: Output,
260    Attr: WritableSeq<O>,
261    Mods: WritableSeq<O>,
262    Name: Writable<O>,
263    Args: WritableSeq<O>,
264    Return: Writable<O>,
265    Body: Writable<O>,
266{
267    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
268        self.attr.for_each(&mut AttributesAccept::new(o)).await?;
269        self.mods.for_each(&mut ModsAccept(o)).await?;
270        o.write("fn ").await?;
271        self.decl.write_to(o).await?;
272        o.write(" -> ").await?;
273        self.return_type.write_to(o).await?;
274        self.body.write_to(o).await?;
275        o.write("\n").await?;
276        Ok(())
277    }
278}
279
280impl<O, Name, Args> Writable<O> for Function<Name, Args>
281where
282    O: Output,
283    Name: Writable<O>,
284    Args: WritableSeq<O>,
285{
286    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
287        self.name.write_to(output).await?;
288        output.write("(").await?;
289        self.args
290            .for_each(&mut SeparatedSeqAccept::comma_separated(output))
291            .await?;
292        output.write(")").await
293    }
294}
295
296impl<O: Output> Writable<O> for FunctionBodyDeclare {
297    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
298        output.write(";").await
299    }
300}
301
302impl<O: Output, Type, Value> Writable<O> for AssociatedTypeEquals<Type, Value>
303where
304    Type: Writable<O>,
305    Value: Writable<O>,
306{
307    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
308        self.0.write_to(o).await?;
309        o.write("=").await?;
310        self.1.write_to(o).await
311    }
312}
313
314impl<O: Output, Type> Writable<O> for DynOf<Type>
315where
316    Type: Writable<O>,
317{
318    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
319        o.write("dyn ").await?;
320        self.0.write_to(o).await
321    }
322}
323
324impl<O: Output, Type> Writable<O> for RefOf<Type>
325where
326    Type: Writable<O>,
327{
328    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
329        o.write("&").await?;
330        self.0.write_to(o).await
331    }
332}
333
334impl<O: Output, Type> Writable<O> for ImplOf<Type>
335where
336    Type: Writable<O>,
337{
338    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
339        o.write("impl ").await?;
340        self.0.write_to(o).await
341    }
342}
343
344impl<O: Output, Type> Writable<O> for LifetimedRefOf<'_, Type>
345where
346    Type: Writable<O>,
347{
348    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
349        o.write("&'").await?;
350        o.write(self.0).await?;
351        o.write(" ").await?;
352        self.1.write_to(o).await
353    }
354}
355
356impl<O: Output, VarName, Value> Writable<O> for AssociatedTypeDef<VarName, Value>
357where
358    VarName: Writable<O>,
359    Value: Writable<O>,
360{
361    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
362        o.write("\ntype ").await?;
363        self.0.write_to(o).await?;
364        o.write(" = ").await?;
365        self.1.write_to(o).await?;
366        o.write(";\n").await?;
367        Ok(())
368    }
369}
370
371impl<O, Attr, Mods, Name, TypeVars, SuperTraits, Body> Writable<O>
372    for TraitDef<Attr, Mods, Name, TypeVars, SuperTraits, Body>
373where
374    O: Output,
375    Attr: WritableSeq<O>,
376    Mods: WritableSeq<O>,
377    Name: Writable<O>,
378    TypeVars: WritableSeq<O>,
379    SuperTraits: WritableSeq<O>,
380    Body: Writable<O>,
381{
382    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
383        self.attr.for_each(&mut AttributesAccept::new(o)).await?;
384        self.mods.for_each(&mut ModsAccept(o)).await?;
385        o.write("trait ").await?;
386        self.name.write_to(o).await?;
387        TypeArgAccept::new(o)
388            .write_inside_diamond(&self.type_variables)
389            .await?;
390        self.super_traits
391            .for_each(&mut type_arg_bound_accept(o))
392            .await?;
393        o.write(" {").await?;
394        self.body.write_to(o).await?;
395        o.write("}").await?;
396        Ok(())
397    }
398}
399
400impl<O, TypeVars, Trait, Recv, Body> Writable<O> for TraitImpl<TypeVars, Trait, Recv, Body>
401where
402    O: Output,
403    TypeVars: WritableSeq<O>,
404    Trait: Writable<O>,
405    Recv: Writable<O>,
406    Body: Writable<O>,
407{
408    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
409        o.write("impl").await?;
410        TypeArgAccept::new(o)
411            .write_inside_diamond(&self.type_variables)
412            .await?;
413        o.write(" ").await?;
414        self.the_trait.write_to(o).await?;
415        o.write(" for ").await?;
416        self.receiver.write_to(o).await?;
417        o.write(" {").await?;
418        self.body.write_to(o).await?;
419        o.write("}").await?;
420        Ok(())
421    }
422}
423
424impl<O, Name, TypeArgs> Writable<O> for Parameterized<Name, TypeArgs>
425where
426    O: Output,
427    Name: Writable<O>,
428    TypeArgs: WritableSeq<O>,
429{
430    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
431        self.name.write_to(o).await?;
432        TypeArgAccept::new(o)
433            .write_inside_diamond(&self.type_args)
434            .await
435    }
436}
437
438impl<O, TypeVar, Bounds> Writable<O> for BoundedTypeVar<TypeVar, Bounds>
439where
440    O: Output,
441    TypeVar: Writable<O>,
442    Bounds: WritableSeq<O>,
443{
444    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
445        self.0.write_to(output).await?;
446        self.1.for_each(&mut type_arg_bound_accept(output)).await
447    }
448}
449
450impl<O> Writable<O> for Lifetime<'_>
451where
452    O: Output,
453{
454    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
455        output.write("'").await?;
456        output.write(self.0).await
457    }
458}
459
460impl<O, Name, Type> Writable<O> for FunctionParam<Name, Type>
461where
462    O: Output,
463    Name: Writable<O>,
464    Type: Writable<O>,
465{
466    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
467        self.0.write_to(output).await?;
468        output.write(": ").await?;
469        self.1.write_to(output).await
470    }
471}
472
473//
474// Sequence acceptors
475//
476
477struct ModsAccept<'o, O>(&'o mut O);
478
479impl<'o, O: Output> SequenceAccept<O> for ModsAccept<'o, O> {
480    async fn accept<W>(&mut self, writable: &W) -> Result<(), O::Error>
481    where
482        W: Writable<O>,
483    {
484        writable.write_to(&mut self.0).await?;
485        self.0.write(" ").await
486    }
487}
488
489struct TypeArgAccept<'o, O> {
490    inner: SeparatedSeqAccept<'o, O, Str<&'static str>, Str<&'static str>>,
491}
492
493impl<'o, O> TypeArgAccept<'o, O> {
494    fn new(output: &'o mut O) -> Self {
495        Self {
496            inner: SeparatedSeqAccept::new(output, Str("<"), Str(", ")),
497        }
498    }
499}
500
501impl<'o, O> TypeArgAccept<'o, O>
502where
503    O: Output,
504{
505    async fn write_inside_diamond<TypeArgs>(&mut self, type_args: &TypeArgs) -> Result<(), O::Error>
506    where
507        TypeArgs: WritableSeq<O>,
508    {
509        type_args.for_each(&mut self.inner).await?;
510        if self.inner.wrote_any {
511            self.inner.output.write(">").await?;
512        }
513        Ok(())
514    }
515}
516
517fn type_arg_bound_accept<'o, O>(
518    output: &'o mut O,
519) -> SeparatedSeqAccept<'o, O, Str<&'static str>, Str<&'static str>> {
520    SeparatedSeqAccept::new(output, Str(": "), Str(" + "))
521}