Skip to main content

harmos_sdk/plugin/
mod.rs

1/* ***
2 *
3 * Harmos
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * ***
8 */
9
10/* === Modules === */
11
12mod owner;
13
14/* === Re-Exports === */
15
16pub use crate::error::Error;
17pub use harmos_domain::plugin::*;
18pub use owner::{Owner, Table};
19
20/* === Imports === */
21
22// Standard Library Imports
23use std::future::Future;
24use std::pin::Pin;
25use std::sync::Arc;
26
27// Third Party Imports
28use serde::de::DeserializeOwned;
29
30// Local Imports
31use crate::operations::{ServingError, WireTable};
32use harmos_domain::model::operations::lifecycle;
33
34/* === Constants === */
35
36/* === Macros === */
37
38/* === Type Aliases === */
39
40/* === Structs === */
41
42/* === Enums === */
43
44/* === Traits === */
45
46pub trait Context: Clone + Send + Sync + 'static {
47    fn assign_operations(&self, bytes: &[u8]) -> Result<(), ServingError>;
48    fn with_generation(&self, token: [u8; 32]) -> Self;
49}
50
51pub trait Plugin: lifecycle::Policy + Send + Sync + Sized + 'static {
52    fn register(_registrar: &mut crate::Registrar) -> Result<(), crate::RegistrationError> {
53        Ok(())
54    }
55
56    type Config: DeserializeOwned + Default + Send + 'static;
57    type Error: std::error::Error + Send + Sync + 'static;
58    type Context: Context;
59    type Resources: Send + Sync + 'static;
60
61    fn resources(&self) -> Arc<Self::Resources>;
62
63    fn prepare(config: Self::Config) -> Result<Self, Self::Error>;
64
65    fn initialize(&mut self, context: &Self::Context) -> impl Future<Output = Result<(), Self::Error>> + Send;
66
67    fn bind_operations(&self, table: &mut WireTable) -> Result<(), ServingError>;
68
69    fn stop(&mut self, context: &Self::Context) -> impl Future<Output = Result<(), Self::Error>> + Send;
70
71    fn cleanup(&mut self) -> impl Future<Output = Result<(), Self::Error>> + Send;
72
73    fn attachment(&self) -> Option<Arc<dyn Attachment<Self::Context>>> {
74        None
75    }
76}
77
78pub trait Attachment<C>: Send + Sync + 'static {
79    fn observers(&self) -> Vec<crate::inventory::observers::Declaration>;
80    fn resources(&self) -> Arc<dyn std::any::Any + Send + Sync>;
81    fn quiesce(&self) -> Pin<Box<dyn Future<Output = Result<(), ServingError>> + Send + '_>>;
82}
83
84/* === Implementations === */
85
86impl Context for () {
87    fn assign_operations(&self, bytes: &[u8]) -> Result<(), ServingError> {
88        if bytes.is_empty() { Ok(()) } else { Err(ServingError::Catalog) }
89    }
90    fn with_generation(&self, _: [u8; 32]) -> Self {}
91}
92
93/* === Public Functions === */
94
95/* === Private Functions === */
96
97/* === Tests === */