wasmcloud_runtime/component/
config.rs

1use async_trait::async_trait;
2use tracing::instrument;
3
4use crate::capability::config::{self, runtime, store};
5
6use super::{Ctx, Handler};
7
8/// `wasi:config/store` implementation
9#[async_trait]
10pub trait Config {
11    /// Handle `wasi:config/store.get`
12    async fn get(&self, key: &str) -> anyhow::Result<Result<Option<String>, config::store::Error>>;
13
14    /// Handle `wasi:config/store.get_all`
15    async fn get_all(&self) -> anyhow::Result<Result<Vec<(String, String)>, config::store::Error>>;
16}
17
18impl<H: Handler> store::Host for Ctx<H> {
19    #[instrument(skip(self))]
20    async fn get(
21        &mut self,
22        key: String,
23    ) -> anyhow::Result<Result<Option<String>, config::store::Error>> {
24        self.attach_parent_context();
25        Config::get(&self.handler, &key).await
26    }
27
28    #[instrument(skip_all)]
29    async fn get_all(
30        &mut self,
31    ) -> anyhow::Result<Result<Vec<(String, String)>, config::store::Error>> {
32        self.attach_parent_context();
33        self.handler.get_all().await
34    }
35}
36
37impl From<config::store::Error> for config::runtime::ConfigError {
38    fn from(err: config::store::Error) -> Self {
39        match err {
40            store::Error::Upstream(err) => Self::Upstream(err),
41            store::Error::Io(err) => Self::Io(err),
42        }
43    }
44}
45
46impl<H: Handler> runtime::Host for Ctx<H> {
47    #[instrument(skip(self))]
48    async fn get(
49        &mut self,
50        key: String,
51    ) -> anyhow::Result<Result<Option<String>, config::runtime::ConfigError>> {
52        self.attach_parent_context();
53        let res = Config::get(&self.handler, &key).await?;
54        Ok(res.map_err(Into::into))
55    }
56
57    #[instrument(skip_all)]
58    async fn get_all(
59        &mut self,
60    ) -> anyhow::Result<Result<Vec<(String, String)>, config::runtime::ConfigError>> {
61        self.attach_parent_context();
62        let res = self.handler.get_all().await?;
63        Ok(res.map_err(Into::into))
64    }
65}