use proc_macro2::TokenStream;
use quote::quote;
use serde::{Deserialize, Serialize};
use crate::python_ast::{CodeGen, CodeGenContext, ExprType, Node, PythonOptions, SymbolTableScopes};
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
pub struct Argument {
pub value: ExprType,
pub lineno: Option<usize>,
pub col_offset: Option<usize>,
pub end_lineno: Option<usize>,
pub end_col_offset: Option<usize>,
}
pub type Arg = ExprType;
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
pub struct Parameter {
pub arg: String,
pub annotation: Option<Box<ExprType>>,
pub type_comment: Option<String>,
pub lineno: Option<usize>,
pub col_offset: Option<usize>,
pub end_lineno: Option<usize>,
pub end_col_offset: Option<usize>,
}
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
pub struct Arguments {
pub posonlyargs: Vec<Parameter>,
pub args: Vec<Parameter>,
pub vararg: Option<Parameter>,
pub kwonlyargs: Vec<Parameter>,
pub kw_defaults: Vec<Option<Box<ExprType>>>,
pub kwarg: Option<Parameter>,
pub defaults: Vec<Box<ExprType>>,
}
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
pub struct CallArguments {
pub args: Vec<ExprType>,
pub keywords: Vec<crate::python_ast::Keyword>,
}
impl CodeGen for Argument {
type Context = CodeGenContext;
type Options = PythonOptions;
type SymbolTable = SymbolTableScopes;
fn to_rust(
self,
ctx: Self::Context,
options: Self::Options,
symbols: Self::SymbolTable,
) -> std::result::Result<TokenStream, Box<dyn std::error::Error>> {
self.value.to_rust(ctx, options, symbols)
}
}
pub(crate) fn is_optional_annotation(ann: &ExprType) -> bool {
match ann {
ExprType::Subscript(sub) => {
matches!(sub.value.as_ref(), ExprType::Name(n) if n.id == "Optional")
}
ExprType::BinOp(op) if matches!(op.op, crate::python_ast::BinOps::BitOr) => {
crate::python_ast::is_none_expr(&op.left) || crate::python_ast::is_none_expr(&op.right)
}
_ => false,
}
}
pub fn python_annotation_to_rust_type(annotation: &ExprType) -> Option<TokenStream> {
match annotation {
ExprType::BinOp(op) if matches!(op.op, crate::python_ast::BinOps::BitOr) => {
let inner = if crate::python_ast::is_none_expr(&op.left) {
op.right.as_ref()
} else if crate::python_ast::is_none_expr(&op.right) {
op.left.as_ref()
} else {
return None;
};
let inner = python_annotation_to_rust_type(inner)?;
return Some(quote!(Option<#inner>));
}
_ => {}
}
match annotation {
ExprType::Name(name) => match name.id.as_str() {
"int" => Some(quote!(i64)),
"float" => Some(quote!(f64)),
"str" => Some(quote!(String)),
"bool" => Some(quote!(bool)),
"bytes" => Some(quote!(Vec<u8>)),
_ => None,
},
ExprType::Subscript(sub) => {
let container = match sub.value.as_ref() {
ExprType::Name(n) => n.id.as_str(),
_ => return None,
};
match (&sub.kind, container) {
(crate::python_ast::SubscriptKind::Index(elt), "Optional") => {
let inner = python_annotation_to_rust_type(elt)?;
Some(quote!(Option<#inner>))
}
(crate::python_ast::SubscriptKind::Index(elt), "list") => {
let inner = python_annotation_to_rust_type(elt)?;
Some(quote!(Vec<#inner>))
}
(crate::python_ast::SubscriptKind::Index(elt), "set" | "frozenset") => {
let inner = python_annotation_to_rust_type(elt)?;
Some(quote!(std::collections::HashSet<#inner>))
}
(crate::python_ast::SubscriptKind::Index(kv), "dict") => {
if let ExprType::Tuple(t) = kv.as_ref() {
if let [k, v] = t.elts.as_slice() {
let k = python_annotation_to_rust_type(k)?;
let v = python_annotation_to_rust_type(v)?;
return Some(quote!(PyDict<#k, #v>));
}
}
None
}
_ => None,
}
}
_ => None,
}
}
impl CodeGen for Parameter {
type Context = CodeGenContext;
type Options = PythonOptions;
type SymbolTable = SymbolTableScopes;
fn to_rust(
self,
ctx: Self::Context,
options: Self::Options,
symbols: Self::SymbolTable,
) -> std::result::Result<TokenStream, Box<dyn std::error::Error>> {
let param_name = crate::python_ast::safe_ident(&self.arg);
if let Some(annotation) = self.annotation {
if matches!(&*annotation, ExprType::Name(n) if n.id == "str") {
return Ok(quote!(#param_name: impl Into<String>));
}
let rust_type = match python_annotation_to_rust_type(&annotation) {
Some(mapped) => mapped,
None => annotation.to_rust(ctx, options, symbols)?,
};
Ok(quote!(#param_name: #rust_type))
} else {
Ok(quote!(#param_name: impl Into<PyObject>))
}
}
}
impl CodeGen for Arguments {
type Context = CodeGenContext;
type Options = PythonOptions;
type SymbolTable = SymbolTableScopes;
fn to_rust(
self,
ctx: Self::Context,
options: Self::Options,
symbols: Self::SymbolTable,
) -> std::result::Result<TokenStream, Box<dyn std::error::Error>> {
let mut params = Vec::new();
for arg in self.posonlyargs {
let param = arg.to_rust(ctx.clone(), options.clone(), symbols.clone())?;
params.push(param);
}
for arg in self.args {
let param = arg.to_rust(ctx.clone(), options.clone(), symbols.clone())?;
params.push(param);
}
if let Some(vararg) = self.vararg {
let vararg_name = crate::python_ast::safe_ident(&vararg.arg);
params.push(quote!(#vararg_name: impl IntoIterator<Item = impl Into<PyObject>>));
}
for arg in self.kwonlyargs {
let param = arg.to_rust(ctx.clone(), options.clone(), symbols.clone())?;
params.push(param);
}
if let Some(kwarg) = self.kwarg {
let kwarg_name = crate::python_ast::safe_ident(&kwarg.arg);
params.push(quote!(#kwarg_name: impl IntoIterator<Item = (impl AsRef<str>, impl Into<PyObject>)>));
}
Ok(quote!(#(#params),*))
}
}
impl CodeGen for CallArguments {
type Context = CodeGenContext;
type Options = PythonOptions;
type SymbolTable = SymbolTableScopes;
fn to_rust(
self,
ctx: Self::Context,
options: Self::Options,
symbols: Self::SymbolTable,
) -> std::result::Result<TokenStream, Box<dyn std::error::Error>> {
let mut all_args = Vec::new();
for arg in self.args {
let rust_arg = arg.to_rust(ctx.clone(), options.clone(), symbols.clone())?;
all_args.push(rust_arg);
}
for keyword in self.keywords {
let rust_kw = keyword.to_rust(ctx.clone(), options.clone(), symbols.clone())?;
all_args.push(rust_kw);
}
Ok(quote!(#(#all_args),*))
}
}
impl Node for Argument {
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 Node for Parameter {
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
}
}
#[cfg(python_ast_upstream_tests)]
mod tests {
use super::*;
use crate::python_ast::{CodeGenContext, ExprType, PythonOptions, SymbolTableScopes, parse};
use test_log::test;
#[test]
fn test_simple_function_call() {
let code = "func(1, 2, 3)";
let result = parse(code, "test.py").unwrap();
let options = PythonOptions::default();
let symbols = SymbolTableScopes::new();
let _rust_code = result.to_rust(CodeGenContext::Module("test".to_string()), options, symbols).unwrap();
}
#[test]
fn test_keyword_arguments() {
let code = "def func(a, b):\n pass\n\nfunc(b=2, a=1)";
let result = parse(code, "test.py").unwrap();
let options = PythonOptions::default();
let symbols = result.clone().find_symbols(SymbolTableScopes::new());
let rust_code = result.to_rust(CodeGenContext::Module("test".to_string()), options, symbols).unwrap().to_string();
assert!(rust_code.contains("func (1 , 2)"), "generated: {}", rust_code);
}
#[test]
fn test_mixed_arguments() {
let code = "def func(a, b, c, d):\n pass\n\nfunc(1, 2, d=4, c=3)";
let result = parse(code, "test.py").unwrap();
let options = PythonOptions::default();
let symbols = result.clone().find_symbols(SymbolTableScopes::new());
let rust_code = result.to_rust(CodeGenContext::Module("test".to_string()), options, symbols).unwrap().to_string();
assert!(rust_code.contains("func (1 , 2 , 3 , 4)"), "generated: {}", rust_code);
}
#[test]
fn test_function_with_defaults() {
let code = r#"
def func(a, b=2, c=3):
pass
"#;
let result = parse(code, "test.py").unwrap();
let options = PythonOptions::default();
let symbols = SymbolTableScopes::new();
let _rust_code = result.to_rust(CodeGenContext::Module("test".to_string()), options, symbols).unwrap();
}
#[test]
fn test_function_with_varargs() {
let code = r#"
def func(a, *args):
pass
"#;
let result = parse(code, "test.py").unwrap();
let options = PythonOptions::default();
let symbols = SymbolTableScopes::new();
let _rust_code = result.to_rust(CodeGenContext::Module("test".to_string()), options, symbols).unwrap();
}
#[test]
fn test_function_with_kwargs() {
let code = r#"
def func(a, **kwargs):
pass
"#;
let result = parse(code, "test.py").unwrap();
let options = PythonOptions::default();
let symbols = SymbolTableScopes::new();
let _rust_code = result.to_rust(CodeGenContext::Module("test".to_string()), options, symbols).unwrap();
}
#[test]
fn test_complex_function_signature() {
let code = r#"
def func(a, b=2, *args, c, d=4, **kwargs):
pass
"#;
let result = parse(code, "test.py").unwrap();
let options = PythonOptions::default();
let symbols = SymbolTableScopes::new();
let _rust_code = result.to_rust(CodeGenContext::Module("test".to_string()), options, symbols).unwrap();
}
#[test]
fn test_keyword_only_arguments() {
let code = r#"
def func(a, *, b, c=3):
pass
"#;
let result = parse(code, "test.py").unwrap();
let options = PythonOptions::default();
let symbols = SymbolTableScopes::new();
let _rust_code = result.to_rust(CodeGenContext::Module("test".to_string()), options, symbols).unwrap();
}
#[test]
fn test_argument_unpacking_call() {
let code = "func(*args, **kwargs)";
let result = parse(code, "test.py");
match result {
Ok(ast) => {
let options = PythonOptions::default();
let symbols = SymbolTableScopes::new();
let rust_code = ast.to_rust(CodeGenContext::Module("test".to_string()), options, symbols);
match rust_code {
Ok(_code) => { }
Err(_e) => { }
}
}
Err(_e) => { }
}
}
#[test]
fn test_arg_with_constant() {
use litrs::Literal;
let literal = Literal::parse("42").unwrap().into_owned();
let constant = crate::python_ast::Constant(Some(literal));
let arg: Arg = ExprType::Constant(constant);
let options = PythonOptions::default();
let symbols = SymbolTableScopes::new();
let rust_code = arg.to_rust(CodeGenContext::Module("test".to_string()), options, symbols).unwrap();
assert!(rust_code.to_string().contains("42"));
}
#[test]
fn test_arg_with_name() {
let name_expr = ExprType::Name(crate::python_ast::Name { id: "variable".to_string() });
let arg: Arg = name_expr;
let options = PythonOptions::default();
let symbols = SymbolTableScopes::new();
let rust_code = arg.to_rust(CodeGenContext::Module("test".to_string()), options, symbols).unwrap();
assert!(rust_code.to_string().contains("variable"));
}
}