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, Attr, Mods, Name, TypeVars, SuperTraits, Body> Writable<O>
207    for TraitDef<Attr, Mods, Name, TypeVars, SuperTraits, Body>
208where
209    O: Output,
210    Attr: WritableSeq<O>,
211    Mods: WritableSeq<O>,
212    Name: Writable<O>,
213    TypeVars: WritableSeq<O>,
214    SuperTraits: WritableSeq<O>,
215    Body: Writable<O>,
216{
217    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
218        self.attr.for_each(&mut AttributesAccept(o)).await?;
219        o.write("\n").await?;
220        self.mods.for_each(&mut ModsAccept(o)).await?;
221        o.write("trait ").await?;
222        self.name.write_to(o).await?;
223        TypeArgAccept::new(o)
224            .write_inside_diamond(&self.type_variables)
225            .await?;
226        self.super_traits
227            .for_each(&mut type_arg_bound_accept(o))
228            .await?;
229        o.write(" {").await?;
230        self.body.write_to(o).await?;
231        o.write("}").await?;
232        Ok(())
233    }
234}
235
236impl<O, TypeVars, Trait, Recv, Body> Writable<O> for TraitImpl<TypeVars, Trait, Recv, Body>
237where
238    O: Output,
239    TypeVars: WritableSeq<O>,
240    Trait: Writable<O>,
241    Recv: Writable<O>,
242    Body: Writable<O>,
243{
244    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
245        o.write("impl").await?;
246        TypeArgAccept::new(o)
247            .write_inside_diamond(&self.type_variables)
248            .await?;
249        o.write(" ").await?;
250        self.the_trait.write_to(o).await?;
251        o.write(" for ").await?;
252        self.receiver.write_to(o).await?;
253        o.write(" {").await?;
254        self.body.write_to(o).await?;
255        o.write("}").await?;
256        Ok(())
257    }
258}
259
260impl<O, Name, TypeArgs> Writable<O> for Parameterized<Name, TypeArgs>
261where
262    O: Output,
263    Name: Writable<O>,
264    TypeArgs: WritableSeq<O>,
265{
266    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
267        self.name.write_to(o).await?;
268        TypeArgAccept::new(o)
269            .write_inside_diamond(&self.type_args)
270            .await
271    }
272}
273
274impl<O, TypeVar, Bounds> Writable<O> for BoundedTypeVar<TypeVar, Bounds>
275where
276    O: Output,
277    TypeVar: Writable<O>,
278    Bounds: WritableSeq<O>,
279{
280    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
281        self.0.write_to(output).await?;
282        self.1.for_each(&mut type_arg_bound_accept(output)).await
283    }
284}
285
286//
287// Sequence acceptors
288//
289
290impl<'o, O: Output> SequenceAccept<O> for AttributesAccept<'o, O> {
291    async fn accept<W: Writable<O> + ?Sized>(&mut self, writable: &W) -> Result<(), O::Error> {
292        self.0.write("#[").await?;
293        writable.write_to(&mut self.0).await?;
294        self.0.write("]\n").await?;
295        Ok(())
296    }
297}
298
299struct ModsAccept<'o, O>(&'o mut O);
300
301impl<'o, O: Output> SequenceAccept<O> for ModsAccept<'o, O> {
302    async fn accept<W>(&mut self, writable: &W) -> Result<(), O::Error>
303    where
304        W: Writable<O>,
305    {
306        writable.write_to(&mut self.0).await?;
307        self.0.write(" ").await
308    }
309}
310
311struct TypeArgAccept<'o, O> {
312    inner: SeparatedSeqAccept<'o, O, Str<&'static str>, Str<&'static str>>,
313}
314
315impl<'o, O> TypeArgAccept<'o, O> {
316    fn new(output: &'o mut O) -> Self {
317        Self {
318            inner: SeparatedSeqAccept::new(output, Str("<"), Str(", ")),
319        }
320    }
321}
322
323impl<'o, O> TypeArgAccept<'o, O>
324where
325    O: Output,
326{
327    async fn write_inside_diamond<TypeArgs>(&mut self, type_args: &TypeArgs) -> Result<(), O::Error>
328    where
329        TypeArgs: WritableSeq<O>,
330    {
331        type_args.for_each(&mut self.inner).await?;
332        if self.inner.wrote_any {
333            self.inner.output.write(">").await?;
334        }
335        Ok(())
336    }
337}
338
339fn type_arg_bound_accept<'o, O>(
340    output: &'o mut O,
341) -> SeparatedSeqAccept<'o, O, Str<&'static str>, Str<&'static str>> {
342    SeparatedSeqAccept::new(output, Str(": "), Str(" + "))
343}