wasmcloud_runtime/component/
bus1_0_0.rs

1//! Compatibility implementation of the `wasmcloud:bus/lattice@1.0.0` interface
2use std::sync::Arc;
3
4use anyhow::Context as _;
5use async_trait::async_trait;
6use tracing::instrument;
7use wasmcloud_core::CallTargetInterface;
8use wasmtime::component::Resource;
9
10use crate::capability::bus1_0_0::lattice;
11
12use super::{Ctx, Handler, TableResult};
13
14#[async_trait]
15/// `wasmcloud:bus/lattice@1.0.0` implementation
16pub trait Bus {
17    /// Set the link name to use for a given list of interfaces
18    async fn set_link_name(&self, link_name: String, interfaces: Vec<Arc<CallTargetInterface>>);
19}
20
21impl<H: Handler> lattice::Host for Ctx<H> {
22    #[instrument(level = "debug", skip_all)]
23    async fn set_link_name(
24        &mut self,
25        link_name: String,
26        interfaces: Vec<Resource<Arc<CallTargetInterface>>>,
27    ) -> anyhow::Result<()> {
28        self.attach_parent_context();
29        let interfaces = interfaces
30            .into_iter()
31            .map(|interface| self.table.get(&interface).cloned())
32            .collect::<TableResult<_>>()
33            .context("failed to convert call target interfaces")?;
34        // NOTE: We're try-unwrapping the outer error, the inner Result should be ignored.
35        let _ = self.handler.set_link_name(link_name, interfaces).await?;
36        Ok(())
37    }
38}
39
40impl<H: Handler> lattice::HostCallTargetInterface for Ctx<H> {
41    async fn new(
42        &mut self,
43        namespace: String,
44        package: String,
45        interface: String,
46    ) -> anyhow::Result<Resource<Arc<CallTargetInterface>>> {
47        self.table
48            .push(Arc::new(CallTargetInterface {
49                namespace,
50                package,
51                interface,
52            }))
53            .context("failed to push target interface")
54    }
55
56    async fn drop(&mut self, interface: Resource<Arc<CallTargetInterface>>) -> anyhow::Result<()> {
57        self.table.delete(interface)?;
58        Ok(())
59    }
60}