spin_trigger/
lib.rs

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