use pyo3::{FromPyObject, PyAny};
use quote::{quote};
use proc_macro2::TokenStream;
use crate::codegen::{CodeGen, PythonOptions, CodeGenContext};
use crate::symbols::SymbolTableScopes;
pub type ListContents = crate::pytypes::List::<dyn CodeGen>;
#[derive(Clone, Debug, Default, FromPyObject)]
pub struct List<'a> {
pub elts: Vec<&'a PyAny>,
pub ctx: Option<String>,
}
impl<'a> CodeGen for List<'a> {
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 ts = TokenStream::new();
log::debug!("================self:{:#?}", self);
for elt in self.elts {
let el: &PyAny = elt.extract()?;
log::debug!("elt: {}", crate::ast_dump(el, None)?);
}
Ok(quote!(vec![#ts]))
}
}
#[cfg(test)]
mod tests {
use test_log::test;
use crate::StatementType;
use crate::ExprType;
use std::panic;
#[test]
fn parse_list() {
let module = crate::parse("[1, 2, 3]", "nothing").unwrap();
let statement = module.body[0].statement.clone();
match statement {
StatementType::Expr(e) => {
match e.value {
ExprType::List(list) => {
log::debug!("{:#?}", list);
assert_eq!(list.len(), 3);
},
_ => panic!("Could not find inner expression")
}
}
_ => panic!("Could not find outer expression.")
}
}
}