mago_codex/identifier/
function_like.rs1use serde::Deserialize;
2use serde::Serialize;
3
4use mago_interner::StringIdentifier;
5use mago_interner::ThreadedInterner;
6use mago_span::Position;
7
8use crate::identifier::method::MethodIdentifier;
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, PartialOrd, Ord)]
15pub enum FunctionLikeIdentifier {
16 Function(StringIdentifier),
19 Method(StringIdentifier, StringIdentifier),
23 Closure(Position),
26}
27
28impl FunctionLikeIdentifier {
29 #[inline]
31 pub const fn is_function(&self) -> bool {
32 matches!(self, FunctionLikeIdentifier::Function(_))
33 }
34
35 #[inline]
37 pub const fn is_method(&self) -> bool {
38 matches!(self, FunctionLikeIdentifier::Method(_, _))
39 }
40
41 #[inline]
43 pub const fn is_closure(&self) -> bool {
44 matches!(self, FunctionLikeIdentifier::Closure(_))
45 }
46
47 #[inline]
50 pub const fn as_method_identifier(&self) -> Option<MethodIdentifier> {
51 match self {
52 FunctionLikeIdentifier::Method(fq_classlike_name, method_name) => {
53 Some(MethodIdentifier::new(*fq_classlike_name, *method_name))
54 }
55 _ => None,
56 }
57 }
58
59 #[inline]
61 pub const fn title_kind_str(&self) -> &'static str {
62 match self {
63 FunctionLikeIdentifier::Function(_) => "Function",
64 FunctionLikeIdentifier::Method(_, _) => "Method",
65 FunctionLikeIdentifier::Closure(_) => "Closure",
66 }
67 }
68
69 #[inline]
71 pub const fn kind_str(&self) -> &'static str {
72 match self {
73 FunctionLikeIdentifier::Function(_) => "function",
74 FunctionLikeIdentifier::Method(_, _) => "method",
75 FunctionLikeIdentifier::Closure(_) => "closure",
76 }
77 }
78
79 #[inline]
87 pub fn as_string(&self, interner: &ThreadedInterner) -> String {
88 match self {
89 FunctionLikeIdentifier::Function(fn_name) => interner.lookup(fn_name).to_string(),
90 FunctionLikeIdentifier::Method(fq_classlike_name, method_name) => {
91 format!("{}::{}", interner.lookup(fq_classlike_name), interner.lookup(method_name))
92 }
93 FunctionLikeIdentifier::Closure(position) => {
94 format!("{}:{}", interner.lookup(&position.source.value()), position.offset)
95 }
96 }
97 }
98
99 #[inline]
105 pub fn to_hash(&self) -> String {
106 match self {
107 FunctionLikeIdentifier::Function(fn_name) => fn_name.to_string(),
108 FunctionLikeIdentifier::Method(fq_classlike_name, method_name) => {
109 format!("{fq_classlike_name}::{method_name}")
110 }
111 FunctionLikeIdentifier::Closure(position) => {
112 format!("{}::{}", position.source.value(), position.offset)
113 }
114 }
115 }
116}