edgee_components_runtime/data_collection/versions/v1_0_0/
mod.rs

1mod convert;
2pub mod execute;
3use crate::config::DataCollectionComponents;
4use crate::context::ComponentsContext;
5use crate::context::HostState;
6use crate::data_collection::versions::v1_0_0::data_collection::{
7    DataCollectionV100, DataCollectionV100Pre,
8};
9use wasmtime::{
10    component::{Component, Linker},
11    Engine, Store,
12};
13
14pub mod data_collection {
15    wasmtime::component::bindgen!({
16        world: "data-collection-v100",
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_0(
28    engine: &Engine,
29    component_config: &DataCollectionComponents,
30) -> anyhow::Result<DataCollectionV100Pre<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 = DataCollectionV100Pre::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_0_component(
50        &self,
51        component_config: DataCollectionComponents,
52    ) -> anyhow::Result<DataCollectionV100Pre<HostState>> {
53        let instance_pre =
54            pre_instanciate_data_collection_component_1_0_0(&self.engine, &component_config)?;
55        Ok(instance_pre)
56    }
57
58    pub fn add_data_collection_1_0_0_component(
59        &mut self,
60        component_config: DataCollectionComponents,
61        instance_pre: DataCollectionV100Pre<HostState>,
62    ) {
63        if !self
64            .components
65            .data_collection_1_0_0
66            .contains_key(&component_config.id)
67        {
68            self.components
69                .data_collection_1_0_0
70                .insert(component_config.id.clone(), instance_pre);
71        }
72    }
73
74    pub async fn get_data_collection_1_0_0_instance(
75        &self,
76        id: &str,
77        store: &mut Store<HostState>,
78    ) -> anyhow::Result<DataCollectionV100> {
79        let instance_pre = self.components.data_collection_1_0_0.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}