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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//! Derive macros for [`queuey`](https://docs.rs/queuey).
//!
//! * [`macro@Queues`] implements `queuey_core::QueueSet` for a fieldless enum.
//! * [`macro@Job`] implements `queuey_core::Job` for a serializable payload type.
//!
//! # Where the generated code points
//!
//! Generated code needs a path to `queuey-core`. Both macros work that
//! out from the *calling* crate's `Cargo.toml` (via `proc-macro-crate`):
//!
//! 1. a dependency on `queuey` (the facade), which emits
//! `::queuey::__core`, its hidden re-export of the core crate;
//! 2. otherwise a dependency on `queuey-core`, which emits
//! `::queuey_core`;
//! 3. otherwise it falls back to `::queuey_core`.
//!
//! Renamed dependencies (`aq = { package = "queuey" }`) are handled. For
//! anything else (a vendored copy, a re-export under yet another name) say so
//! explicitly with `#[queues(crate = "...")]` / `#[job(crate = "...")]`, which
//! always wins.
//!
//! # Duration literals
//!
//! Every duration in these attributes is a string literal parsed while the macro
//! runs: an integer followed by an optional unit of `ms`, `s`, `m`, `h` or `d`.
//! A bare integer means seconds. Whitespace is ignored, so `"500ms"`, `"30s"`,
//! `"2 m"` and `"30"` are all valid. Anything else is a compile error pointing at
//! the literal. Zero is rejected everywhere a duration is accepted: a zero
//! `message_ttl` discards every message on publish, and a zero backoff is
//! spelled `backoff = "none"`.
//!
//! # `retry(...)` grammar
//!
//! Shared by `#[queue(...)]` and `#[job(...)]`:
//!
//! ```text
//! retry(
//! max_attempts = 3, // u32 >= 1, default 3 (1 means no retries)
//! backoff = "exponential", // "none" | "fixed" | "exponential", default "exponential"
//! delay = "1s", // fixed only, required for "fixed"
//! base = "1s", // exponential only, default "1s", must be <= max
//! factor = 2.0, // exponential only, default 2.0, must be > 0
//! max = "5m", // exponential only, default "5m"
//! jitter = true, // exponential only, default true
//! )
//! ```
//!
//! The exponential defaults match `queuey_core::Backoff::exponential()`.
use TokenStream;
use ;
/// Implement `queuey_core::QueueSet` for a fieldless enum.
///
/// The enum must also derive the trait's supertraits; the macro deliberately does
/// not add them for you:
///
/// ```text
/// #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Queues)]
/// ```
///
/// # Attributes
///
/// Container attribute `#[queues(...)]`, optional:
///
/// ```text
/// #[queues(
/// prefix = "myapp", // queue names become "myapp.<name>"
/// crate = "queuey", // path to the core crate re-export
/// )]
/// ```
///
/// Variant attribute `#[queue(...)]`, optional on every variant:
///
/// ```text
/// #[queue(
/// name = "img", // default: snake_case of the variant
/// prefetch = 10, // u16
/// durable = true, // bool
/// message_ttl = "30s", // duration literal
/// max_priority = 10, // u8 in 0..=255; 0 disables priorities
/// retry(max_attempts = 3, backoff = "exponential", base = "1s", factor = 2.0,
/// max = "5m", jitter = true),
/// )]
/// ```
///
/// `max_priority` is the number of AMQP priority levels the queue is declared
/// with (`x-max-priority`). Omitted, the `QueueConfig` default applies;
/// `max_priority = 0` turns priorities off, so the queue carries no
/// `x-max-priority` argument at all. **Changing this value on a queue
/// that already exists is refused by the broker**: RabbitMQ answers a redeclare
/// with different arguments with `PRECONDITION_FAILED`, so an existing
/// deployment must delete the queue first.
///
/// # Example
///
/// ```
/// use queuey_core::{QueueSet, Backoff};
/// use queuey_macros::Queues;
///
/// #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Queues)]
/// #[queues(prefix = "myapp")]
/// enum AppQueues {
/// #[queue(prefetch = 10, max_priority = 5)]
/// Emails,
/// #[queue(name = "img", message_ttl = "30s", max_priority = 0, retry(max_attempts = 5))]
/// ImageResize,
/// }
///
/// assert_eq!(AppQueues::Emails.name(), "myapp.emails");
/// assert_eq!(AppQueues::ImageResize.name(), "myapp.img");
/// assert_eq!(AppQueues::Emails.config().prefetch, 10);
/// assert_eq!(AppQueues::Emails.config().max_priority, Some(5));
/// assert_eq!(AppQueues::ImageResize.config().max_priority, None);
/// assert_eq!(AppQueues::from_name("myapp.img"), Some(AppQueues::ImageResize));
/// ```
///
/// # Errors
///
/// Compile errors, spanned at the offending token, are produced for: a non-enum
/// item, a generic enum, an enum without variants, a variant with fields,
/// duplicate resolved queue names, unknown or duplicated attribute keys, a
/// literal of the wrong type, an empty `prefix`/`name`/resolved queue name, a
/// `prefetch` outside `1..=65535` (`0` means *unlimited* in AMQP, so omit the key
/// instead), a `max_priority` outside `0..=255`, a `max_attempts` outside
/// `1..=u32::MAX`, a malformed or zero
/// duration, an unknown backoff kind, `base` greater than `max`, `delay` outside
/// of `backoff = "fixed"`, and `base`/`factor`/`max`/`jitter` outside of
/// `backoff = "exponential"`.
/// Implement `queuey_core::Job` for a struct or enum.
///
/// The type must also be `Serialize + DeserializeOwned`; add those derives
/// yourself, this macro never generates them.
///
/// # Attributes
///
/// ```text
/// #[job(
/// queue = AppQueues::Emails, // required; Job::Queue = AppQueues
/// name = "emails.send", // default: module_path!() + "::" + type name
/// retry(max_attempts = 5), // optional; generates retry_policy()
/// crate = "queuey", // path to the core crate re-export
/// )]
/// ```
///
/// `queue` is a path with at least two segments: the last segment is the variant
/// (`Job::QUEUE`) and everything before it is the queue set type (`Job::Queue`).
///
/// # Example
///
/// ```
/// use queuey_core::Job;
/// use queuey_macros::{Job, Queues};
/// use serde::{Deserialize, Serialize};
///
/// #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Queues)]
/// enum AppQueues {
/// Emails,
/// }
///
/// #[derive(Job, Serialize, Deserialize)]
/// #[job(queue = AppQueues::Emails, retry(max_attempts = 5, backoff = "fixed", delay = "2s"))]
/// struct SendEmail {
/// to: String,
/// }
///
/// assert_eq!(SendEmail::QUEUE, AppQueues::Emails);
/// assert!(SendEmail::retry_policy().is_some());
/// ```
///
/// # Errors
///
/// Compile errors, spanned at the offending token, are produced for: a missing
/// `queue` key, a `queue` path with a single segment, a generic type, unknown or
/// duplicated attribute keys, and every `retry(...)` error listed on
/// [`macro@Queues`].