python_ast/ast/tree/
with_stmt.rs

1use proc_macro2::TokenStream;
2use pyo3::{Bound, FromPyObject, PyAny, PyResult};
3use quote::quote;
4use serde::{Deserialize, Serialize};
5
6use crate::{
7    CodeGen, CodeGenContext, Node, PythonOptions, Statement, SymbolTableScopes,
8    extract_list, WithItem,
9};
10
11/// Regular with statement (with context as var: ...)
12#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
13pub struct With {
14    /// The with items (context managers)
15    pub items: Vec<WithItem>,
16    /// The body of the with statement
17    pub body: Vec<Statement>,
18    /// Position information
19    pub lineno: Option<usize>,
20    pub col_offset: Option<usize>,
21    pub end_lineno: Option<usize>,
22    pub end_col_offset: Option<usize>,
23}
24
25impl<'a> FromPyObject<'a> for With {
26    fn extract_bound(ob: &Bound<'a, PyAny>) -> PyResult<Self> {
27        // Extract items (list of withitem objects)
28        let items: Vec<WithItem> = extract_list(ob, "items", "with items")?;
29        
30        // Extract body
31        let body: Vec<Statement> = extract_list(ob, "body", "with body")?;
32        
33        Ok(With {
34            items,
35            body,
36            lineno: ob.lineno(),
37            col_offset: ob.col_offset(),
38            end_lineno: ob.end_lineno(),
39            end_col_offset: ob.end_col_offset(),
40        })
41    }
42}
43
44impl Node for With {
45    fn lineno(&self) -> Option<usize> { self.lineno }
46    fn col_offset(&self) -> Option<usize> { self.col_offset }
47    fn end_lineno(&self) -> Option<usize> { self.end_lineno }
48    fn end_col_offset(&self) -> Option<usize> { self.end_col_offset }
49}
50
51impl CodeGen for With {
52    type Context = CodeGenContext;
53    type Options = PythonOptions;
54    type SymbolTable = SymbolTableScopes;
55
56    fn find_symbols(self, symbols: Self::SymbolTable) -> Self::SymbolTable {
57        // Process items and body
58        let symbols = self.items.into_iter().fold(symbols, |acc, item| {
59            let acc = item.context_expr.find_symbols(acc);
60            if let Some(vars) = item.optional_vars {
61                vars.find_symbols(acc)
62            } else {
63                acc
64            }
65        });
66        self.body.into_iter().fold(symbols, |acc, stmt| stmt.find_symbols(acc))
67    }
68
69    fn to_rust(
70        self,
71        ctx: Self::Context,
72        options: Self::Options,
73        symbols: Self::SymbolTable,
74    ) -> Result<TokenStream, Box<dyn std::error::Error>> {
75        // Generate body
76        let body_tokens: Result<Vec<TokenStream>, Box<dyn std::error::Error>> = self.body.into_iter()
77            .map(|stmt| stmt.to_rust(ctx.clone(), options.clone(), symbols.clone()))
78            .collect();
79        let body_tokens = body_tokens?;
80
81        // For now, generate a simplified block
82        // In practice, this would need proper context management
83        Ok(quote! {
84            {
85                // With block - simplified translation
86                // Python's with doesn't map directly to Rust patterns
87                #(#body_tokens)*
88            }
89        })
90    }
91}
92
93#[cfg(test)]
94mod tests {
95    // Tests would go here - currently commented out as they need full AST infrastructure
96    // create_parse_test!(test_simple_with, "with context:\n    pass", "test.py");
97}