From 1a97874c09ad38f58980f629e46030901c93f9b3 Mon Sep 17 00:00:00 2001 From: Ryan Levick Date: Mon, 29 Jul 2024 14:50:13 +0200 Subject: [PATCH] Move host impls to their own module Signed-off-by: Ryan Levick --- crates/factor-variables/src/host.rs | 46 +++++++++++++++++++++ crates/factor-variables/src/lib.rs | 44 +------------------- crates/factor-variables/src/spin_cli/mod.rs | 28 ++++++------- 3 files changed, 61 insertions(+), 57 deletions(-) create mode 100644 crates/factor-variables/src/host.rs diff --git a/crates/factor-variables/src/host.rs b/crates/factor-variables/src/host.rs new file mode 100644 index 000000000..aa1d70f34 --- /dev/null +++ b/crates/factor-variables/src/host.rs @@ -0,0 +1,46 @@ +use spin_factors::anyhow; +use spin_world::{async_trait, v1, v2::variables}; + +use crate::InstanceState; + +#[async_trait] +impl variables::Host for InstanceState { + async fn get(&mut self, key: String) -> Result { + let key = spin_expressions::Key::new(&key).map_err(expressions_to_variables_err)?; + self.expression_resolver + .resolve(&self.component_id, key) + .await + .map_err(expressions_to_variables_err) + } + + fn convert_error(&mut self, error: variables::Error) -> anyhow::Result { + Ok(error) + } +} + +#[async_trait] +impl v1::config::Host for InstanceState { + async fn get_config(&mut self, key: String) -> Result { + ::get(self, key) + .await + .map_err(|err| match err { + variables::Error::InvalidName(msg) => v1::config::Error::InvalidKey(msg), + variables::Error::Undefined(msg) => v1::config::Error::Provider(msg), + other => v1::config::Error::Other(format!("{other}")), + }) + } + + fn convert_error(&mut self, err: v1::config::Error) -> anyhow::Result { + Ok(err) + } +} + +fn expressions_to_variables_err(err: spin_expressions::Error) -> variables::Error { + use spin_expressions::Error; + match err { + Error::InvalidName(msg) => variables::Error::InvalidName(msg), + Error::Undefined(msg) => variables::Error::Undefined(msg), + Error::Provider(err) => variables::Error::Provider(err.to_string()), + other => variables::Error::Other(format!("{other}")), + } +} diff --git a/crates/factor-variables/src/lib.rs b/crates/factor-variables/src/lib.rs index 554f89cbb..21e95b701 100644 --- a/crates/factor-variables/src/lib.rs +++ b/crates/factor-variables/src/lib.rs @@ -1,3 +1,4 @@ +mod host; pub mod runtime_config; pub mod spin_cli; @@ -9,7 +10,6 @@ use spin_factors::{ anyhow, ConfigureAppContext, Factor, InitContext, InstanceBuilders, PrepareContext, RuntimeFactors, SelfInstanceBuilder, }; -use spin_world::{async_trait, v1, v2::variables}; /// A factor for providing variables to components. pub struct VariablesFactor { @@ -87,45 +87,3 @@ impl InstanceState { } impl SelfInstanceBuilder for InstanceState {} - -#[async_trait] -impl variables::Host for InstanceState { - async fn get(&mut self, key: String) -> Result { - let key = spin_expressions::Key::new(&key).map_err(expressions_to_variables_err)?; - self.expression_resolver - .resolve(&self.component_id, key) - .await - .map_err(expressions_to_variables_err) - } - - fn convert_error(&mut self, error: variables::Error) -> anyhow::Result { - Ok(error) - } -} - -#[async_trait] -impl v1::config::Host for InstanceState { - async fn get_config(&mut self, key: String) -> Result { - ::get(self, key) - .await - .map_err(|err| match err { - variables::Error::InvalidName(msg) => v1::config::Error::InvalidKey(msg), - variables::Error::Undefined(msg) => v1::config::Error::Provider(msg), - other => v1::config::Error::Other(format!("{other}")), - }) - } - - fn convert_error(&mut self, err: v1::config::Error) -> anyhow::Result { - Ok(err) - } -} - -fn expressions_to_variables_err(err: spin_expressions::Error) -> variables::Error { - use spin_expressions::Error; - match err { - Error::InvalidName(msg) => variables::Error::InvalidName(msg), - Error::Undefined(msg) => variables::Error::Undefined(msg), - Error::Provider(err) => variables::Error::Provider(err.to_string()), - other => variables::Error::Other(format!("{other}")), - } -} diff --git a/crates/factor-variables/src/spin_cli/mod.rs b/crates/factor-variables/src/spin_cli/mod.rs index 13c1cbb23..bf21a944d 100644 --- a/crates/factor-variables/src/spin_cli/mod.rs +++ b/crates/factor-variables/src/spin_cli/mod.rs @@ -10,6 +10,20 @@ use statik::StaticVariablesProvider; use crate::runtime_config::RuntimeConfig; +/// Resolves a runtime configuration for the variables factor from a TOML table. +pub fn runtime_config_from_toml(table: &toml::Table) -> anyhow::Result> { + let Some(array) = table.get("variable_provider") else { + return Ok(None); + }; + + let provider_configs: Vec = array.clone().try_into()?; + let providers = provider_configs + .into_iter() + .map(VariableProviderConfiguration::into_provider) + .collect(); + Ok(Some(RuntimeConfig { providers })) +} + /// A runtime configuration used in the Spin CLI for one type of variable provider. #[derive(Debug, Deserialize)] #[serde(rename_all = "snake_case", tag = "type")] @@ -33,17 +47,3 @@ impl VariableProviderConfiguration { } } } - -/// Resolves a runtime configuration for the variables factor from a TOML table. -pub fn runtime_config_from_toml(table: &toml::Table) -> anyhow::Result> { - let Some(array) = table.get("variable_provider") else { - return Ok(None); - }; - - let provider_configs: Vec = array.clone().try_into()?; - let providers = provider_configs - .into_iter() - .map(VariableProviderConfiguration::into_provider) - .collect(); - Ok(Some(RuntimeConfig { providers })) -}