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 serde::{Deserialize, Serialize};

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

/// A keyword argument, gnerally used in function calls.
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
pub struct NamedExpr {
    pub left: Box<ExprType>,
    pub right: Box<ExprType>,
}

impl CodeGen for NamedExpr {
    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 left = self.left.clone().to_rust(ctx.clone(), options.clone(), symbols.clone())?;
        let right = self.right.clone().to_rust(ctx, options, symbols)?;
        Ok(quote!(#left = #right))
    }
}

#[cfg(python_ast_upstream_tests)]
mod test {
    use super::*;
    use crate::python_ast::{Constant, ExprType, Name};
    use litrs::*;

    #[test]
    fn test_named_expression() {
        let named_expression = NamedExpr {
            left: Box::new(ExprType::Name(Name { id: "a".to_string() })),
            right: Box::new(ExprType::Constant(Constant(Some(Literal::Integer(IntegerLit::parse("1".to_string()).unwrap()))))),
        };
        let rust = named_expression.to_rust(CodeGenContext::Module("test".to_string()), PythonOptions::default(), SymbolTableScopes::new()).unwrap();
        assert_eq!(rust.to_string(), "a = 1");
    }
}