use mago_database::file::File;
use mago_span::Span;
use mago_word::Word;
use crate::identifier::method::MethodIdentifier;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum FunctionLikeIdentifier {
Function(Word),
Method(Word, Word),
Closure(Word),
}
impl FunctionLikeIdentifier {
#[inline]
#[must_use]
pub fn for_closure(file: &File, span: Span) -> Self {
Self::Closure(crate::build_synthetic_name("closure", file, span))
}
#[inline]
#[must_use]
pub const fn is_function(&self) -> bool {
matches!(self, FunctionLikeIdentifier::Function(_))
}
#[inline]
#[must_use]
pub const fn is_method(&self) -> bool {
matches!(self, FunctionLikeIdentifier::Method(_, _))
}
#[inline]
#[must_use]
pub const fn is_closure(&self) -> bool {
matches!(self, FunctionLikeIdentifier::Closure(_))
}
#[inline]
#[must_use]
pub const fn as_method_identifier(&self) -> Option<MethodIdentifier> {
match self {
FunctionLikeIdentifier::Method(fq_classlike_name, method_name) => {
Some(MethodIdentifier::new(*fq_classlike_name, *method_name))
}
_ => None,
}
}
#[inline]
#[must_use]
pub const fn title_kind_str(&self) -> &'static str {
match self {
FunctionLikeIdentifier::Function(_) => "Function",
FunctionLikeIdentifier::Method(_, _) => "Method",
FunctionLikeIdentifier::Closure(_) => "Closure",
}
}
#[inline]
#[must_use]
pub const fn kind_str(&self) -> &'static str {
match self {
FunctionLikeIdentifier::Function(_) => "function",
FunctionLikeIdentifier::Method(_, _) => "method",
FunctionLikeIdentifier::Closure(_) => "closure",
}
}
#[inline]
#[must_use]
pub fn as_string(&self) -> String {
match self {
FunctionLikeIdentifier::Function(fn_name) => fn_name.to_string(),
FunctionLikeIdentifier::Method(fq_classlike_name, method_name) => {
format!("{fq_classlike_name}::{method_name}")
}
FunctionLikeIdentifier::Closure(name) => name.to_string(),
}
}
#[inline]
#[must_use]
pub fn to_hash(&self) -> String {
self.as_string()
}
}
impl From<MethodIdentifier> for FunctionLikeIdentifier {
#[inline]
fn from(value: MethodIdentifier) -> Self {
FunctionLikeIdentifier::Method(value.get_class_name(), value.get_method_name())
}
}