|
| 1 | +//! # Omniqueue |
| 2 | +//! |
| 3 | +//! Omniqueue provides a high-level interface for sending and receiving the following over a range |
| 4 | +//! of queue backends: |
| 5 | +//! |
| 6 | +//! * Raw byte arrays in the way most compatible with the queue backend |
| 7 | +//! |
| 8 | +//! * JSON encoded byte arrays for types that implement [`serde::Deserialize`] and |
| 9 | +//! [`serde::Serialize`] |
| 10 | +//! |
| 11 | +//! * Arbitrary types for which an encoder and/or decoder has been defined |
| 12 | +//! |
| 13 | +//! ## Cargo Features |
| 14 | +//! |
| 15 | +//! Each backend is enabled with its associated cargo feature. All backends are enabled by default. |
| 16 | +//! As of present it supports: |
| 17 | +//! |
| 18 | +//! * In-memory queues |
| 19 | +//! |
| 20 | +//! * RabbitMQ |
| 21 | +//! |
| 22 | +//! * Redis streams |
| 23 | +//! |
| 24 | +//! * SQS |
| 25 | +//! |
| 26 | +//! ## How to Use Omniqueue |
| 27 | +//! |
| 28 | +//! Each queue backend has a unique configuration type. One of these configurations is taken |
| 29 | +//! when constructing the [`queue::QueueBuilder`]. |
| 30 | +//! |
| 31 | +//! To create a simple producer and/or consumer: |
| 32 | +//! |
| 33 | +//! ```compile_fail |
| 34 | +//! let cfg = SqsConfig { |
| 35 | +//! queue_dsn: "http://localhost:9234/queue/queue_name".to_owned(), |
| 36 | +//! override_endpoint: true, |
| 37 | +//! }; |
| 38 | +//! |
| 39 | +//! // Either both producer and consumer |
| 40 | +//! let (p, mut c) = SqsQueueBackend::builder(cfg.clone()).build_pair().await?; |
| 41 | +//! |
| 42 | +//! // Or one half |
| 43 | +//! let p = SqsQueueBackend::builder(cfg.clone()).build_producer().await?; |
| 44 | +//! let mut c = SqsQueueBackend::builder(cfg).build_consumer().await?; |
| 45 | +//! |
| 46 | +//! (p, c) |
| 47 | +//! ``` |
| 48 | +//! |
| 49 | +//! Sending and receiving information from this queue is simple: |
| 50 | +//! |
| 51 | +//! ```compile_fail |
| 52 | +//! p.send_serde_json(&ExampleType::default()).await?; |
| 53 | +//! |
| 54 | +//! let delivery = c.receive().await?; |
| 55 | +//! let payload = delivery.payload_serde_json::<ExampleType>().await?; |
| 56 | +//! delivery.ack().await?; |
| 57 | +//! ``` |
| 58 | +//! |
| 59 | +//! ## `DynProducer`s and `DynConsumer`s |
| 60 | +//! |
| 61 | +//! Dynamic-dispatch can be used easily for when you're not sure which backend to use at |
| 62 | +//! compile-time. |
| 63 | +//! |
| 64 | +//! Making a `DynProducer` or `DynConsumer` is as simple as adding one line to the builder: |
| 65 | +//! |
| 66 | +//! ```compile_fail |
| 67 | +//! let (p, mut c) = RabbitMqBackend::builder(cfg) |
| 68 | +//! .make_dynamic() |
| 69 | +//! .build_pair() |
| 70 | +//! .await?; |
| 71 | +//! ``` |
| 72 | +//! |
| 73 | +//! ## Encoders/Decoders |
| 74 | +//! |
| 75 | +//! The [`encoding::CustomEncoder`]s and [`decoding::CustomDecoder`]s given to the builder upon |
| 76 | +//! producer/consumer creation will be used to convert from/to the queue's native representation |
| 77 | +//! into/from a given type. This helps enforce a separation of responsibilities where only the |
| 78 | +//! application setting up a concrete queue instance should ever have to think about the internal |
| 79 | +//! data-representation of items within the queue while abstract uses of queues should be able to |
| 80 | +//! work with simple Rust types. |
| 81 | +//! |
| 82 | +//! Any function or closure with the right signature may be used as an encoder or decoder. |
| 83 | +//! |
| 84 | +//! ```compile_fail |
| 85 | +//! #[derive(Debug, PartialEq)] |
| 86 | +//! struct ExampleType { |
| 87 | +//! field: u8, |
| 88 | +//! } |
| 89 | +//! |
| 90 | +//! let (p, mut c) = RabbitMqBackend::builder(cfg) |
| 91 | +//! .with_encoder(|et: &ExampleType| -> Result<Vec<u8>, QueueError> { |
| 92 | +//! Ok(vec![et.field]) |
| 93 | +//! }) |
| 94 | +//! .with_decoder(|v: &Vec<u8>| -> Result<ExampleType, QueueError> { |
| 95 | +//! Ok(ExampleType { |
| 96 | +//! field: *v.first().unwrap_or(&0), |
| 97 | +//! }) |
| 98 | +//! }) |
| 99 | +//! ``` |
1 | 100 | use std::fmt::Debug;
|
2 | 101 |
|
3 | 102 | use thiserror::Error;
|
|
0 commit comments