use crate::core::FunctionMetrics;
use std::path::Path;
use syn::ItemFn;
pub struct RustFunctionContext<'a> {
pub item_fn: &'a ItemFn,
pub metrics: Option<&'a FunctionMetrics>,
pub impl_context: Option<ImplContext>,
pub file_path: &'a Path,
}
#[derive(Clone, Debug)]
pub struct ImplContext {
pub impl_type: String,
pub is_trait_impl: bool,
pub trait_name: Option<String>,
}
impl<'a> RustFunctionContext<'a> {
pub fn from_item_fn(item_fn: &'a ItemFn, file_path: &'a Path) -> Self {
Self {
item_fn,
metrics: None,
impl_context: None,
file_path,
}
}
pub fn with_impl_context(mut self, ctx: ImplContext) -> Self {
self.impl_context = Some(ctx);
self
}
pub fn is_async(&self) -> bool {
self.item_fn.sig.asyncness.is_some()
}
pub fn body(&self) -> &syn::Block {
&self.item_fn.block
}
#[allow(dead_code, unused_imports)]
pub fn body_text(&self) -> String {
use quote::ToTokens;
quote::quote!(#(self.item_fn.block)).to_string()
}
pub fn is_trait_impl(&self) -> bool {
self.impl_context
.as_ref()
.map(|ctx| ctx.is_trait_impl)
.unwrap_or(false)
}
pub fn trait_name(&self) -> Option<&str> {
self.impl_context
.as_ref()
.and_then(|ctx| ctx.trait_name.as_deref())
}
}