spin_factors/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
mod factor;
mod prepare;
pub mod runtime_config;
mod runtime_factors;

pub use anyhow;
pub use serde;
pub use wasmtime;

pub use spin_app::{App, AppComponent};
pub use spin_factors_derive::RuntimeFactors;

pub use crate::{
    factor::{ConfigureAppContext, ConfiguredApp, Factor, FactorInstanceState, InitContext},
    prepare::{FactorInstanceBuilder, PrepareContext, SelfInstanceBuilder},
    runtime_config::{FactorRuntimeConfigSource, RuntimeConfigSourceFinalizer},
    runtime_factors::{
        AsInstanceState, HasInstanceBuilder, RuntimeFactors, RuntimeFactorsInstanceState,
    },
};

/// Result wrapper type defaulting to use [`Error`].
pub type Result<T, E = Error> = std::result::Result<T, E>;

#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error("two or more factors share the same type: {0}")]
    DuplicateFactorTypes(String),
    #[error("factor dependency ordering error: {0}")]
    DependencyOrderingError(String),
    #[error("{factor}::InstanceBuilder::build failed: {source}")]
    FactorBuildError {
        factor: &'static str,
        source: anyhow::Error,
    },
    #[error("{factor}::configure_app failed: {source}")]
    FactorConfigureAppError {
        factor: &'static str,
        source: anyhow::Error,
    },
    #[error("{factor}::init failed: {source}")]
    FactorInitError {
        factor: &'static str,
        source: anyhow::Error,
    },
    #[error("{factor}::prepare failed: {source}")]
    FactorPrepareError {
        factor: &'static str,
        source: anyhow::Error,
    },
    #[error("no such factor: {0}")]
    NoSuchFactor(&'static str),
    #[error("{factor} requested already-consumed key {key:?}")]
    RuntimeConfigReusedKey { factor: &'static str, key: String },
    #[error("runtime config error: {0}")]
    RuntimeConfigSource(#[source] anyhow::Error),
    #[error("unused runtime config key(s): {}", keys.join(", "))]
    RuntimeConfigUnusedKeys { keys: Vec<String> },
    #[error("unknown component: {0}")]
    UnknownComponent(String),
}

impl Error {
    fn no_such_factor<T: Factor>() -> Self {
        Self::NoSuchFactor(std::any::type_name::<T>())
    }

    pub fn runtime_config_reused_key<T: Factor>(key: impl Into<String>) -> Self {
        Self::RuntimeConfigReusedKey {
            factor: std::any::type_name::<T>(),
            key: key.into(),
        }
    }

    // These helpers are used by factors-derive

    #[doc(hidden)]
    pub fn factor_init_error<T: Factor>(source: anyhow::Error) -> Self {
        let factor = std::any::type_name::<T>();
        Self::FactorInitError { factor, source }
    }

    #[doc(hidden)]
    pub fn factor_configure_app_error<T: Factor>(source: anyhow::Error) -> Self {
        let factor = std::any::type_name::<T>();
        Self::FactorConfigureAppError { factor, source }
    }

    #[doc(hidden)]
    pub fn factor_prepare_error<T: Factor>(source: anyhow::Error) -> Self {
        let factor = std::any::type_name::<T>();
        Self::FactorPrepareError { factor, source }
    }

    #[doc(hidden)]
    pub fn factor_build_error<T: Factor>(source: anyhow::Error) -> Self {
        let factor = std::any::type_name::<T>();
        Self::FactorBuildError { factor, source }
    }
}