Skip to content

Commit 3563a1b

Browse files
committed
Be more flexible with trigger type -> world mapping
Signed-off-by: itowlson <ivan.towlson@fermyon.com>
1 parent 86f61b8 commit 3563a1b

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

crates/environments/src/environment_definition.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,16 @@ fn load_environment_from_dir(path: &Path) -> anyhow::Result<TargetEnvironment> {
177177
/// for example when it supports multiple versions of the Spin or WASI interfaces.)
178178
///
179179
/// In terms of implementation, internally the environment is represented by a
180-
/// WIT package that adheres to a specific naming convention (that the worlds for
181-
/// a given trigger type are exactly whose names begin `trigger-xxx` where
182-
/// `xxx` is the Spin trigger type).
180+
/// WIT package that adheres to a specific naming convention - namely that the worlds for
181+
/// a given trigger type are exactly whose names begin with one of:
182+
///
183+
/// * `trigger-xxx`
184+
/// * `xxx-trigger`
185+
/// * `spin-xxx` (a convention used by some plugins)
186+
///
187+
/// where `xxx` is the Spin trigger type. This flexibility is intended to maximise
188+
/// reuse of existing WIT files rather than needing to create custom ones to
189+
/// define environments.
183190
pub struct TargetEnvironment {
184191
name: String,
185192
decoded: wit_parser::decoding::DecodedWasm,
@@ -243,10 +250,16 @@ impl TargetEnvironment {
243250
/// Returns true if the given trigger type provides the world identified by
244251
/// `world` in this environment.
245252
pub fn is_world_for(&self, trigger_type: &TriggerType, world: &wit_parser::World) -> bool {
246-
world.name.starts_with(&format!("trigger-{trigger_type}"))
253+
self.matches_world_name(trigger_type, world)
247254
&& world.package.is_some_and(|p| p == self.package_id)
248255
}
249256

257+
fn matches_world_name(&self, trigger_type: &TriggerType, world: &wit_parser::World) -> bool {
258+
world.name.starts_with(&format!("trigger-{trigger_type}"))
259+
|| world.name.starts_with(&format!("{trigger_type}-trigger"))
260+
|| world.name.starts_with(&format!("spin-{trigger_type}"))
261+
}
262+
250263
/// Returns true if the given trigger type can run in this environment.
251264
pub fn supports_trigger_type(&self, trigger_type: &TriggerType) -> bool {
252265
self.decoded

0 commit comments

Comments
 (0)