mago_codex/identifier/
function_like.rs1use mago_database::file::File;
2
3use mago_span::Span;
4use mago_word::Word;
5
6use crate::identifier::method::MethodIdentifier;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
13#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
14pub enum FunctionLikeIdentifier {
15 Function(Word),
18 Method(Word, Word),
22 Closure(Word),
30}
31
32impl FunctionLikeIdentifier {
33 #[inline]
34 #[must_use]
35 pub fn for_closure(file: &File, span: Span) -> Self {
36 Self::Closure(crate::build_synthetic_name("closure", file, span))
37 }
38
39 #[inline]
41 #[must_use]
42 pub const fn is_function(&self) -> bool {
43 matches!(self, FunctionLikeIdentifier::Function(_))
44 }
45
46 #[inline]
48 #[must_use]
49 pub const fn is_method(&self) -> bool {
50 matches!(self, FunctionLikeIdentifier::Method(_, _))
51 }
52
53 #[inline]
55 #[must_use]
56 pub const fn is_closure(&self) -> bool {
57 matches!(self, FunctionLikeIdentifier::Closure(_))
58 }
59
60 #[inline]
63 #[must_use]
64 pub const fn as_method_identifier(&self) -> Option<MethodIdentifier> {
65 match self {
66 FunctionLikeIdentifier::Method(fq_classlike_name, method_name) => {
67 Some(MethodIdentifier::new(*fq_classlike_name, *method_name))
68 }
69 _ => None,
70 }
71 }
72
73 #[inline]
75 #[must_use]
76 pub const fn title_kind_str(&self) -> &'static str {
77 match self {
78 FunctionLikeIdentifier::Function(_) => "Function",
79 FunctionLikeIdentifier::Method(_, _) => "Method",
80 FunctionLikeIdentifier::Closure(_) => "Closure",
81 }
82 }
83
84 #[inline]
86 #[must_use]
87 pub const fn kind_str(&self) -> &'static str {
88 match self {
89 FunctionLikeIdentifier::Function(_) => "function",
90 FunctionLikeIdentifier::Method(_, _) => "method",
91 FunctionLikeIdentifier::Closure(_) => "closure",
92 }
93 }
94
95 #[inline]
100 #[must_use]
101 pub fn as_string(&self) -> String {
102 match self {
103 FunctionLikeIdentifier::Function(fn_name) => fn_name.to_string(),
104 FunctionLikeIdentifier::Method(fq_classlike_name, method_name) => {
105 format!("{fq_classlike_name}::{method_name}")
106 }
107 FunctionLikeIdentifier::Closure(name) => name.to_string(),
108 }
109 }
110
111 #[inline]
113 #[must_use]
114 pub fn to_hash(&self) -> String {
115 self.as_string()
116 }
117}
118
119impl From<MethodIdentifier> for FunctionLikeIdentifier {
120 #[inline]
121 fn from(value: MethodIdentifier) -> Self {
122 FunctionLikeIdentifier::Method(value.get_class_name(), value.get_method_name())
123 }
124}