Skip to content

Commit 0bf64cc

Browse files
committed
factors: Reorganize spin-factors
Signed-off-by: Lann Martin <lann.martin@fermyon.com>
1 parent f610ec6 commit 0bf64cc

File tree

16 files changed

+531
-378
lines changed

16 files changed

+531
-378
lines changed

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/factor-outbound-networking/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ authors = { workspace = true }
55
edition = { workspace = true }
66

77
[dependencies]
8-
anyhow = "1"
98
futures-util = "0.3"
109
ipnet = "2.9.0"
1110
spin-factor-variables = { path = "../factor-variables" }

crates/factor-outbound-networking/src/lib.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
use std::{collections::HashMap, sync::Arc};
22

3-
use anyhow::Context;
43
use futures_util::{
54
future::{BoxFuture, Shared},
65
FutureExt,
76
};
87
use spin_factor_variables::VariablesFactor;
98
use spin_factor_wasi::WasiFactor;
109
use spin_factors::{
11-
Factor, FactorInstancePreparer, InstancePreparers, PrepareContext, Result, SpinFactors,
10+
anyhow::{self, Context},
11+
ConfigureAppContext, Factor, FactorInstancePreparer, InstancePreparers, PrepareContext,
12+
RuntimeConfig, SpinFactors,
1213
};
1314
use spin_outbound_networking::{AllowedHostsConfig, ALLOWED_HOSTS_KEY};
1415

@@ -21,11 +22,12 @@ impl Factor for OutboundNetworkingFactor {
2122

2223
fn configure_app<Factors: SpinFactors>(
2324
&self,
24-
app: &spin_factors::App,
25-
_ctx: spin_factors::ConfigureAppContext<Factors>,
26-
) -> Result<Self::AppConfig> {
25+
ctx: ConfigureAppContext<Factors>,
26+
_runtime_config: &mut impl RuntimeConfig,
27+
) -> anyhow::Result<Self::AppConfig> {
2728
// Extract allowed_outbound_hosts for all components
28-
let component_allowed_hosts = app
29+
let component_allowed_hosts = ctx
30+
.app()
2931
.components()
3032
.map(|component| {
3133
Ok((
@@ -37,7 +39,7 @@ impl Factor for OutboundNetworkingFactor {
3739
.into(),
3840
))
3941
})
40-
.collect::<Result<_>>()?;
42+
.collect::<anyhow::Result<_>>()?;
4143
Ok(AppConfig {
4244
component_allowed_hosts,
4345
})
@@ -49,17 +51,17 @@ pub struct AppConfig {
4951
component_allowed_hosts: HashMap<String, Arc<[String]>>,
5052
}
5153

52-
type AllowedHostsFuture = Shared<BoxFuture<'static, Arc<anyhow::Result<AllowedHostsConfig>>>>;
54+
type SharedFutureResult<T> = Shared<BoxFuture<'static, Arc<anyhow::Result<T>>>>;
5355

5456
pub struct InstancePreparer {
55-
allowed_hosts_future: AllowedHostsFuture,
57+
allowed_hosts_future: SharedFutureResult<AllowedHostsConfig>,
5658
}
5759

5860
impl FactorInstancePreparer<OutboundNetworkingFactor> for InstancePreparer {
5961
fn new<Factors: SpinFactors>(
6062
ctx: PrepareContext<OutboundNetworkingFactor>,
6163
mut preparers: InstancePreparers<Factors>,
62-
) -> Result<Self> {
64+
) -> anyhow::Result<Self> {
6365
let hosts = ctx
6466
.app_config()
6567
.component_allowed_hosts
@@ -104,7 +106,7 @@ impl FactorInstancePreparer<OutboundNetworkingFactor> for InstancePreparer {
104106
})
105107
}
106108

107-
fn prepare(self) -> Result<<OutboundNetworkingFactor as Factor>::InstanceState> {
109+
fn prepare(self) -> anyhow::Result<<OutboundNetworkingFactor as Factor>::InstanceState> {
108110
Ok(())
109111
}
110112
}

crates/factor-variables/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ authors = { workspace = true }
55
edition = { workspace = true }
66

77
[dependencies]
8-
anyhow = "1"
98
spin-expressions = { path = "../expressions" }
109
spin-factors = { path = "../factors" }
1110
spin-world = { path = "../world" }

crates/factor-variables/src/lib.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use std::sync::Arc;
22

33
use spin_expressions::ProviderResolver;
44
use spin_factors::{
5-
Factor, FactorInstancePreparer, InstancePreparers, PrepareContext, Result, SpinFactors,
5+
anyhow, ConfigureAppContext, Factor, FactorInstancePreparer, InitContext, InstancePreparers,
6+
PrepareContext, RuntimeConfig, SpinFactors,
67
};
78
use spin_world::{async_trait, v1::config as v1_config, v2::variables};
89

@@ -15,18 +16,19 @@ impl Factor for VariablesFactor {
1516

1617
fn init<Factors: SpinFactors>(
1718
&mut self,
18-
mut ctx: spin_factors::InitContext<Factors, Self>,
19-
) -> Result<()> {
19+
mut ctx: InitContext<Factors, Self>,
20+
) -> anyhow::Result<()> {
2021
ctx.link_bindings(v1_config::add_to_linker)?;
2122
ctx.link_bindings(variables::add_to_linker)?;
2223
Ok(())
2324
}
2425

2526
fn configure_app<Factors: SpinFactors>(
2627
&self,
27-
app: &spin_factors::App,
28-
_ctx: spin_factors::ConfigureAppContext<Factors>,
29-
) -> Result<Self::AppConfig> {
28+
ctx: ConfigureAppContext<Factors>,
29+
_runtime_config: &mut impl RuntimeConfig,
30+
) -> anyhow::Result<Self::AppConfig> {
31+
let app = ctx.app();
3032
let mut resolver =
3133
ProviderResolver::new(app.variables().map(|(key, val)| (key.clone(), val.clone())))?;
3234
for component in app.components() {
@@ -61,7 +63,7 @@ impl FactorInstancePreparer<VariablesFactor> for InstancePreparer {
6163
fn new<Factors: SpinFactors>(
6264
ctx: PrepareContext<VariablesFactor>,
6365
_preparers: InstancePreparers<Factors>,
64-
) -> Result<Self> {
66+
) -> anyhow::Result<Self> {
6567
let component_id = ctx.app_component().id().to_string();
6668
let resolver = ctx.app_config().resolver.clone();
6769
Ok(Self {
@@ -72,7 +74,7 @@ impl FactorInstancePreparer<VariablesFactor> for InstancePreparer {
7274
})
7375
}
7476

75-
fn prepare(self) -> Result<<VariablesFactor as Factor>::InstanceState> {
77+
fn prepare(self) -> anyhow::Result<InstanceState> {
7678
Ok(self.state)
7779
}
7880
}
@@ -92,7 +94,7 @@ impl variables::Host for InstanceState {
9294
.map_err(expressions_to_variables_err)
9395
}
9496

95-
fn convert_error(&mut self, error: variables::Error) -> Result<variables::Error> {
97+
fn convert_error(&mut self, error: variables::Error) -> anyhow::Result<variables::Error> {
9698
Ok(error)
9799
}
98100
}

crates/factor-wasi/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ authors = { workspace = true }
55
edition = { workspace = true }
66

77
[dependencies]
8-
anyhow = "1"
98
cap-primitives = "3.0.0"
109
spin-factors = { path = "../factors" }
1110
wasmtime-wasi = { workspace = true }

crates/factor-wasi/src/lib.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ pub mod preview1;
22

33
use std::{future::Future, net::SocketAddr, path::Path};
44

5-
use anyhow::ensure;
65
use spin_factors::{
7-
AppComponent, Factor, FactorInstancePreparer, InitContext, InstancePreparers, PrepareContext,
8-
Result, SpinFactors,
6+
anyhow, AppComponent, Factor, FactorInstancePreparer, InitContext, InstancePreparers,
7+
PrepareContext, SpinFactors,
98
};
109
use wasmtime_wasi::{ResourceTable, WasiCtx, WasiCtxBuilder, WasiView};
1110

@@ -26,7 +25,10 @@ impl Factor for WasiFactor {
2625
type InstancePreparer = InstancePreparer;
2726
type InstanceState = InstanceState;
2827

29-
fn init<Factors: SpinFactors>(&mut self, mut ctx: InitContext<Factors, Self>) -> Result<()> {
28+
fn init<Factors: SpinFactors>(
29+
&mut self,
30+
mut ctx: InitContext<Factors, Self>,
31+
) -> anyhow::Result<()> {
3032
fn type_annotate<T, F>(f: F) -> F
3133
where
3234
F: Fn(&mut T) -> &mut dyn WasiView,
@@ -70,14 +72,22 @@ impl Factor for WasiFactor {
7072
}
7173

7274
pub trait FilesMounter {
73-
fn mount_files(&self, app_component: &AppComponent, ctx: MountFilesContext) -> Result<()>;
75+
fn mount_files(
76+
&self,
77+
app_component: &AppComponent,
78+
ctx: MountFilesContext,
79+
) -> anyhow::Result<()>;
7480
}
7581

7682
pub struct DummyFilesMounter;
7783

7884
impl FilesMounter for DummyFilesMounter {
79-
fn mount_files(&self, app_component: &AppComponent, _ctx: MountFilesContext) -> Result<()> {
80-
ensure!(
85+
fn mount_files(
86+
&self,
87+
app_component: &AppComponent,
88+
_ctx: MountFilesContext,
89+
) -> anyhow::Result<()> {
90+
anyhow::ensure!(
8191
app_component.files().next().is_none(),
8292
"DummyFilesMounter can't actually mount files"
8393
);
@@ -95,7 +105,7 @@ impl<'a> MountFilesContext<'a> {
95105
host_path: impl AsRef<Path>,
96106
guest_path: impl AsRef<str>,
97107
writable: bool,
98-
) -> Result<()> {
108+
) -> anyhow::Result<()> {
99109
use wasmtime_wasi::{DirPerms, FilePerms};
100110
let (dir_perms, file_perms) = if writable {
101111
(DirPerms::all(), FilePerms::all())
@@ -117,7 +127,7 @@ impl FactorInstancePreparer<WasiFactor> for InstancePreparer {
117127
fn new<Factors: SpinFactors>(
118128
ctx: PrepareContext<WasiFactor>,
119129
_preparers: InstancePreparers<Factors>,
120-
) -> Result<Self> {
130+
) -> anyhow::Result<Self> {
121131
let mut wasi_ctx = WasiCtxBuilder::new();
122132

123133
// Apply environment variables
@@ -136,7 +146,7 @@ impl FactorInstancePreparer<WasiFactor> for InstancePreparer {
136146
Ok(Self { wasi_ctx })
137147
}
138148

139-
fn prepare(self) -> Result<InstanceState> {
149+
fn prepare(self) -> anyhow::Result<InstanceState> {
140150
let Self { mut wasi_ctx } = self;
141151
Ok(InstanceState {
142152
ctx: wasi_ctx.build(),

crates/factor-wasi/src/preview1.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use spin_factors::{
2-
Factor, FactorInstancePreparer, InitContext, InstancePreparers, PrepareContext, Result, SpinFactors
2+
anyhow, Factor, FactorInstancePreparer, InitContext, InstancePreparers, PrepareContext,
3+
SpinFactors,
34
};
45
use wasmtime_wasi::{preview1::WasiP1Ctx, WasiCtxBuilder};
56

@@ -10,7 +11,10 @@ impl Factor for WasiPreview1Factor {
1011
type InstancePreparer = InstancePreparer;
1112
type InstanceState = WasiP1Ctx;
1213

13-
fn init<Factors: SpinFactors>(&mut self, mut ctx: InitContext<Factors, Self>) -> Result<()> {
14+
fn init<Factors: SpinFactors>(
15+
&mut self,
16+
mut ctx: InitContext<Factors, Self>,
17+
) -> anyhow::Result<()> {
1418
ctx.link_module_bindings(wasmtime_wasi::preview1::add_to_linker_async)
1519
}
1620
}
@@ -23,13 +27,13 @@ impl FactorInstancePreparer<WasiPreview1Factor> for InstancePreparer {
2327
fn new<Factors: SpinFactors>(
2428
_ctx: PrepareContext<WasiPreview1Factor>,
2529
_preparers: InstancePreparers<Factors>,
26-
) -> Result<Self> {
30+
) -> anyhow::Result<Self> {
2731
Ok(Self {
2832
wasi_ctx: WasiCtxBuilder::new(),
2933
})
3034
}
3135

32-
fn prepare(mut self) -> Result<<WasiPreview1Factor as Factor>::InstanceState> {
36+
fn prepare(mut self) -> anyhow::Result<WasiP1Ctx> {
3337
Ok(self.wasi_ctx.build_p1())
3438
}
3539
}

crates/factors-derive/src/lib.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,14 @@ fn expand_factors(input: &DeriveInput) -> syn::Result<TokenStream> {
5353
factor_types.push(&field.ty);
5454
}
5555

56+
let TypeId = quote!(::std::any::TypeId);
5657
let factors_crate = format_ident!("spin_factors");
5758
let factors_path = quote!(::#factors_crate);
58-
let Factor = quote!(#factors_path::Factor);
59-
let Result = quote!(#factors_path::Result);
6059
let wasmtime = quote!(#factors_path::wasmtime);
60+
let Result = quote!(#factors_path::Result);
61+
let Factor = quote!(#factors_path::Factor);
6162
let ConfiguredApp = quote!(#factors_path::ConfiguredApp);
62-
let TypeId = quote!(::std::any::TypeId);
63+
let RuntimeConfigTracker = quote!(#factors_path::__internal::RuntimeConfigTracker);
6364

6465
Ok(quote! {
6566
impl #name {
@@ -81,16 +82,21 @@ fn expand_factors(input: &DeriveInput) -> syn::Result<TokenStream> {
8182
Ok(())
8283
}
8384

84-
pub fn configure_app(&self, app: #factors_path::App) -> #Result<#ConfiguredApp<Self>> {
85+
pub fn configure_app(
86+
&self,
87+
app: #factors_path::App,
88+
runtime_config: impl #factors_path::RuntimeConfigSource
89+
) -> #Result<#ConfiguredApp<Self>> {
8590
let mut app_configs = #app_configs_name {
8691
#( #factor_names: None, )*
8792
};
93+
let mut runtime_config = #RuntimeConfigTracker::new(runtime_config);
8894
#(
8995
app_configs.#factor_names = Some(
9096
#Factor::configure_app(
9197
&self.#factor_names,
92-
&app,
93-
#factors_path::ConfigureAppContext::<Self>::new(&app_configs),
98+
#factors_path::ConfigureAppContext::<Self>::new(&app, &app_configs),
99+
&mut runtime_config,
94100
)?
95101
);
96102
)*
@@ -99,7 +105,7 @@ fn expand_factors(input: &DeriveInput) -> syn::Result<TokenStream> {
99105

100106
pub fn build_store_data(&self, configured_app: &#ConfiguredApp<Self>, component_id: &str) -> #Result<#state_name> {
101107
let app_component = configured_app.app().get_component(component_id).ok_or_else(|| {
102-
#factors_path::Error::msg(format!("unknown component {component_id:?}"))
108+
#wasmtime::Error::msg("unknown component")
103109
})?;
104110
let mut preparers = #preparers_name {
105111
#( #factor_names: None, )*

crates/factors/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ edition = { workspace = true }
66

77
[dependencies]
88
anyhow = "1.0"
9+
serde = "1.0"
910
spin-app = { path = "../app" }
1011
spin-factors-derive = { path = "../factors-derive" }
12+
thiserror = "1.0"
13+
tracing = { workspace = true }
1114
wasmtime = { workspace = true }
1215

1316
[dev-dependencies]

0 commit comments

Comments
 (0)