1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
use std::marker::PhantomData;
use std::sync::Arc;
use std::time::Duration;
use crate::producer::NoDedup;
use crate::types::OffsetSpecification;
use crate::{
client::{Client, ClientOptions, MetricsCollector},
consumer::ConsumerBuilder,
error::StreamDeleteError,
producer::ProducerBuilder,
stream_creator::StreamCreator,
RabbitMQStreamResult,
};
#[derive(Clone)]
pub struct Environment {
pub(crate) options: EnvironmentOptions,
}
impl Environment {
pub fn builder() -> EnvironmentBuilder {
EnvironmentBuilder(EnvironmentOptions::default())
}
async fn boostrap(options: EnvironmentOptions) -> RabbitMQStreamResult<Self> {
let client = Client::connect(options.client_options.clone()).await?;
client.close().await?;
Ok(Environment { options })
}
pub fn stream_creator(&self) -> StreamCreator {
StreamCreator::new(self.clone())
}
pub fn producer(&self) -> ProducerBuilder<NoDedup> {
ProducerBuilder {
environment: self.clone(),
name: None,
batch_size: 100,
batch_publishing_delay: Duration::from_millis(100),
data: PhantomData,
}
}
pub fn consumer(&self) -> ConsumerBuilder {
ConsumerBuilder {
environment: self.clone(),
offset_specification: OffsetSpecification::Next,
}
}
pub(crate) async fn create_client(&self) -> RabbitMQStreamResult<Client> {
Client::connect(self.options.client_options.clone()).await
}
pub async fn delete_stream(&self, stream: &str) -> Result<(), StreamDeleteError> {
let response = self.create_client().await?.delete_stream(stream).await?;
if response.is_ok() {
Ok(())
} else {
Err(StreamDeleteError::Delete {
stream: stream.to_owned(),
status: response.code().clone(),
})
}
}
}
pub struct EnvironmentBuilder(EnvironmentOptions);
impl EnvironmentBuilder {
pub async fn build(self) -> RabbitMQStreamResult<Environment> {
Environment::boostrap(self.0).await
}
pub fn host(mut self, host: &str) -> EnvironmentBuilder {
self.0.client_options.host = host.to_owned();
self
}
pub fn username(mut self, username: &str) -> EnvironmentBuilder {
self.0.client_options.user = username.to_owned();
self
}
pub fn password(mut self, password: &str) -> EnvironmentBuilder {
self.0.client_options.password = password.to_owned();
self
}
pub fn virtual_host(mut self, virtual_host: &str) -> EnvironmentBuilder {
self.0.client_options.v_host = virtual_host.to_owned();
self
}
pub fn port(mut self, port: u16) -> EnvironmentBuilder {
self.0.client_options.port = port;
self
}
pub fn metrics_collector(
mut self,
collector: impl MetricsCollector + Send + Sync + 'static,
) -> EnvironmentBuilder {
self.0.client_options.collector = Arc::new(collector);
self
}
}
#[derive(Clone, Default)]
pub struct EnvironmentOptions {
pub(crate) client_options: ClientOptions,
}