rust_rabbit/
lib.rs

1//! # rust-rabbit 🐰
2//!
3//! A **simple, reliable** RabbitMQ client library for Rust.
4//! Focus on core functionality with minimal configuration.
5//!
6//! ## Features
7//!
8//! - **🚀 Simple API**: Just Publisher and Consumer with essential methods
9//! - **🔄 Flexible Retry**: Exponential, linear, or custom retry mechanisms  
10//! - **🛠️ Auto-Setup**: Automatic queue/exchange declaration and binding
11//! - **⚡ Built-in Reliability**: Default ACK behavior with error handling
12//!
13//! ## Quick Start
14//!
15//! ### Publisher
16//!
17//! ```rust,no_run
18//! use rust_rabbit::{Connection, Publisher, PublishOptions};
19//! use serde::Serialize;
20//!
21//! #[derive(Serialize)]
22//! struct Order {
23//!     id: u32,
24//!     amount: f64,
25//! }
26//!
27//! #[tokio::main]
28//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
29//!     let connection = Connection::new("amqp://localhost:5672").await?;
30//!     let publisher = Publisher::new(connection);
31//!     
32//!     let order = Order { id: 123, amount: 99.99 };
33//!     
34//!     // Publish to exchange
35//!     publisher.publish_to_exchange("orders", "new.order", &order, None).await?;
36//!     
37//!     // Publish directly to queue
38//!     publisher.publish_to_queue("order_queue", &order, None).await?;
39//!     
40//!     Ok(())
41//! }
42//! ```
43//!
44//! ### Consumer with Retry
45//!
46//! ```rust,no_run
47//! use rust_rabbit::{Connection, Consumer, RetryConfig};
48//! use serde::Deserialize;
49//!
50//! #[derive(Deserialize, Clone)]
51//! struct Order {
52//!     id: u32,
53//!     amount: f64,
54//! }
55//!
56//! #[tokio::main]
57//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
58//!     let connection = Connection::new("amqp://localhost:5672").await?;
59//!     
60//!     let consumer = Consumer::builder(connection, "order_queue")
61//!         .with_retry(RetryConfig::exponential_default()) // 1s->2s->4s->8s->16s
62//!         .bind_to_exchange("orders", "order.*")
63//!         .concurrency(5)
64//!         .build();
65//!     
66//!     consumer.consume(|msg: rust_rabbit::Message<Order>| async move {
67//!         println!("Processing order {}: ${}", msg.data.id, msg.data.amount);
68//!         // Your business logic here
69//!         Ok(()) // ACK message
70//!     }).await?;
71//!     
72//!     Ok(())
73//! }
74//! ```
75//!
76//! ## Retry Configurations
77//!
78//! ```rust,no_run
79//! use rust_rabbit::RetryConfig;
80//! use std::time::Duration;
81//!
82//! // Exponential: 1s -> 2s -> 4s -> 8s -> 16s (5 retries)
83//! let exponential = RetryConfig::exponential_default();
84//!
85//! // Custom exponential: 2s -> 4s -> 8s -> 16s -> 32s (with cap at 60s)
86//! let custom_exp = RetryConfig::exponential(5, Duration::from_secs(2), Duration::from_secs(60));
87//!
88//! // Linear: 10s -> 10s -> 10s (3 retries)  
89//! let linear = RetryConfig::linear(3, Duration::from_secs(10));
90//!
91//! // Custom delays: 1s -> 5s -> 30s
92//! let custom = RetryConfig::custom(vec![
93//!     Duration::from_secs(1),
94//!     Duration::from_secs(5),
95//!     Duration::from_secs(30),
96//! ]);
97//!
98//! // No retries
99//! let no_retry = RetryConfig::no_retry();
100//! ```
101//!
102//! ## MessageEnvelope System
103//!
104//! For advanced retry tracking and error handling, use the MessageEnvelope system:
105//!
106//! ```rust,no_run
107//! use rust_rabbit::{Connection, Publisher, Consumer, MessageEnvelope, RetryConfig};
108//! use serde::{Serialize, Deserialize};
109//!
110//! #[derive(Serialize, Deserialize, Clone)]
111//! struct Order {
112//!     id: u32,
113//!     amount: f64,
114//! }
115//!
116//! #[tokio::main]
117//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
118//!     let connection = Connection::new("amqp://localhost:5672").await?;
119//!     
120//!     // Publisher with envelope
121//!     let publisher = Publisher::new(connection.clone());
122//!     let order = Order { id: 123, amount: 99.99 };
123//!     let envelope = MessageEnvelope::new(order, "order_queue")
124//!         .with_max_retries(3);
125//!     
126//!     publisher.publish_envelope_to_queue("order_queue", &envelope, None).await?;
127//!     
128//!     // Consumer with envelope processing
129//!     let consumer = Consumer::builder(connection, "order_queue")
130//!         .with_retry(RetryConfig::exponential_default())
131//!         .build();
132//!     
133//!     consumer.consume_envelopes(|envelope: MessageEnvelope<Order>| async move {
134//!         println!("Processing order {} (attempt {})", 
135//!                  envelope.payload.id, 
136//!                  envelope.metadata.retry_attempt + 1);
137//!         
138//!         // Access retry metadata
139//!         if !envelope.is_first_attempt() {
140//!             println!("This is a retry. Last error: {:?}", envelope.last_error());
141//!         }
142//!         
143//!         // Your business logic here
144//!         Ok(())
145//!     }).await?;
146//!     
147//!     Ok(())
148//! }
149//! ```
150
151// Re-export main types for easy access
152pub use connection::Connection;
153pub use consumer::{Consumer, ConsumerBuilder, Message};
154pub use error::{Result, RustRabbitError};
155pub use message::{ErrorRecord, ErrorType, MessageEnvelope, MessageMetadata, MessageSource};
156pub use publisher::{PublishOptions, Publisher};
157pub use retry::{RetryConfig, RetryMechanism};
158
159// Internal modules
160mod connection;
161mod consumer;
162mod error;
163mod message;
164mod publisher;
165mod retry;
166
167/// Prelude module for convenient imports
168pub mod prelude {
169    pub use crate::{
170        Connection, Consumer, ConsumerBuilder, ErrorRecord, ErrorType, Message, MessageEnvelope,
171        MessageMetadata, MessageSource, PublishOptions, Publisher, Result, RetryConfig,
172        RetryMechanism, RustRabbitError,
173    };
174}
175
176#[cfg(test)]
177mod tests {
178    use super::*;
179    use serde::{Deserialize, Serialize};
180    use std::time::Duration;
181
182    #[derive(Debug, Serialize, Deserialize, PartialEq)]
183    #[allow(dead_code)]
184    struct TestMessage {
185        id: u32,
186        content: String,
187    }
188
189    #[tokio::test]
190    async fn test_api_compilation() {
191        // This test ensures the API compiles correctly
192        // Real integration tests would require a RabbitMQ instance
193
194        let _connection_result = Connection::new("amqp://localhost:5672").await;
195
196        // Test retry configurations
197        let _exponential = RetryConfig::exponential_default();
198        let _linear = RetryConfig::linear(3, Duration::from_secs(5));
199        let _custom = RetryConfig::custom(vec![Duration::from_secs(1), Duration::from_secs(5)]);
200        let _no_retry = RetryConfig::no_retry();
201    }
202
203    #[test]
204    fn test_basic_api_exists() {
205        // Test that our main types exist and can be referenced
206        use crate::prelude::*;
207
208        // This is a compile-time test - if it compiles, our API is accessible
209        let _: Option<Connection> = None;
210        let _: Option<Publisher> = None;
211        let _: Option<Consumer> = None;
212        let _: Option<RetryConfig> = None;
213
214        // Test that we can create basic configs
215        let _retry = RetryConfig::exponential_default();
216        let _options = PublishOptions::new();
217    }
218
219    #[test]
220    fn test_retry_config_calculations() {
221        let config = RetryConfig::exponential(5, Duration::from_secs(1), Duration::from_secs(30));
222
223        assert_eq!(config.calculate_delay(0), Some(Duration::from_secs(1)));
224        assert_eq!(config.calculate_delay(1), Some(Duration::from_secs(2)));
225        assert_eq!(config.calculate_delay(2), Some(Duration::from_secs(4)));
226        assert_eq!(config.calculate_delay(3), Some(Duration::from_secs(8)));
227        assert_eq!(config.calculate_delay(4), Some(Duration::from_secs(16)));
228        // Attempt 5 exceeds max_retries (5), so should return None
229        assert_eq!(config.calculate_delay(5), None);
230    }
231
232    #[test]
233    fn test_retry_config_linear() {
234        let config = RetryConfig::linear(3, Duration::from_secs(5));
235
236        assert_eq!(config.max_retries, 3);
237        assert_eq!(config.calculate_delay(0), Some(Duration::from_secs(5)));
238        assert_eq!(config.calculate_delay(1), Some(Duration::from_secs(5)));
239        assert_eq!(config.calculate_delay(2), Some(Duration::from_secs(5)));
240        assert_eq!(config.calculate_delay(3), None); // Exceeds max_retries
241    }
242
243    #[test]
244    fn test_retry_config_custom() {
245        let delays = vec![
246            Duration::from_secs(1),
247            Duration::from_secs(3),
248            Duration::from_secs(7),
249        ];
250        let config = RetryConfig::custom(delays.clone());
251
252        assert_eq!(config.max_retries, 3);
253        assert_eq!(config.calculate_delay(0), Some(Duration::from_secs(1)));
254        assert_eq!(config.calculate_delay(1), Some(Duration::from_secs(3)));
255        assert_eq!(config.calculate_delay(2), Some(Duration::from_secs(7)));
256        assert_eq!(config.calculate_delay(3), None); // Exceeds max_retries
257    }
258
259    #[test]
260    fn test_publish_options() {
261        let options = PublishOptions::new()
262            .mandatory()
263            .with_expiration("60000")
264            .with_priority(5);
265
266        assert!(options.mandatory);
267        assert_eq!(options.expiration, Some("60000".to_string()));
268        assert_eq!(options.priority, Some(5));
269    }
270}