Skip to main content

nidus_http/
controller.rs

1//! Controller metadata.
2
3use axum::Router;
4
5use crate::error::RoutePathError;
6use crate::router::{RouteDefinition, join_paths};
7
8/// Controller route group with a shared path prefix.
9pub struct Controller {
10    prefix: String,
11    routes: Vec<RouteDefinition>,
12}
13
14impl Controller {
15    /// Creates an empty controller route group.
16    pub fn new(prefix: impl Into<String>) -> Self {
17        Self {
18            prefix: prefix.into(),
19            routes: Vec::new(),
20        }
21    }
22
23    /// Tries to create an empty controller route group with a validated prefix.
24    pub fn try_new(prefix: impl Into<String>) -> Result<Self, RoutePathError> {
25        let prefix = prefix.into();
26        join_paths(&prefix, "/").map(|prefix| Self {
27            prefix,
28            routes: Vec::new(),
29        })
30    }
31
32    /// Adds a route to this controller.
33    pub fn route(mut self, route: RouteDefinition) -> Self {
34        self.routes.push(route);
35        self
36    }
37
38    /// Builds an Axum router from the controller routes.
39    pub fn into_router(self) -> Router {
40        self.try_into_router()
41            .unwrap_or_else(|error| panic!("{error}"))
42    }
43
44    /// Tries to build an Axum router from the controller routes.
45    pub fn try_into_router(self) -> Result<Router, RoutePathError> {
46        let mut router = Router::new();
47        for route in self.routes {
48            let full_path = join_paths(&self.prefix, route.path())?;
49            router = router.merge(route.into_router(full_path));
50        }
51        Ok(router)
52    }
53}