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
18#[async_trait]
19impl<H: Handler> store::Host for Ctx<H> {
20    #[instrument(skip(self))]
21    async fn get(
22        &mut self,
23        key: String,
24    ) -> anyhow::Result<Result<Option<String>, config::store::Error>> {
25        self.attach_parent_context();
26        Config::get(&self.handler, &key).await
27    }
28
29    #[instrument(skip_all)]
30    async fn get_all(
31        &mut self,
32    ) -> anyhow::Result<Result<Vec<(String, String)>, config::store::Error>> {
33        self.attach_parent_context();
34        self.handler.get_all().await
35    }
36}
37
38impl From<config::store::Error> for config::runtime::ConfigError {
39    fn from(err: config::store::Error) -> Self {
40        match err {
41            store::Error::Upstream(err) => Self::Upstream(err),
42            store::Error::Io(err) => Self::Io(err),
43        }
44    }
45}
46
47#[async_trait]
48impl<H: Handler> runtime::Host for Ctx<H> {
49    #[instrument(skip(self))]
50    async fn get(
51        &mut self,
52        key: String,
53    ) -> anyhow::Result<Result<Option<String>, config::runtime::ConfigError>> {
54        self.attach_parent_context();
55        let res = Config::get(&self.handler, &key).await?;
56        Ok(res.map_err(Into::into))
57    }
58
59    #[instrument(skip_all)]
60    async fn get_all(
61        &mut self,
62    ) -> anyhow::Result<Result<Vec<(String, String)>, config::runtime::ConfigError>> {
63        self.attach_parent_context();
64        let res = self.handler.get_all().await?;
65        Ok(res.map_err(Into::into))
66    }
67}