use proc_macro2::TokenStream;
use pyo3::{Borrowed, FromPyObject, PyAny, PyResult, prelude::PyAnyMethods, types::PyTypeMethods};
use serde::{Deserialize, Serialize};
use crate::{
CodeGen, CodeGenContext, ExprType, Node, PythonOptions, SymbolTableScopes,
PyAttributeExtractor,
};
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct Starred {
pub value: Box<ExprType>,
pub ctx: Option<String>,
pub lineno: Option<usize>,
pub col_offset: Option<usize>,
pub end_lineno: Option<usize>,
pub end_col_offset: Option<usize>,
}
impl<'a, 'py> FromPyObject<'a, 'py> for Starred {
type Error = pyo3::PyErr;
fn extract(ob: Borrowed<'a, 'py, PyAny>) -> PyResult<Self> {
let value = ob.extract_attr_with_context("value", "starred expression value")?;
let value: ExprType = value.extract()?;
let ctx = ob.getattr("ctx").ok().and_then(|ctx_obj| {
ctx_obj.get_type().name().ok().and_then(|name| name.extract().ok())
});
Ok(Starred {
value: Box::new(value),
ctx,
lineno: ob.lineno(),
col_offset: ob.col_offset(),
end_lineno: ob.end_lineno(),
end_col_offset: ob.end_col_offset(),
})
}
}
impl Node for Starred {
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 Starred {
type Context = CodeGenContext;
type Options = PythonOptions;
type SymbolTable = SymbolTableScopes;
fn find_symbols(self, symbols: Self::SymbolTable) -> Self::SymbolTable {
(*self.value).clone().find_symbols(symbols)
}
fn to_rust(
self,
_ctx: Self::Context,
_options: Self::Options,
_symbols: Self::SymbolTable,
) -> Result<TokenStream, Box<dyn std::error::Error>> {
Err(
"starred unpacking (`*expr`) is not supported yet: it would \
silently pass the collection as a single value instead of \
spreading its elements. Spell the elements out explicitly."
.to_string()
.into(),
)
}
}
#[cfg(test)]
mod tests {
}