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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
//! Plugin trait for composable Autumn integrations.
//!
//! A [`Plugin`] encapsulates configuration and wiring for a reusable piece of
//! infrastructure (durable workflows, live feeds, telemetry exporters, etc.)
//! that attaches itself to an [`AppBuilder`]. Users register plugins with
//! [`AppBuilder::plugin`](crate::app::AppBuilder::plugin) or the tuple-taking
//! [`AppBuilder::plugins`](crate::app::AppBuilder::plugins); each plugin's
//! [`build`](Plugin::build) runs exactly once.
//!
//! # Naming conventions
//!
//! First-party plugin crates are named `autumn-<name>-plugin`. Third-party
//! crates are named `autumn-plugin-<name>` to keep names unambiguous on
//! crates.io. Each crate exposes a `<Name>Plugin` struct at its root with a
//! `::new()` constructor and `#[must_use]` fluent configuration methods.
//!
//! # Authoring a plugin
//!
//! ```rust,no_run
//! use autumn_web::app::AppBuilder;
//! use autumn_web::plugin::Plugin;
//!
//! pub struct HelloPlugin {
//! greeting: String,
//! }
//!
//! impl HelloPlugin {
//! #[must_use]
//! pub fn new() -> Self {
//! Self { greeting: "hello".to_owned() }
//! }
//!
//! #[must_use]
//! pub fn greeting(mut self, greeting: impl Into<String>) -> Self {
//! self.greeting = greeting.into();
//! self
//! }
//! }
//!
//! impl Plugin for HelloPlugin {
//! fn build(self, app: AppBuilder) -> AppBuilder {
//! let greeting = self.greeting;
//! app.on_startup(move |_state| {
//! let greeting = greeting.clone();
//! async move {
//! tracing::info!(%greeting, "hello plugin started");
//! Ok(())
//! }
//! })
//! }
//! }
//! ```
//!
//! # Duplicate registration
//!
//! Registering two plugins that share the same [`Plugin::name`] is a no-op
//! after the first: the second call emits a `tracing::warn!` and returns the
//! builder unchanged. The default name is [`std::any::type_name`] of the
//! plugin struct, so two different instances of the same type collide by
//! default -- override [`Plugin::name`] if a plugin is genuinely designed to
//! be registered more than once.
use std::borrow::Cow;
use crate::app::AppBuilder;
/// A reusable Autumn integration that wires itself into an [`AppBuilder`].
///
/// See the [module-level documentation](self) for conventions and examples.
pub trait Plugin: Sized + Send + 'static {
/// Stable identifier used for duplicate-registration detection.
///
/// Defaults to [`std::any::type_name`] of the concrete plugin struct, so
/// two instances of the same type collide by default. Override to allow
/// multiple instances of the same type to coexist; the return type is
/// [`Cow<'static, str>`](std::borrow::Cow) so plugins can compute a
/// unique label from runtime configuration without leaking memory.
fn name(&self) -> Cow<'static, str> {
Cow::Borrowed(std::any::type_name::<Self>())
}
/// Apply this plugin's configuration to the builder.
///
/// Called exactly once per `AppBuilder`. Implementations typically chain
/// [`AppBuilder::on_startup`], [`AppBuilder::on_shutdown`],
/// [`AppBuilder::nest`], [`AppBuilder::with_extension`] and (with the
/// `db` feature) [`AppBuilder::migrations`].
///
/// Plugins can also install **tier-1 subsystem replacements** here —
/// [`AppBuilder::with_config_loader`], [`AppBuilder::with_pool_provider`]
/// (with the `db` feature), [`AppBuilder::with_telemetry_provider`], and
/// [`AppBuilder::with_session_store`] — which is the canonical way to
/// distribute a custom subsystem (e.g. `AwsSecretsConfigPlugin`) for
/// downstream consumers as a one-line install. See
/// `docs/guide/extensibility.md` for the full extensibility model.
#[must_use]
fn build(self, app: AppBuilder) -> AppBuilder;
}
/// A bundle of plugins that can be applied to an [`AppBuilder`] in one call.
///
/// Implemented for every [`Plugin`] and for tuples of up to eight plugins.
/// Used by [`AppBuilder::plugins`](crate::app::AppBuilder::plugins).
pub trait Plugins: Sized {
/// Apply every plugin in this bundle to the builder, in declaration order.
#[must_use]
fn apply(self, app: AppBuilder) -> AppBuilder;
}
impl<P: Plugin> Plugins for P {
fn apply(self, app: AppBuilder) -> AppBuilder {
app.plugin(self)
}
}
macro_rules! impl_plugins_tuple {
($($idx:tt => $ty:ident),+ $(,)?) => {
impl<$($ty: Plugin),+> Plugins for ($($ty,)+) {
#[allow(non_snake_case)]
fn apply(self, app: AppBuilder) -> AppBuilder {
let ($($ty,)+) = self;
let app = app;
$(let app = app.plugin($ty);)+
app
}
}
};
}
impl_plugins_tuple!(0 => P0);
impl_plugins_tuple!(0 => P0, 1 => P1);
impl_plugins_tuple!(0 => P0, 1 => P1, 2 => P2);
impl_plugins_tuple!(0 => P0, 1 => P1, 2 => P2, 3 => P3);
impl_plugins_tuple!(0 => P0, 1 => P1, 2 => P2, 3 => P3, 4 => P4);
impl_plugins_tuple!(0 => P0, 1 => P1, 2 => P2, 3 => P3, 4 => P4, 5 => P5);
impl_plugins_tuple!(0 => P0, 1 => P1, 2 => P2, 3 => P3, 4 => P4, 5 => P5, 6 => P6);
impl_plugins_tuple!(
0 => P0, 1 => P1, 2 => P2, 3 => P3, 4 => P4, 5 => P5, 6 => P6, 7 => P7
);
#[cfg(test)]
mod tests {
use std::sync::Arc;
use std::sync::Mutex;
use super::*;
#[derive(Default)]
struct Recorder {
events: Arc<Mutex<Vec<&'static str>>>,
}
impl Recorder {
fn new() -> Self {
Self::default()
}
fn events(&self) -> Vec<&'static str> {
self.events
.lock()
.expect("lock shouldn't be poisoned")
.clone()
}
fn push(&self, label: &'static str) {
self.events
.lock()
.expect("lock shouldn't be poisoned")
.push(label);
}
}
struct RecordingPlugin {
label: &'static str,
recorder: Arc<Recorder>,
}
impl Plugin for RecordingPlugin {
fn name(&self) -> Cow<'static, str> {
Cow::Borrowed(self.label)
}
fn build(self, app: AppBuilder) -> AppBuilder {
self.recorder.push(self.label);
app
}
}
struct ColaPlugin {
recorder: Arc<Recorder>,
}
impl Plugin for ColaPlugin {
fn build(self, app: AppBuilder) -> AppBuilder {
self.recorder.push("cola");
app
}
}
struct PepsiPlugin {
recorder: Arc<Recorder>,
}
impl Plugin for PepsiPlugin {
fn build(self, app: AppBuilder) -> AppBuilder {
self.recorder.push("pepsi");
app
}
}
#[test]
fn single_plugin_builds_once() {
let recorder = Arc::new(Recorder::new());
let builder = crate::app::app().plugin(RecordingPlugin {
label: "only",
recorder: recorder.clone(),
});
assert_eq!(recorder.events(), vec!["only"]);
assert!(builder.has_plugin("only"));
}
#[test]
fn duplicate_named_plugin_is_skipped_with_warning() {
let recorder = Arc::new(Recorder::new());
let builder = crate::app::app()
.plugin(RecordingPlugin {
label: "dup",
recorder: recorder.clone(),
})
.plugin(RecordingPlugin {
label: "dup",
recorder: recorder.clone(),
});
assert_eq!(recorder.events(), vec!["dup"]);
assert!(builder.has_plugin("dup"));
}
#[test]
fn single_plugin_applied_via_plugins_trait() {
let recorder = Arc::new(Recorder::new());
let builder = crate::app::app().plugins(RecordingPlugin {
label: "single_via_trait",
recorder: recorder.clone(),
});
assert_eq!(recorder.events(), vec!["single_via_trait"]);
assert!(builder.has_plugin("single_via_trait"));
}
#[test]
fn tuple_of_plugins_applies_in_declaration_order() {
let recorder = Arc::new(Recorder::new());
let _builder = crate::app::app().plugins((
ColaPlugin {
recorder: recorder.clone(),
},
PepsiPlugin {
recorder: recorder.clone(),
},
));
assert_eq!(recorder.events(), vec!["cola", "pepsi"]);
}
}