rib/registry/
component_dependency.rs1use crate::wit_type::TypeEnum;
16use crate::wit_type::{TypeVariant, WitExport};
17use crate::{
18 ComponentDependencyKey, Expr, FunctionDictionary, FunctionName, FunctionType,
19 FunctionTypeRegistry, InstanceCreationType,
20};
21
22#[derive(Debug, Default, Hash, Clone, Eq, PartialEq, PartialOrd, Ord)]
24pub struct ComponentDependency {
25 pub key: ComponentDependencyKey,
26 pub function_dictionary: FunctionDictionary,
27}
28
29impl ComponentDependency {
30 pub fn from_wit_metadata(
31 key: ComponentDependencyKey,
32 exports: &[WitExport],
33 ) -> Result<Self, String> {
34 let function_type_registry = FunctionTypeRegistry::from_export_metadata(exports);
35 let function_dictionary =
36 FunctionDictionary::from_function_type_registry(&function_type_registry)?;
37 Ok(ComponentDependency {
38 key,
39 function_dictionary,
40 })
41 }
42
43 pub fn get_variants(&self) -> Vec<TypeVariant> {
44 self.function_dictionary.get_all_variants()
45 }
46
47 pub fn get_enums(&self) -> Vec<TypeEnum> {
48 self.function_dictionary.get_all_enums()
49 }
50
51 pub fn get_function_type(
52 &self,
53 function_name: &FunctionName,
54 ) -> Result<(ComponentDependencyKey, FunctionType), String> {
55 let types: Vec<&FunctionType> = self
56 .function_dictionary
57 .name_and_types
58 .iter()
59 .filter_map(|(f_name, function_type)| {
60 if f_name == function_name {
61 Some(function_type)
62 } else {
63 None
64 }
65 })
66 .collect();
67
68 if types.is_empty() {
69 Err("unknown function".to_string())
70 } else {
71 Ok((self.key.clone(), types[0].clone()))
72 }
73 }
74
75 pub fn narrow_to_component(&mut self, _component_dependency_key: &ComponentDependencyKey) {}
77
78 pub fn function_dictionary(&self) -> Vec<&FunctionDictionary> {
79 vec![&self.function_dictionary]
80 }
81
82 pub fn get_worker_instance_type(
83 &self,
84 worker_name: Option<Expr>,
85 ) -> Result<InstanceCreationType, String> {
86 Ok(InstanceCreationType::WitWorker {
87 component_info: None,
88 worker_name: worker_name.map(Box::new),
89 })
90 }
91}