spin_factors/
lib.rs

1mod factor;
2mod prepare;
3pub mod runtime_config;
4mod runtime_factors;
5
6pub use anyhow;
7pub use serde;
8pub use wasmtime;
9
10pub use spin_app::{App, AppComponent};
11pub use spin_factors_derive::RuntimeFactors;
12
13pub use crate::{
14    factor::{
15        ConfigureAppContext, ConfiguredApp, Factor, FactorField, FactorInitContext,
16        FactorInstanceState, InitContext,
17    },
18    prepare::{FactorInstanceBuilder, PrepareContext, SelfInstanceBuilder},
19    runtime_config::{FactorRuntimeConfigSource, RuntimeConfigSourceFinalizer},
20    runtime_factors::{
21        AsInstanceState, HasInstanceBuilder, RuntimeFactors, RuntimeFactorsInstanceState,
22    },
23};
24
25/// Result wrapper type defaulting to use [`Error`].
26pub type Result<T, E = Error> = std::result::Result<T, E>;
27
28#[derive(Debug, thiserror::Error)]
29pub enum Error {
30    #[error("two or more factors share the same type: {0}")]
31    DuplicateFactorTypes(String),
32    #[error("factor dependency ordering error: {0}")]
33    DependencyOrderingError(String),
34    #[error("{factor}::InstanceBuilder::build failed: {source}")]
35    FactorBuildError {
36        factor: &'static str,
37        source: anyhow::Error,
38    },
39    #[error("{factor}::configure_app failed: {source}")]
40    FactorConfigureAppError {
41        factor: &'static str,
42        source: anyhow::Error,
43    },
44    #[error("{factor}::init failed: {source}")]
45    FactorInitError {
46        factor: &'static str,
47        source: anyhow::Error,
48    },
49    #[error("{factor}::prepare failed: {source}")]
50    FactorPrepareError {
51        factor: &'static str,
52        source: anyhow::Error,
53    },
54    #[error("no such factor: {0}")]
55    NoSuchFactor(&'static str),
56    #[error("{factor} requested already-consumed key {key:?}")]
57    RuntimeConfigReusedKey { factor: &'static str, key: String },
58    #[error("runtime config error: {0}")]
59    RuntimeConfigSource(#[source] anyhow::Error),
60    #[error("unused runtime config key(s): {}", keys.join(", "))]
61    RuntimeConfigUnusedKeys { keys: Vec<String> },
62    #[error("unknown component: {0}")]
63    UnknownComponent(String),
64}
65
66impl Error {
67    fn no_such_factor<T: Factor>() -> Self {
68        Self::NoSuchFactor(std::any::type_name::<T>())
69    }
70
71    pub fn runtime_config_reused_key<T: Factor>(key: impl Into<String>) -> Self {
72        Self::RuntimeConfigReusedKey {
73            factor: std::any::type_name::<T>(),
74            key: key.into(),
75        }
76    }
77
78    // These helpers are used by factors-derive
79
80    #[doc(hidden)]
81    pub fn factor_init_error<T: Factor>(source: anyhow::Error) -> Self {
82        let factor = std::any::type_name::<T>();
83        Self::FactorInitError { factor, source }
84    }
85
86    #[doc(hidden)]
87    pub fn factor_configure_app_error<T: Factor>(source: anyhow::Error) -> Self {
88        let factor = std::any::type_name::<T>();
89        Self::FactorConfigureAppError { factor, source }
90    }
91
92    #[doc(hidden)]
93    pub fn factor_prepare_error<T: Factor>(source: anyhow::Error) -> Self {
94        let factor = std::any::type_name::<T>();
95        Self::FactorPrepareError { factor, source }
96    }
97
98    #[doc(hidden)]
99    pub fn factor_build_error<T: Factor>(source: anyhow::Error) -> Self {
100        let factor = std::any::type_name::<T>();
101        Self::FactorBuildError { factor, source }
102    }
103}