Skip to main content

spin_trigger/
lib.rs

1pub mod cli;
2pub mod loader;
3
4use heck::ToTitleCase;
5use std::future::Future;
6
7use clap::Args;
8use spin_core::Linker;
9use spin_factors::RuntimeFactors;
10use spin_factors_executor::{FactorsExecutorApp, FactorsInstanceBuilder};
11
12pub use spin_app::App;
13
14/// Type alias for a [`spin_factors_executor::FactorsExecutorApp`] specialized to a [`Trigger`].
15pub type TriggerApp<T, F> = FactorsExecutorApp<F, <T as Trigger<F>>::InstanceState>;
16
17/// Type alias for a [`spin_factors_executor::FactorsInstanceBuilder`] specialized to a [`Trigger`].
18pub type TriggerInstanceBuilder<'a, T, F> =
19    FactorsInstanceBuilder<'a, F, <T as Trigger<F>>::InstanceState>;
20
21/// Type alias for a [`spin_core::Store`] specialized to a [`Trigger`].
22pub type Store<T, F> = spin_core::Store<TriggerInstanceState<T, F>>;
23
24/// Type alias for [`spin_factors_executor::InstanceState`] specialized to a [`Trigger`].
25type TriggerInstanceState<T, F> = spin_factors_executor::InstanceState<
26    <F as RuntimeFactors>::InstanceState,
27    <T as Trigger<F>>::InstanceState,
28>;
29
30/// A trigger for a Spin runtime.
31pub trait Trigger<F: RuntimeFactors>: Sized + Send {
32    /// A unique identifier for this trigger.
33    const TYPE: &'static str;
34
35    /// The specific CLI arguments for this trigger.
36    type CliArgs: Args;
37
38    /// The instance state for this trigger.
39    type InstanceState: Send + 'static;
40
41    /// Constructs a new trigger.
42    fn new(cli_args: Self::CliArgs, app: &App) -> anyhow::Result<Self>;
43
44    /// Update the [`spin_core::Config`] for this trigger.
45    ///
46    /// !!!Warning!!! This is unsupported; many configurations are likely to
47    /// cause errors or unexpected behavior, especially in future versions.
48    #[doc(hidden)]
49    fn update_core_config(&mut self, config: &mut spin_core::Config) -> anyhow::Result<()> {
50        let _ = config;
51        Ok(())
52    }
53
54    /// Update the [`Linker`] for this trigger.
55    fn add_to_linker(
56        &mut self,
57        linker: &mut Linker<TriggerInstanceState<Self, F>>,
58    ) -> anyhow::Result<()> {
59        let _ = linker;
60        Ok(())
61    }
62
63    /// Run this trigger.
64    fn run(
65        self,
66        trigger_app: TriggerApp<Self, F>,
67    ) -> impl Future<Output = anyhow::Result<()>> + Send;
68
69    /// Returns a list of host requirements supported by this trigger specifically.
70    ///
71    /// See [`App::ensure_needs_only`].
72    fn supported_host_requirements() -> Vec<&'static str> {
73        Vec::new()
74    }
75
76    /// Returns the display name for the type of this trigger. Defaults to title case.
77    fn display_name() -> String {
78        Self::TYPE.to_title_case()
79    }
80}