python_ast/ast/tree/
list.rs1use proc_macro2::TokenStream;
2use pyo3::{FromPyObject, PyAny};
3use quote::quote;
4
5use crate::{dump, CodeGen, CodeGenContext, PythonOptions, SymbolTableScopes};
6
7pub type ListContents = crate::pytypes::List<dyn CodeGen>;
11
12#[derive(Clone, Debug, Default, FromPyObject)]
13pub struct List<'a> {
14 pub elts: Vec<&'a PyAny>,
15 pub ctx: Option<String>,
16}
17
18impl<'a> CodeGen for List<'a> {
19 type Context = CodeGenContext;
20 type Options = PythonOptions;
21 type SymbolTable = SymbolTableScopes;
22
23 fn to_rust(
24 self,
25 _ctx: Self::Context,
26 _options: Self::Options,
27 _symbols: Self::SymbolTable,
28 ) -> Result<TokenStream, Box<dyn std::error::Error>> {
29 let ts = TokenStream::new();
30 log::debug!("================self:{:#?}", self);
31 for elt in self.elts {
32 let el: &PyAny = elt.extract()?;
33 log::debug!("elt: {}", dump(el, None)?);
34 }
36 Ok(quote!(vec![#ts]))
37 }
38}
39
40#[cfg(test)]
43mod tests {
44 use crate::ExprType;
45 use crate::StatementType;
46 use std::panic;
47 use test_log::test;
48
49 #[test]
50 fn parse_list() {
51 let module = crate::parse("[1, 2, 3]", "nothing.py").unwrap();
52 let statement = module.raw.body[0].statement.clone();
53 match statement {
54 StatementType::Expr(e) => match e.value {
55 ExprType::List(list) => {
56 log::debug!("{:#?}", list);
57 assert_eq!(list.len(), 3);
58 }
59 _ => panic!("Could not find inner expression"),
60 },
61 _ => panic!("Could not find outer expression."),
62 }
63 }
64}