|
1 | 1 | use spin_factors::anyhow; |
2 | | -use spin_world::{async_trait, v1, v2::variables}; |
| 2 | +use spin_world::{async_trait, v1, v2::variables, wasi::config as wasi_config}; |
3 | 3 | use tracing::{instrument, Level}; |
4 | 4 |
|
5 | 5 | use crate::InstanceState; |
@@ -37,6 +37,47 @@ impl v1::config::Host for InstanceState { |
37 | 37 | } |
38 | 38 | } |
39 | 39 |
|
| 40 | +#[async_trait] |
| 41 | +impl wasi_config::store::Host for InstanceState { |
| 42 | + async fn get(&mut self, key: String) -> Result<Option<String>, wasi_config::store::Error> { |
| 43 | + match <Self as variables::Host>::get(self, key).await { |
| 44 | + Ok(value) => Ok(Some(value)), |
| 45 | + Err(e) => { |
| 46 | + match e { |
| 47 | + variables::Error::Undefined(_) => Ok(None), |
| 48 | + variables::Error::InvalidName(_) => Ok(None), // this is the guidance from https://github.com/WebAssembly/wasi-runtime-config/pull/19 |
| 49 | + variables::Error::Provider(msg) => { |
| 50 | + Err(wasi_config::store::Error::Upstream(msg)) |
| 51 | + } |
| 52 | + variables::Error::Other(msg) => Err(wasi_config::store::Error::Io(msg)), |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + async fn get_all(&mut self) -> Result<Vec<(String, String)>, wasi_config::store::Error> { |
| 59 | + let all = self |
| 60 | + .expression_resolver |
| 61 | + .resolve_all(&self.component_id) |
| 62 | + .await; |
| 63 | + all.map_err(|e| { |
| 64 | + match expressions_to_variables_err(e) { |
| 65 | + variables::Error::Undefined(msg) => wasi_config::store::Error::Io(msg), // this shouldn't happen but just in case |
| 66 | + variables::Error::InvalidName(msg) => wasi_config::store::Error::Io(msg), // this shouldn't happen but just in case |
| 67 | + variables::Error::Provider(msg) => wasi_config::store::Error::Upstream(msg), |
| 68 | + variables::Error::Other(msg) => wasi_config::store::Error::Io(msg), |
| 69 | + } |
| 70 | + }) |
| 71 | + } |
| 72 | + |
| 73 | + fn convert_error( |
| 74 | + &mut self, |
| 75 | + err: wasi_config::store::Error, |
| 76 | + ) -> anyhow::Result<wasi_config::store::Error> { |
| 77 | + Ok(err) |
| 78 | + } |
| 79 | +} |
| 80 | + |
40 | 81 | fn expressions_to_variables_err(err: spin_expressions::Error) -> variables::Error { |
41 | 82 | use spin_expressions::Error; |
42 | 83 | match err { |
|
0 commit comments