weirdnet-code-spaces 0.1.0

Weirdnet code spaces: math, Polars, Rust and typed Python sources compiled into plugins against the weirdnet-plugin SDK, content-addressed builds, ABI-checked loading, trust hashes and space line-ups, under an application profile.
Documentation
use proc_macro2::TokenStream;
use quote::quote;

use crate::python_ast::{CodeGen, CodeGenContext, ExprType, PythonOptions, SymbolTableScopes};

use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
//#[pyo3(transparent)]
pub struct Await {
    pub value: Box<ExprType>,
}

impl<'a> CodeGen for Await {
    type Context = CodeGenContext;
    type Options = PythonOptions;
    type SymbolTable = SymbolTableScopes;

    fn to_rust(self, _ctx: Self::Context, _options: Self::Options, _symbols: Self::SymbolTable) -> Result<TokenStream, Box<dyn std::error::Error>> {
        let value = self.value.to_rust(_ctx, _options, _symbols)?;
        // A call to a user async function lowers to `f(...)?` — but the `?`
        // must apply to the awaited Result, not the future: reorder to
        // `f(...).await?`.
        let rendered = value.to_string();
        if let Some(inner) = rendered.trim_end().strip_suffix('?') {
            let inner: TokenStream = inner.parse().map_err(|e| format!("re-parsing awaited call: {:?}", e))?;
            return Ok(quote!(#inner.await?));
        }
        Ok(quote!(#value.await))
    }
}