Skip to content

Commit

Permalink
Move host impls to their own module
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Levick <[email protected]>
  • Loading branch information
rylev committed Jul 29, 2024
1 parent bf2e257 commit 1a97874
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 57 deletions.
46 changes: 46 additions & 0 deletions crates/factor-variables/src/host.rs
Original file line number Diff line number Diff line change
@@ -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<String, variables::Error> {
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<variables::Error> {
Ok(error)
}
}

#[async_trait]
impl v1::config::Host for InstanceState {
async fn get_config(&mut self, key: String) -> Result<String, v1::config::Error> {
<Self as variables::Host>::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<v1::config::Error> {
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}")),
}
}
44 changes: 1 addition & 43 deletions crates/factor-variables/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod host;
pub mod runtime_config;
pub mod spin_cli;

Expand All @@ -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 {
Expand Down Expand Up @@ -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<String, variables::Error> {
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<variables::Error> {
Ok(error)
}
}

#[async_trait]
impl v1::config::Host for InstanceState {
async fn get_config(&mut self, key: String) -> Result<String, v1::config::Error> {
<Self as variables::Host>::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<v1::config::Error> {
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}")),
}
}
28 changes: 14 additions & 14 deletions crates/factor-variables/src/spin_cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<RuntimeConfig>> {
let Some(array) = table.get("variable_provider") else {
return Ok(None);
};

let provider_configs: Vec<VariableProviderConfiguration> = 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")]
Expand All @@ -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<Option<RuntimeConfig>> {
let Some(array) = table.get("variable_provider") else {
return Ok(None);
};

let provider_configs: Vec<VariableProviderConfiguration> = array.clone().try_into()?;
let providers = provider_configs
.into_iter()
.map(VariableProviderConfiguration::into_provider)
.collect();
Ok(Some(RuntimeConfig { providers }))
}

0 comments on commit 1a97874

Please sign in to comment.