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 lashlang_artifact_store: Arc<dyn lashlang::LashlangArtifactStore>,
24    pub(crate) store: Option<Arc<dyn crate::store::RuntimePersistence>>,
25}
26
27#[derive(Clone)]
28pub struct PersistentRuntimeServices(RuntimeServices);
29
30impl std::ops::Deref for PersistentRuntimeServices {
31    type Target = RuntimeServices;
32
33    fn deref(&self) -> &Self::Target {
34        &self.0
35    }
36}
37
38pub(crate) struct NoopSessionManager;
39
40impl SessionStateService for NoopSessionManager {}
41impl SessionLifecycleService for NoopSessionManager {}
42impl SessionGraphService for NoopSessionManager {}
43impl RuntimeServices {
44    pub fn new(plugins: Arc<PluginSession>) -> Self {
45        Self {
46            plugins,
47            attachment_store: Arc::new(crate::InMemoryAttachmentStore::new()),
48            lashlang_artifact_store: lashlang::global_in_memory_lashlang_artifact_store(),
49            store: None,
50        }
51    }
52
53    pub(crate) fn with_attachment_store(
54        mut self,
55        attachment_store: Arc<dyn crate::AttachmentStore>,
56    ) -> Self {
57        self.attachment_store = attachment_store;
58        self
59    }
60
61    pub(crate) fn with_lashlang_artifact_store(
62        mut self,
63        artifact_store: Arc<dyn lashlang::LashlangArtifactStore>,
64    ) -> Self {
65        self.lashlang_artifact_store = artifact_store;
66        self
67    }
68}
69
70impl PersistentRuntimeServices {
71    pub fn new(
72        plugins: Arc<PluginSession>,
73        store: Arc<dyn crate::store::RuntimePersistence>,
74    ) -> Self {
75        Self(RuntimeServices {
76            plugins,
77            attachment_store: Arc::new(crate::InMemoryAttachmentStore::new()),
78            lashlang_artifact_store: lashlang::global_in_memory_lashlang_artifact_store(),
79            store: Some(store),
80        })
81    }
82
83    pub(crate) fn into_runtime_services(self) -> RuntimeServices {
84        self.0
85    }
86
87    pub fn store(&self) -> Arc<dyn crate::store::RuntimePersistence> {
88        self.0
89            .store
90            .as_ref()
91            .expect("persistent runtime services must carry a store")
92            .clone()
93    }
94}