Skip to main content

shepherd_rs/transform/
attempt.rs

1use std::error::Error;
2use std::fmt::Debug;
3use std::hash::Hash;
4
5/// A trait representing a transformation attempt in the shepherd-rs
6/// processing system. Each transformation attempt is an attempt to resolve a
7/// `TransformRequest` and produce an output based on the input provided.
8///
9/// The `Input` and `Output` types are generic, associated types, but they are
10/// a little more complex than the `TransformRequest`'s `Input` and `Output`
11/// types. A `TransformAttempt`'s `Input` to includes the `TransformRequest`'s
12/// `Input`, but also includes a `CallCtx`, a attempt-specific additional data
13/// needed for processing and an `Identifier` that uniquely identifies the
14/// attempt.
15pub trait TransformAttempt: Send + Sync + Clone {
16    type TransformRequestIdentifier: Eq + Clone + Hash + Send + Sync + Debug;
17    type Identifier: Eq
18        + Clone
19        + Hash
20        + Send
21        + Sync
22        + Debug
23        + Into<Self::TransformRequestIdentifier>;
24    type CallCtx: Send;
25    type CallArgsType: Send;
26    type ReturnCtx: Send + Sync + Clone;
27    type ReturnType: Send + Sync + Clone;
28
29    type TransformError: Send + Sync + Clone + Error;
30
31    type SendPackage = (Self::Identifier, Self::CallCtx, Self::CallArgsType);
32
33    type ReturnPackage: Send + Sync + Clone = (
34        Self::Identifier,
35        Self::ReturnCtx,
36        Result<Self::ReturnType, Self::TransformError>,
37    );
38
39    fn request_id(&self) -> Self::TransformRequestIdentifier;
40    fn attempt_id(&self) -> Self::Identifier;
41
42    fn new(
43        attempt_id: Self::Identifier,
44        call_ctx: Self::CallCtx,
45        call_val: Self::CallArgsType,
46    ) -> Self;
47
48    fn set_return_package(&mut self, return_pkg: Self::ReturnPackage);
49
50    fn from_return_package(
51        attempt_id: Self::Identifier,
52        return_package: Self::ReturnPackage,
53    ) -> Self;
54}
55
56#[cfg(test)]
57#[derive(Clone, serde::Serialize, serde::Deserialize)]
58pub struct NilTA;
59
60#[cfg(test)]
61#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
62pub struct NilTAError;
63
64#[cfg(test)]
65impl std::fmt::Display for NilTAError {
66    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
67        write!(f, "NilTAError: This is a placeholder error for NilTA")
68    }
69}
70
71#[cfg(test)]
72impl std::error::Error for NilTAError {}
73
74#[cfg(test)]
75impl TransformAttempt for NilTA {
76    type CallArgsType = ();
77    type CallCtx = ();
78    type Identifier = ();
79    type ReturnCtx = ();
80    type ReturnPackage = (
81        Self::Identifier,
82        Self::ReturnCtx,
83        Result<Self::ReturnType, Self::TransformError>,
84    );
85    type ReturnType = ();
86    type SendPackage = (Self::Identifier, Self::CallCtx, Self::CallArgsType);
87    type TransformError = NilTAError;
88    type TransformRequestIdentifier = ();
89
90    fn request_id(&self) -> Self::TransformRequestIdentifier { () }
91
92    fn attempt_id(&self) -> Self::Identifier { () }
93
94    fn new(
95        _attempt_id: Self::Identifier,
96        _call_ctx: Self::CallCtx,
97        _call_val: Self::CallArgsType,
98    ) -> Self {
99        NilTA
100    }
101
102    fn set_return_package(&mut self, _return_pkg: Self::ReturnPackage) {}
103
104    fn from_return_package(
105        _attempt_id: Self::Identifier,
106        _return_package: Self::ReturnPackage,
107    ) -> Self {
108        NilTA
109    }
110}