1use crate::{ComponentDependencyKey, DynamicParsedFunctionName, Expr};
16use crate::{FullyQualifiedResourceConstructor, VariableId};
17use std::fmt::Display;
18
19#[derive(Debug, Hash, PartialEq, Eq, Clone, Ord, PartialOrd)]
20pub enum CallType {
21 Function {
22 component_info: Option<ComponentDependencyKey>,
23 instance_identifier: Option<Box<InstanceIdentifier>>,
26 function_name: DynamicParsedFunctionName,
29 },
30 VariantConstructor(String),
31 EnumConstructor(String),
32 InstanceCreation(InstanceCreationType),
33}
34
35#[derive(Debug, Hash, PartialEq, Eq, Clone, Ord, PartialOrd)]
38pub enum InstanceIdentifier {
39 WitWorker {
40 variable_id: Option<VariableId>,
41 worker_name: Option<Box<Expr>>,
42 },
43
44 WitResource {
45 variable_id: Option<VariableId>,
46 worker_name: Option<Box<Expr>>,
47 resource_name: String,
48 },
49}
50
51impl InstanceIdentifier {
52 pub fn worker_name_mut(&mut self) -> Option<&mut Box<Expr>> {
53 match self {
54 InstanceIdentifier::WitWorker { worker_name, .. } => worker_name.as_mut(),
55 InstanceIdentifier::WitResource { worker_name, .. } => worker_name.as_mut(),
56 }
57 }
58 pub fn worker_name(&self) -> Option<&Expr> {
59 match self {
60 InstanceIdentifier::WitWorker { worker_name, .. } => worker_name.as_deref(),
61 InstanceIdentifier::WitResource { worker_name, .. } => worker_name.as_deref(),
62 }
63 }
64}
65
66#[derive(Debug, Hash, PartialEq, Eq, Clone, Ord, PartialOrd)]
67pub enum InstanceCreationType {
68 WitWorker {
70 component_info: Option<ComponentDependencyKey>,
71 worker_name: Option<Box<Expr>>,
72 },
73 WitResource {
78 component_info: Option<ComponentDependencyKey>,
79 module: Option<InstanceIdentifier>,
82 resource_name: FullyQualifiedResourceConstructor,
83 },
84}
85
86impl InstanceCreationType {
87 pub fn worker_name(&self) -> Option<Expr> {
88 match self {
89 InstanceCreationType::WitWorker { worker_name, .. } => worker_name.as_deref().cloned(),
90 InstanceCreationType::WitResource { module, .. } => {
91 let r = module.as_ref().and_then(|m| m.worker_name());
92 r.cloned()
93 }
94 }
95 }
96}
97
98impl CallType {
99 pub fn function_name(&self) -> Option<DynamicParsedFunctionName> {
100 match self {
101 CallType::Function { function_name, .. } => Some(function_name.clone()),
102 _ => None,
103 }
104 }
105 pub fn worker_expr(&self) -> Option<&Expr> {
106 match self {
107 CallType::Function {
108 instance_identifier,
109 ..
110 } => {
111 let module = instance_identifier.as_ref()?;
112 module.worker_name()
113 }
114 _ => None,
115 }
116 }
117
118 pub fn function_call(
119 function: DynamicParsedFunctionName,
120 component_info: Option<ComponentDependencyKey>,
121 ) -> CallType {
122 CallType::Function {
123 instance_identifier: None,
124 function_name: function,
125 component_info,
126 }
127 }
128
129 pub fn function_call_with_worker(
130 module: InstanceIdentifier,
131 function: DynamicParsedFunctionName,
132 component_info: Option<ComponentDependencyKey>,
133 ) -> CallType {
134 CallType::Function {
135 instance_identifier: Some(Box::new(module)),
136 function_name: function,
137 component_info,
138 }
139 }
140
141 pub fn is_resource_method(&self) -> bool {
142 match self {
143 CallType::Function { function_name, .. } => function_name
144 .to_parsed_function_name()
145 .function
146 .resource_method_name()
147 .is_some(),
148 _ => false,
149 }
150 }
151}
152
153impl Display for CallType {
154 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
155 match self {
156 CallType::Function { function_name, .. } => write!(f, "{function_name}"),
157 CallType::VariantConstructor(name) => write!(f, "{name}"),
158 CallType::EnumConstructor(name) => write!(f, "{name}"),
159 CallType::InstanceCreation(instance_creation_type) => match instance_creation_type {
160 InstanceCreationType::WitWorker { .. } => {
161 write!(f, "instance")
162 }
163 InstanceCreationType::WitResource { resource_name, .. } => {
164 write!(f, "{}", resource_name.resource_name)
165 }
166 },
167 }
168 }
169}