harmos_sdk/operations/
authored.rs1use std::future::Future;
18use std::sync::Arc;
19
20use super::{FailureKind, OperationDeclaration};
24
25pub trait EndpointSet: Send + 'static {
38 fn item_types() -> (Option<std::any::TypeId>, Option<std::any::TypeId>);
39 fn transferred(&self) -> bool {
40 Self::item_types() != (None, None)
41 }
42}
43
44pub trait InputEndpoint: Send + 'static {
45 type Item: Send + 'static;
46 fn transferred(&self) -> bool {
47 true
48 }
49}
50pub trait OutputEndpoint: Send + 'static {
51 type Item: Send + 'static;
52 fn transferred(&self) -> bool {
53 true
54 }
55}
56
57pub trait Operation: Send + Sized + 'static {
58 type Declaration: OperationDeclaration;
59 type Resources: Send + Sync + 'static;
60 type Error: std::error::Error + Send + Sync + 'static;
61 type Context<'a>: Send;
62 type Endpoints: EndpointSet;
63
64 fn initialize<'ctx>(
67 resources: Arc<Self::Resources>, context: Self::Context<'ctx>,
68 ) -> impl Future<Output = Result<Self, Self::Error>> + Send;
69
70 fn run<'ctx>(
71 &mut self, context: Self::Context<'ctx>, request: &mut Self::Declaration, endpoints: &mut Self::Endpoints,
72 ) -> impl Future<Output = Result<super::Response<<Self::Declaration as OperationDeclaration>::Response>, Self::Error>> + Send;
73
74 fn stop(&mut self) -> impl Future<Output = Result<(), Self::Error>> + Send {
75 async { Ok(()) }
76 }
77
78 fn cleanup(&mut self) -> impl Future<Output = Result<(), Self::Error>> + Send {
79 async { Ok(()) }
80 }
81
82 fn transferred(&self) -> bool {
83 false
84 }
85
86 fn classify(_error: &Self::Error) -> FailureKind {
87 FailureKind::Permanent
88 }
89}
90
91impl EndpointSet for () {
94 fn item_types() -> (Option<std::any::TypeId>, Option<std::any::TypeId>) {
95 (None, None)
96 }
97}
98
99impl<I: InputEndpoint, O: OutputEndpoint> EndpointSet for (I, O) {
100 fn transferred(&self) -> bool {
101 self.0.transferred() || self.1.transferred()
102 }
103 fn item_types() -> (Option<std::any::TypeId>, Option<std::any::TypeId>) {
104 (Some(std::any::TypeId::of::<I::Item>()), Some(std::any::TypeId::of::<O::Item>()))
105 }
106}
107
108