1pub mod processor;
6pub mod processor_pool;
7pub mod router;
8pub mod sender;
9
10pub use processor::{
12 DefaultPlainMessageProcessor, LoggingPlainMessageProcessor, PlainMessageProcessor,
13 ValidationPlainMessageProcessor,
14};
15pub use processor_pool::{ProcessorPool, ProcessorPoolConfig};
16pub use router::DefaultPlainMessageRouter;
17pub use sender::{HttpPlainMessageSender, NodePlainMessageSender, PlainMessageSender};
18
19use crate::error::Result;
21use async_trait::async_trait;
22use tap_msg::didcomm::PlainMessage;
23
24pub trait PlainMessageRouter: Send + Sync {
26 fn route_message_impl(&self, message: &PlainMessage) -> Result<String>;
28}
29
30#[async_trait]
32pub trait RouterAsyncExt: PlainMessageRouter {
33 async fn route_message(&self, message: &PlainMessage) -> Result<String>;
35}
36
37#[async_trait]
38impl<T: PlainMessageRouter + Sync> RouterAsyncExt for T {
39 async fn route_message(&self, message: &PlainMessage) -> Result<String> {
40 self.route_message_impl(message)
41 }
42}
43
44#[derive(Clone, Debug)]
46pub enum PlainMessageProcessorType {
47 Default(DefaultPlainMessageProcessor),
48 Logging(LoggingPlainMessageProcessor),
49 Validation(ValidationPlainMessageProcessor),
50 Composite(CompositePlainMessageProcessor),
51}
52
53#[derive(Clone, Debug)]
55pub enum PlainMessageRouterType {
56 Default(DefaultPlainMessageRouter),
57}
58
59#[derive(Clone, Debug)]
61pub struct CompositePlainMessageProcessor {
62 processors: Vec<PlainMessageProcessorType>,
63}
64
65impl CompositePlainMessageProcessor {
66 pub fn new(processors: Vec<PlainMessageProcessorType>) -> Self {
68 Self { processors }
69 }
70
71 pub fn add_processor(&mut self, processor: PlainMessageProcessorType) {
73 self.processors.push(processor);
74 }
75}
76
77#[async_trait]
78impl PlainMessageProcessor for CompositePlainMessageProcessor {
79 async fn process_incoming(&self, message: PlainMessage) -> Result<Option<PlainMessage>> {
80 let mut current_message = message;
81
82 for processor in &self.processors {
83 let processed = match processor {
84 PlainMessageProcessorType::Default(p) => {
85 p.process_incoming(current_message).await?
86 }
87 PlainMessageProcessorType::Logging(p) => {
88 p.process_incoming(current_message).await?
89 }
90 PlainMessageProcessorType::Validation(p) => {
91 p.process_incoming(current_message).await?
92 }
93 PlainMessageProcessorType::Composite(p) => {
94 p.process_incoming(current_message).await?
95 }
96 };
97
98 if let Some(msg) = processed {
99 current_message = msg;
100 } else {
101 return Ok(None);
103 }
104 }
105
106 Ok(Some(current_message))
107 }
108
109 async fn process_outgoing(&self, message: PlainMessage) -> Result<Option<PlainMessage>> {
110 let mut current_message = message;
111
112 for processor in &self.processors {
113 let processed = match processor {
114 PlainMessageProcessorType::Default(p) => {
115 p.process_outgoing(current_message).await?
116 }
117 PlainMessageProcessorType::Logging(p) => {
118 p.process_outgoing(current_message).await?
119 }
120 PlainMessageProcessorType::Validation(p) => {
121 p.process_outgoing(current_message).await?
122 }
123 PlainMessageProcessorType::Composite(p) => {
124 p.process_outgoing(current_message).await?
125 }
126 };
127
128 if let Some(msg) = processed {
129 current_message = msg;
130 } else {
131 return Ok(None);
133 }
134 }
135
136 Ok(Some(current_message))
137 }
138}
139
140#[derive(Clone)]
142pub struct CompositePlainMessageRouter {
143 routers: Vec<PlainMessageRouterType>,
144}
145
146impl CompositePlainMessageRouter {
147 pub fn new(routers: Vec<PlainMessageRouterType>) -> Self {
149 Self { routers }
150 }
151
152 pub fn add_router(&mut self, router: PlainMessageRouterType) {
154 self.routers.push(router);
155 }
156}
157
158impl PlainMessageRouter for CompositePlainMessageRouter {
159 fn route_message_impl(&self, message: &PlainMessage) -> Result<String> {
160 for router in &self.routers {
162 let result = match router {
163 PlainMessageRouterType::Default(r) => r.route_message_impl(message),
164 };
165
166 match result {
167 Ok(did) => return Ok(did),
168 Err(_) => continue, }
170 }
171
172 Err(crate::error::Error::Routing(
174 "No router could handle the message".to_string(),
175 ))
176 }
177}