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, lifecycle};
32
33/* === Constants === */
34
35/* === Macros === */
36
37/* === Type Aliases === */
38
39/* === Structs === */
40
41/* === Enums === */
42
43/* === Traits === */
44
45pub trait Context: Clone + Send + Sync + 'static {
46    fn with_generation(&self, token: [u8; 32]) -> Self;
47}
48
49pub trait Plugin: lifecycle::Policy + Send + Sync + Sized + 'static {
50    fn register(_registrar: &mut crate::Registrar) -> Result<(), crate::RegistrationError> {
51        Ok(())
52    }
53
54    type Config: DeserializeOwned + Default + Send + 'static;
55    type Error: std::error::Error + Send + Sync + 'static;
56    type Context: Context;
57    type Resources: Send + Sync + 'static;
58
59    fn resources(&self) -> Arc<Self::Resources>;
60
61    fn prepare(config: Self::Config) -> Result<Self, Self::Error>;
62
63    fn initialize(&mut self, context: &Self::Context) -> impl Future<Output = Result<(), Self::Error>> + Send;
64
65    fn bind_operations(&self, table: &mut WireTable<Self::Context>) -> Result<(), ServingError>;
66
67    fn stop(&mut self, context: &Self::Context) -> impl Future<Output = Result<(), Self::Error>> + Send;
68
69    fn cleanup(&mut self) -> impl Future<Output = Result<(), Self::Error>> + Send;
70
71    fn attachment(&self) -> Option<Arc<dyn Attachment<Self::Context>>> {
72        None
73    }
74}
75
76pub trait Attachment<C>: Send + Sync + 'static {
77    fn observers(&self) -> Vec<crate::inventory::observers::Declaration>;
78    fn resources(&self) -> Arc<dyn std::any::Any + Send + Sync>;
79    fn quiesce(&self) -> Pin<Box<dyn Future<Output = Result<(), ServingError>> + Send + '_>>;
80}
81
82/* === Implementations === */
83
84impl Context for () {
85    fn with_generation(&self, _: [u8; 32]) -> Self {}
86}
87
88/* === Public Functions === */
89
90/* === Private Functions === */
91
92/* === Tests === */