Skip to content

Commit f5910a7

Browse files
committed
factors: Add RuntimeFactors::prepare
This breaks RuntimeFactors::build_instance_state into two steps, mirroring Factor's methods. Some triggers will need access to these builders, e.g. WAGI needs access to WasiFactor's stdin configuration. Signed-off-by: Lann Martin <lann.martin@fermyon.com>
1 parent d74bd80 commit f5910a7

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
lines changed

crates/factors-derive/src/lib.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,10 @@ fn expand_factors(input: &DeriveInput) -> syn::Result<TokenStream> {
118118
Ok(#ConfiguredApp::new(app, app_state))
119119
}
120120

121-
fn build_instance_state(
121+
fn prepare(
122122
&self, configured_app: &#ConfiguredApp<Self>,
123123
component_id: &str,
124-
) -> #Result<Self::InstanceState> {
124+
) -> #Result<Self::InstanceBuilders> {
125125
let app_component = configured_app.app().get_component(component_id).ok_or_else(|| {
126126
#factors_path::Error::UnknownComponent(component_id.to_string())
127127
})?;
@@ -140,6 +140,13 @@ fn expand_factors(input: &DeriveInput) -> syn::Result<TokenStream> {
140140
).map_err(#Error::factor_prepare_error::<#factor_types>)?
141141
);
142142
)*
143+
Ok(builders)
144+
}
145+
146+
fn build_instance_state(
147+
&self,
148+
builders: Self::InstanceBuilders,
149+
) -> #Result<Self::InstanceState> {
143150
Ok(#state_name {
144151
#(
145152
#factor_names: #FactorInstanceBuilder::build(

crates/factors-test/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ impl TestEnvironment {
6969
configured_app.app().components().next().context(
7070
"expected configured app to have at least one component, but it did not",
7171
)?;
72-
Ok(factors.build_instance_state(&configured_app, component.id())?)
72+
let builders = factors.prepare(&configured_app, component.id())?;
73+
74+
Ok(factors.build_instance_state(builders)?)
7375
}
7476

7577
pub fn new_linker<T: RuntimeFactors>() -> Linker<T> {

crates/factors/src/runtime_factors.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,17 @@ pub trait RuntimeFactors: Sized + 'static {
4646
runtime_config: impl RuntimeConfigSource,
4747
) -> crate::Result<ConfiguredApp<Self>>;
4848

49-
/// Build the instance state for the factors.
50-
fn build_instance_state(
49+
/// Prepare the factors' instance state builders.
50+
fn prepare(
5151
&self,
5252
configured_app: &ConfiguredApp<Self>,
5353
component_id: &str,
54+
) -> crate::Result<Self::InstanceBuilders>;
55+
56+
/// Build the instance state for the factors.
57+
fn build_instance_state(
58+
&self,
59+
builders: Self::InstanceBuilders,
5460
) -> crate::Result<Self::InstanceState>;
5561

5662
/// Get the app state related to a particular factor.

crates/factors/tests/smoke.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ use http_body_util::BodyExt;
55
use serde::Deserialize;
66
use spin_app::App;
77
use spin_factor_key_value::{KeyValueFactor, MakeKeyValueStore};
8+
use spin_factor_key_value_redis::RedisKeyValueStore;
89
use spin_factor_outbound_http::OutboundHttpFactor;
910
use spin_factor_outbound_networking::OutboundNetworkingFactor;
1011
use spin_factor_variables::{StaticVariables, VariablesFactor};
1112
use spin_factor_wasi::{DummyFilesMounter, WasiFactor};
1213
use spin_factors::{FactorRuntimeConfig, RuntimeConfigSource, RuntimeFactors};
1314
use spin_key_value_sqlite::{DatabaseLocation, KeyValueSqlite};
14-
use spin_factor_key_value_redis::RedisKeyValueStore;
1515
use wasmtime_wasi_http::WasiHttpView;
1616

1717
#[derive(RuntimeFactors)]
@@ -53,7 +53,8 @@ async fn smoke_test_works() -> anyhow::Result<()> {
5353
factors.init(&mut linker).unwrap();
5454

5555
let configured_app = factors.configure_app(app, TestSource)?;
56-
let data = factors.build_instance_state(&configured_app, "smoke-app")?;
56+
let builders = factors.prepare(&configured_app, "smoke-app")?;
57+
let data = factors.build_instance_state(builders)?;
5758

5859
assert_eq!(
5960
data.variables

0 commit comments

Comments
 (0)