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
//! Bundle (and other) route loaders.
use crate::{HttpError, RouteCollection};
/// Loads routes into a [`RouteCollection`].
///
/// Bundles implement this to contribute routes during kernel compile/boot.
pub trait RouteLoader {
/// Appends routes to `collection`.
///
/// # Errors
///
/// Returns [`HttpError`] when registration fails (duplicate name, invalid path, …).
fn load(&self, collection: &mut RouteCollection) -> Result<(), HttpError>;
}
impl<F> RouteLoader for F
where
F: Fn(&mut RouteCollection) -> Result<(), HttpError>,
{
fn load(&self, collection: &mut RouteCollection) -> Result<(), HttpError> {
self(collection)
}
}
/// Runs every loader in order into a fresh collection.
///
/// # Errors
///
/// Returns the first loader error.
pub fn load_routes(loaders: &[&dyn RouteLoader]) -> Result<RouteCollection, HttpError> {
let mut collection = RouteCollection::new();
for loader in loaders {
loader.load(&mut collection)?;
}
Ok(collection)
}