Skip to main content

lash_core/plugin/
services.rs

1use std::sync::Arc;
2
3use super::*;
4
5#[derive(Debug, thiserror::Error)]
6pub enum PluginActionInvokeError {
7    #[error("unknown plugin action `{0}`")]
8    Unknown(String),
9    #[error("unknown plugin session `{0}`")]
10    UnknownSession(String),
11    #[error("plugin action `{0}` requires a session")]
12    MissingSession(String),
13    #[error("plugin action `{0}` does not accept a session")]
14    UnexpectedSession(String),
15    #[error("plugin session registry is unavailable")]
16    SessionRegistryPoisoned,
17}
18
19#[derive(Clone)]
20pub struct RuntimeServices {
21    pub plugins: Arc<PluginSession>,
22    pub attachment_store: Arc<dyn crate::AttachmentStore>,
23    pub process_env_store: Arc<dyn crate::ProcessExecutionEnvStore>,
24    pub clock: Arc<dyn crate::Clock>,
25    pub(crate) store: Option<Arc<dyn crate::store::RuntimePersistence>>,
26}
27
28#[derive(Clone)]
29pub struct PersistentRuntimeServices(RuntimeServices);
30
31impl std::ops::Deref for PersistentRuntimeServices {
32    type Target = RuntimeServices;
33
34    fn deref(&self) -> &Self::Target {
35        &self.0
36    }
37}
38
39pub(crate) struct NoopSessionManager;
40
41impl SessionStateService for NoopSessionManager {}
42impl SessionLifecycleService for NoopSessionManager {}
43impl SessionGraphService for NoopSessionManager {}
44impl RuntimeServices {
45    pub fn new(plugins: Arc<PluginSession>) -> Self {
46        Self {
47            plugins,
48            attachment_store: Arc::new(crate::InMemoryAttachmentStore::new()),
49            process_env_store: Arc::new(crate::InMemoryProcessExecutionEnvStore::new()),
50            clock: Arc::new(crate::SystemClock),
51            store: None,
52        }
53    }
54
55    pub(crate) fn with_clock(mut self, clock: Arc<dyn crate::Clock>) -> Self {
56        self.clock = clock;
57        self
58    }
59
60    pub(crate) fn with_attachment_store(
61        mut self,
62        attachment_store: Arc<dyn crate::AttachmentStore>,
63    ) -> Self {
64        self.attachment_store = attachment_store;
65        self
66    }
67
68    pub(crate) fn with_process_env_store(
69        mut self,
70        process_env_store: Arc<dyn crate::ProcessExecutionEnvStore>,
71    ) -> Self {
72        self.process_env_store = process_env_store;
73        self
74    }
75}
76
77impl PersistentRuntimeServices {
78    pub fn new(
79        plugins: Arc<PluginSession>,
80        store: Arc<dyn crate::store::RuntimePersistence>,
81    ) -> Self {
82        Self(RuntimeServices {
83            plugins,
84            attachment_store: Arc::new(crate::InMemoryAttachmentStore::new()),
85            process_env_store: Arc::new(crate::InMemoryProcessExecutionEnvStore::new()),
86            clock: Arc::new(crate::SystemClock),
87            store: Some(store),
88        })
89    }
90
91    pub(crate) fn into_runtime_services(self) -> RuntimeServices {
92        self.0
93    }
94
95    pub fn store(&self) -> Arc<dyn crate::store::RuntimePersistence> {
96        self.0
97            .store
98            .as_ref()
99            .expect("persistent runtime services must carry a store")
100            .clone()
101    }
102}