python_ast/ast/tree/
starred.rs1use 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#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
13pub struct Starred {
14 pub value: Box<ExprType>,
16 pub ctx: Option<String>,
18 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 let value = ob.extract_attr_with_context("value", "starred expression value")?;
29 let value: ExprType = value.extract()?;
30
31 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 Ok(quote! {
76 #value
78 })
79 }
80}
81
82#[cfg(test)]
83mod tests {
84 }