python_ast/ast/tree/
starred.rs

1use proc_macro2::TokenStream;
2use pyo3::{Bound, FromPyObject, PyAny, PyResult, prelude::PyAnyMethods, types::PyTypeMethods};
3use quote::quote;
4use serde::{Deserialize, Serialize};
5
6use crate::{
7    CodeGen, CodeGenContext, ExprType, Node, PythonOptions, SymbolTableScopes,
8    PyAttributeExtractor,
9};
10
11/// Starred expression for unpacking (*args)
12#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
13pub struct Starred {
14    /// The expression being unpacked
15    pub value: Box<ExprType>,
16    /// Context (Load, Store, etc.) - not used in Rust generation
17    pub ctx: Option<String>,
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 Starred {
26    fn extract_bound(ob: &Bound<'a, PyAny>) -> PyResult<Self> {
27        // Extract the value being starred
28        let value = ob.extract_attr_with_context("value", "starred expression value")?;
29        let value: ExprType = value.extract()?;
30        
31        // Extract context (Load, Store, etc.) - optional
32        let ctx = ob.getattr("ctx").ok().and_then(|ctx_obj| {
33            ctx_obj.get_type().name().ok().and_then(|name| name.extract().ok())
34        });
35        
36        Ok(Starred {
37            value: Box::new(value),
38            ctx,
39            lineno: ob.lineno(),
40            col_offset: ob.col_offset(),
41            end_lineno: ob.end_lineno(),
42            end_col_offset: ob.end_col_offset(),
43        })
44    }
45}
46
47impl Node for Starred {
48    fn lineno(&self) -> Option<usize> { self.lineno }
49    fn col_offset(&self) -> Option<usize> { self.col_offset }
50    fn end_lineno(&self) -> Option<usize> { self.end_lineno }
51    fn end_col_offset(&self) -> Option<usize> { self.end_col_offset }
52}
53
54impl CodeGen for Starred {
55    type Context = CodeGenContext;
56    type Options = PythonOptions;
57    type SymbolTable = SymbolTableScopes;
58
59    fn find_symbols(self, symbols: Self::SymbolTable) -> Self::SymbolTable {
60        (*self.value).clone().find_symbols(symbols)
61    }
62
63    fn to_rust(
64        self,
65        ctx: Self::Context,
66        options: Self::Options,
67        symbols: Self::SymbolTable,
68    ) -> Result<TokenStream, Box<dyn std::error::Error>> {
69        let value = (*self.value).clone().to_rust(ctx, options, symbols)?;
70        let _value_str = value.to_string();
71        
72        // Handle starred expressions for unpacking collections
73        // In Rust context, we need to indicate this is an unpacking operation
74        // The parent context (like vec! or function call) will handle the actual unpacking
75        Ok(quote! {
76            // STARRED: This indicates unpacking - parent context should handle
77            #value
78        })
79    }
80}
81
82#[cfg(test)]
83mod tests {
84    // Note: These tests will likely fail until full starred expression support is added
85    // create_parse_test!(test_starred_args, "*args", "test.py");
86    // create_parse_test!(test_starred_in_call, "func(*args)", "test.py");
87}