edgee_components_runtime/data_collection/versions/v1_0_1/
mod.rs1mod convert;
2pub mod execute;
3use crate::config::DataCollectionComponents;
4use crate::context::ComponentsContext;
5use crate::context::HostState;
6use crate::data_collection::versions::v1_0_1::data_collection::{
7 DataCollectionV101, DataCollectionV101Pre,
8};
9use wasmtime::{
10 component::{Component, Linker},
11 Engine, Store,
12};
13
14pub mod data_collection {
15 wasmtime::component::bindgen!({
16 world: "data-collection-v101",
17 path: "src/data_collection/wit",
18 imports: {
19 default: async | trappable,
20 },
21 exports: {
22 default: async | trappable,
23 },
24 });
25}
26
27pub fn pre_instanciate_data_collection_component_1_0_1(
28 engine: &Engine,
29 component_config: &DataCollectionComponents,
30) -> anyhow::Result<DataCollectionV101Pre<HostState>> {
31 let mut linker = Linker::new(engine);
32 wasmtime_wasi::p2::add_to_linker_async(&mut linker)?;
33
34 let span = tracing::info_span!("component-context", component = %component_config.id, category = "data-collection");
35 let _span = span.enter();
36
37 tracing::debug!("Loading new data collection component");
38
39 let component = Component::from_file(engine, &component_config.file)?;
40 let instance_pre = linker.instantiate_pre(&component)?;
41 let instance_pre = DataCollectionV101Pre::new(instance_pre)?;
42
43 tracing::debug!("loaded new data collection component");
44
45 Ok(instance_pre)
46}
47
48impl ComponentsContext {
49 pub fn pre_instanciate_data_collection_1_0_1_component(
50 &self,
51 component_config: DataCollectionComponents,
52 ) -> anyhow::Result<DataCollectionV101Pre<HostState>> {
53 let instance_pre =
54 pre_instanciate_data_collection_component_1_0_1(&self.engine, &component_config)?;
55 Ok(instance_pre)
56 }
57
58 pub fn add_data_collection_1_0_1_component(
59 &mut self,
60 component_config: DataCollectionComponents,
61 instance_pre: DataCollectionV101Pre<HostState>,
62 ) {
63 if !self
64 .components
65 .data_collection_1_0_1
66 .contains_key(&component_config.id)
67 {
68 self.components
69 .data_collection_1_0_1
70 .insert(component_config.id.clone(), instance_pre);
71 }
72 }
73
74 pub async fn get_data_collection_1_0_1_instance(
75 &self,
76 id: &str,
77 store: &mut Store<HostState>,
78 ) -> anyhow::Result<DataCollectionV101> {
79 let instance_pre = self.components.data_collection_1_0_1.get(id);
80
81 if instance_pre.is_none() {
82 return Err(anyhow::anyhow!("component not found: {}", id));
83 }
84
85 instance_pre.unwrap().instantiate_async(store).await
86 }
87}