1use axum::Router;
4
5use crate::error::RoutePathError;
6use crate::router::{RouteDefinition, join_paths};
7
8pub struct Controller {
10 prefix: String,
11 routes: Vec<RouteDefinition>,
12}
13
14impl Controller {
15 pub fn new(prefix: impl Into<String>) -> Self {
17 Self {
18 prefix: prefix.into(),
19 routes: Vec::new(),
20 }
21 }
22
23 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 pub fn route(mut self, route: RouteDefinition) -> Self {
34 self.routes.push(route);
35 self
36 }
37
38 pub fn into_router(self) -> Router {
40 self.try_into_router()
41 .unwrap_or_else(|error| panic!("{error}"))
42 }
43
44 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}