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    /// An object which composes extras onto the primary component.
55    /// TODO: the combination of functions and objects and traits is a bit funny and we may/should be able to streamline it.
56    fn trigger_dependencies_composer() -> impl spin_factors_executor::TriggerDependenciesComposer {
57        // the do-nothing unit composer
58    }
59
60    /// Update the [`Linker`] for this trigger.
61    fn add_to_linker(
62        &mut self,
63        linker: &mut Linker<TriggerInstanceState<Self, F>>,
64    ) -> anyhow::Result<()> {
65        let _ = linker;
66        Ok(())
67    }
68
69    /// Run this trigger.
70    fn run(
71        self,
72        trigger_app: TriggerApp<Self, F>,
73    ) -> impl Future<Output = anyhow::Result<()>> + Send;
74
75    /// Returns a list of host requirements supported by this trigger specifically.
76    ///
77    /// See [`App::ensure_needs_only`].
78    fn supported_host_requirements() -> Vec<&'static str> {
79        Vec::new()
80    }
81
82    /// Returns the display name for the type of this trigger. Defaults to title case.
83    fn display_name() -> String {
84        Self::TYPE.to_title_case()
85    }
86}