python_ast/ast/tree/
await_kw.rs

1use proc_macro2::TokenStream;
2use pyo3::{Bound, PyAny, PyResult, FromPyObject, prelude::PyAnyMethods};
3use quote::quote;
4
5use crate::{CodeGen, CodeGenContext, ExprType, PythonOptions, SymbolTableScopes};
6
7use serde::{Deserialize, Serialize};
8
9#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
10//#[pyo3(transparent)]
11pub struct Await {
12    pub value: Box<ExprType>,
13}
14
15impl<'a> FromPyObject<'a> for Await {
16    fn extract_bound(ob: &Bound<'_, PyAny>) -> PyResult<Self> {
17        let value = ob.getattr("value").expect("Await.value");
18        Ok(Await {
19            value: Box::new(value.extract().expect("Await.value")),
20        })
21    }
22}
23
24impl<'a> CodeGen for Await {
25    type Context = CodeGenContext;
26    type Options = PythonOptions;
27    type SymbolTable = SymbolTableScopes;
28
29    fn to_rust(
30        self,
31        _ctx: Self::Context,
32        _options: Self::Options,
33        _symbols: Self::SymbolTable,
34    ) -> Result<TokenStream, Box<dyn std::error::Error>> {
35        let value = self
36            .value
37            .to_rust(_ctx, _options, _symbols)
38            .expect("Failed to convert async function to rust");
39        Ok(quote!(#value.await))
40    }
41}