Skip to main content

rib/registry/
component_dependency.rs

1use crate::wit_type::TypeEnum;
2use crate::wit_type::{TypeVariant, WitExport};
3use crate::{
4    ComponentDependencyKey, Expr, FunctionDictionary, FunctionName, FunctionType,
5    FunctionTypeRegistry, InstanceCreationType,
6};
7
8/// Single Wasm component: identity plus resolved export surface as a function dictionary.
9#[derive(Debug, Default, Hash, Clone, Eq, PartialEq, PartialOrd, Ord)]
10pub struct ComponentDependency {
11    pub key: ComponentDependencyKey,
12    pub function_dictionary: FunctionDictionary,
13}
14
15impl ComponentDependency {
16    pub fn from_wit_metadata(
17        key: ComponentDependencyKey,
18        exports: &[WitExport],
19    ) -> Result<Self, String> {
20        let function_type_registry = FunctionTypeRegistry::from_export_metadata(exports);
21        let function_dictionary =
22            FunctionDictionary::from_function_type_registry(&function_type_registry)?;
23        Ok(ComponentDependency {
24            key,
25            function_dictionary,
26        })
27    }
28
29    pub fn get_variants(&self) -> Vec<TypeVariant> {
30        self.function_dictionary.get_all_variants()
31    }
32
33    pub fn get_enums(&self) -> Vec<TypeEnum> {
34        self.function_dictionary.get_all_enums()
35    }
36
37    pub fn get_function_type(
38        &self,
39        function_name: &FunctionName,
40    ) -> Result<(ComponentDependencyKey, FunctionType), String> {
41        let types: Vec<&FunctionType> = self
42            .function_dictionary
43            .name_and_types
44            .iter()
45            .filter_map(|(f_name, function_type)| {
46                if f_name == function_name {
47                    Some(function_type)
48                } else {
49                    None
50                }
51            })
52            .collect();
53
54        if types.is_empty() {
55            Err("unknown function".to_string())
56        } else {
57            Ok((self.key.clone(), types[0].clone()))
58        }
59    }
60
61    /// No-op; kept so narrowing call sites stay stable.
62    pub fn narrow_to_component(&mut self, _component_dependency_key: &ComponentDependencyKey) {}
63
64    pub fn function_dictionary(&self) -> Vec<&FunctionDictionary> {
65        vec![&self.function_dictionary]
66    }
67
68    pub fn get_worker_instance_type(
69        &self,
70        worker_name: Option<Expr>,
71    ) -> Result<InstanceCreationType, String> {
72        Ok(InstanceCreationType::WitWorker {
73            component_info: None,
74            worker_name: worker_name.map(Box::new),
75        })
76    }
77}