1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use super::{Ctx, Handler};

use crate::capability::config::{self, runtime, store};

use async_trait::async_trait;
use tracing::instrument;

/// `wasi:config/store` implementation
#[async_trait]
pub trait Config {
    /// Handle `wasi:config/store.get`
    async fn get(&self, key: &str) -> anyhow::Result<Result<Option<String>, config::store::Error>>;

    /// Handle `wasi:config/store.get_all`
    async fn get_all(&self) -> anyhow::Result<Result<Vec<(String, String)>, config::store::Error>>;
}

#[async_trait]
impl<H: Handler> store::Host for Ctx<H> {
    #[instrument(skip(self))]
    async fn get(
        &mut self,
        key: String,
    ) -> anyhow::Result<Result<Option<String>, config::store::Error>> {
        Config::get(&self.handler, &key).await
    }

    #[instrument(skip_all)]
    async fn get_all(
        &mut self,
    ) -> anyhow::Result<Result<Vec<(String, String)>, config::store::Error>> {
        self.handler.get_all().await
    }
}

impl From<config::store::Error> for config::runtime::ConfigError {
    fn from(err: config::store::Error) -> Self {
        match err {
            store::Error::Upstream(err) => Self::Upstream(err),
            store::Error::Io(err) => Self::Io(err),
        }
    }
}

#[async_trait]
impl<H: Handler> runtime::Host for Ctx<H> {
    #[instrument(skip(self))]
    async fn get(
        &mut self,
        key: String,
    ) -> anyhow::Result<Result<Option<String>, config::runtime::ConfigError>> {
        let res = Config::get(&self.handler, &key).await?;
        Ok(res.map_err(Into::into))
    }

    #[instrument(skip_all)]
    async fn get_all(
        &mut self,
    ) -> anyhow::Result<Result<Vec<(String, String)>, config::runtime::ConfigError>> {
        let res = self.handler.get_all().await?;
        Ok(res.map_err(Into::into))
    }
}