use log::debug;
use proc_macro2::TokenStream;
use pyo3::{Bound, FromPyObject, PyAny, PyResult, prelude::PyAnyMethods};
use quote::{format_ident, quote};
use serde::{Deserialize, Serialize};
use crate::ast::tree::statement::PyStatementTrait;
use crate::{
CodeGen, CodeGenContext, ExprType, Object, ParameterList, PythonOptions, Statement,
StatementType, SymbolTableNode, SymbolTableScopes,
};
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct FunctionDef {
pub name: String,
pub args: ParameterList,
pub body: Vec<Statement>,
pub decorator_list: Vec<ExprType>,
}
impl<'a> FromPyObject<'a> for FunctionDef {
fn extract_bound(ob: &Bound<'a, PyAny>) -> PyResult<Self> {
let name: String = ob.getattr("name")?.extract()?;
let args: ParameterList = ob.getattr("args")?.extract()?;
let body: Vec<Statement> = ob.getattr("body")?.extract()?;
let decorator_list: Vec<ExprType> = ob.getattr("decorator_list")?.extract().unwrap_or_default();
Ok(FunctionDef {
name,
args,
body,
decorator_list,
})
}
}
impl PyStatementTrait for FunctionDef {
}
impl CodeGen for FunctionDef {
type Context = CodeGenContext;
type Options = PythonOptions;
type SymbolTable = SymbolTableScopes;
fn find_symbols(self, symbols: Self::SymbolTable) -> Self::SymbolTable {
let mut symbols = symbols;
symbols.insert(
self.name.clone(),
SymbolTableNode::FunctionDef(self.clone()),
);
symbols
}
fn to_rust(
self,
ctx: Self::Context,
options: Self::Options,
symbols: SymbolTableScopes,
) -> Result<TokenStream, Box<dyn std::error::Error>> {
let mut streams = TokenStream::new();
let fn_name = format_ident!("{}", self.name);
let visibility = if self.name.starts_with("_") && !self.name.starts_with("__") {
quote!() } else if self.name.starts_with("__") && self.name.ends_with("__") {
quote!(pub(crate)) } else {
quote!(pub) };
let is_async = match ctx.clone() {
CodeGenContext::Async(_) => {
quote!(async)
}
_ => quote!(),
};
let parameters = self
.args
.clone()
.to_rust(ctx.clone(), options.clone(), symbols.clone())
.expect(format!("parsing arguments {:?}", self.args).as_str());
for s in self.body.iter() {
streams.extend(
s.clone()
.to_rust(ctx.clone(), options.clone(), symbols.clone())
.expect(format!("parsing statement {:?}", s).as_str()),
);
streams.extend(quote!(;));
}
let function = if let Some(docstring) = self.get_docstring() {
let doc_lines: Vec<_> = docstring
.lines()
.map(|line| {
if line.trim().is_empty() {
quote! { #[doc = ""] }
} else {
let doc_line = format!("{}", line);
quote! { #[doc = #doc_line] }
}
})
.collect();
quote! {
#(#doc_lines)*
#visibility #is_async fn #fn_name(#parameters) {
#streams
}
}
} else {
quote! {
#visibility #is_async fn #fn_name(#parameters) {
#streams
}
}
};
debug!("function: {}", function);
Ok(function)
}
}
impl FunctionDef {
fn get_docstring(&self) -> Option<String> {
if self.body.is_empty() {
return None;
}
let expr = self.body[0].clone();
match expr.statement {
StatementType::Expr(e) => match e.value {
ExprType::Constant(c) => {
let raw_string = c.to_string();
Some(self.format_docstring(&raw_string))
},
_ => None,
},
_ => None,
}
}
fn format_docstring(&self, raw: &str) -> String {
let content = raw.trim_matches('"');
let lines: Vec<&str> = content.lines().collect();
if lines.is_empty() {
return String::new();
}
let mut formatted = vec![lines[0].trim().to_string()];
if lines.len() > 1 {
if !lines[0].trim().is_empty() && !lines[1].trim().is_empty() {
formatted.push(String::new());
}
for line in lines.iter().skip(1) {
let cleaned = line.trim();
if cleaned.starts_with("Args:") {
formatted.push(String::new());
formatted.push("# Arguments".to_string());
} else if cleaned.starts_with("Returns:") {
formatted.push(String::new());
formatted.push("# Returns".to_string());
} else if cleaned.starts_with("Example:") {
formatted.push(String::new());
formatted.push("# Examples".to_string());
} else if cleaned.starts_with(">>>") {
formatted.push(format!("```rust"));
formatted.push(format!("// {}", cleaned));
} else if !cleaned.is_empty() {
formatted.push(cleaned.to_string());
}
}
if content.contains(">>>") {
formatted.push("```".to_string());
}
}
formatted.join("\n")
}
}
impl Object for FunctionDef {}