use crate::utils::path_ident;
use syn::{FnArg, Receiver, ReturnType, Signature};
#[derive(Debug)]
pub(crate) enum FunctionKind {
Static,
Constructor(HeapTy),
Method,
Builder(HeapTy),
}
#[derive(Debug)]
pub(crate) enum HeapTy {
None,
Box,
}
impl FunctionKind {
pub(crate) fn from_fn(sig: &Signature, self_ident: &syn::Ident) -> Self {
if let ReturnType::Type(_, ty) = &sig.output {
if let syn::Type::Path(type_path) = &**ty {
let ty_path = path_ident(&type_path.path);
if ty_path == "Self" || ty_path == self_ident.to_string() {
match sig.inputs.first() {
Some(FnArg::Receiver(Receiver { .. })) => {
return Self::Builder(HeapTy::None)
}
_ => return Self::Constructor(HeapTy::None),
};
}
if ty_path == "Box < Self >" {
match sig.inputs.first() {
Some(FnArg::Receiver(Receiver { .. })) => {
return Self::Builder(HeapTy::Box)
}
_ => return Self::Constructor(HeapTy::Box),
};
}
}
}
match sig.inputs.first() {
Some(FnArg::Receiver(Receiver { .. })) => Self::Method,
_ => Self::Static,
}
}
}