use proc_macro2::TokenStream;
use pyo3::{Bound, FromPyObject, PyAny, PyResult};
use quote::quote;
use serde::{Deserialize, Serialize};
use crate::{
CodeGen, CodeGenContext, Node, PythonOptions, Statement, SymbolTableScopes,
extract_list, WithItem,
};
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct With {
pub items: Vec<WithItem>,
pub body: Vec<Statement>,
pub lineno: Option<usize>,
pub col_offset: Option<usize>,
pub end_lineno: Option<usize>,
pub end_col_offset: Option<usize>,
}
impl<'a> FromPyObject<'a> for With {
fn extract_bound(ob: &Bound<'a, PyAny>) -> PyResult<Self> {
let items: Vec<WithItem> = extract_list(ob, "items", "with items")?;
let body: Vec<Statement> = extract_list(ob, "body", "with body")?;
Ok(With {
items,
body,
lineno: ob.lineno(),
col_offset: ob.col_offset(),
end_lineno: ob.end_lineno(),
end_col_offset: ob.end_col_offset(),
})
}
}
impl Node for With {
fn lineno(&self) -> Option<usize> { self.lineno }
fn col_offset(&self) -> Option<usize> { self.col_offset }
fn end_lineno(&self) -> Option<usize> { self.end_lineno }
fn end_col_offset(&self) -> Option<usize> { self.end_col_offset }
}
impl CodeGen for With {
type Context = CodeGenContext;
type Options = PythonOptions;
type SymbolTable = SymbolTableScopes;
fn find_symbols(self, symbols: Self::SymbolTable) -> Self::SymbolTable {
let symbols = self.items.into_iter().fold(symbols, |acc, item| {
let acc = item.context_expr.find_symbols(acc);
if let Some(vars) = item.optional_vars {
vars.find_symbols(acc)
} else {
acc
}
});
self.body.into_iter().fold(symbols, |acc, stmt| stmt.find_symbols(acc))
}
fn to_rust(
self,
ctx: Self::Context,
options: Self::Options,
symbols: Self::SymbolTable,
) -> Result<TokenStream, Box<dyn std::error::Error>> {
let body_tokens: Result<Vec<TokenStream>, Box<dyn std::error::Error>> = self.body.into_iter()
.map(|stmt| stmt.to_rust(ctx.clone(), options.clone(), symbols.clone()))
.collect();
let body_tokens = body_tokens?;
Ok(quote! {
{
#(#body_tokens)*
}
})
}
}
#[cfg(test)]
mod tests {
}