-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Cleanup code by creating
NestedRoutes
- Loading branch information
1 parent
faf4ebc
commit aaf5122
Showing
11 changed files
with
89 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// prose-pod-api | ||
// | ||
// Copyright: 2023, Rémi Bardon <[email protected]> | ||
// License: Mozilla Public License v2.0 (MPL v2.0) | ||
|
||
use rocket::{Route, Rocket, Build}; | ||
|
||
type PrefixedRoutes = Vec<(String, Vec<Route>)>; | ||
|
||
pub struct NestedRoutes { | ||
inner: PrefixedRoutes, | ||
} | ||
|
||
impl NestedRoutes { | ||
pub fn new() -> Self { | ||
Self { inner: vec![] } | ||
} | ||
pub fn nesting(self, prefix: &'static str, other: NestedRoutes) -> Self { | ||
let mut inner: PrefixedRoutes = self.inner; | ||
inner.extend(other.prefixed(prefix).inner); | ||
Self { inner } | ||
} | ||
pub fn prefixed(self, prefix: &'static str) -> Self { | ||
let inner: PrefixedRoutes = self.inner | ||
.into_iter() | ||
.map(|(k, v)| (format!("{}/{}", prefix, k), v)) | ||
.collect(); | ||
Self { inner } | ||
} | ||
pub fn mount(self, rocket: Rocket<Build>) -> Rocket<Build> { | ||
self.inner.into_iter() | ||
.fold(rocket, |acc, (base, routes)| acc.mount(base, routes)) | ||
} | ||
} | ||
|
||
impl From<Vec<Route>> for NestedRoutes { | ||
fn from(routes: Vec<Route>) -> Self { | ||
Self { inner: vec![("".to_string(), routes)] } | ||
} | ||
} | ||
|
||
impl From<Vec<(&'static str, NestedRoutes)>> for NestedRoutes { | ||
fn from(vec: Vec<(&'static str, NestedRoutes)>) -> Self { | ||
let mut inner: PrefixedRoutes = vec![]; | ||
for (p, k) in vec { | ||
inner.extend(k.prefixed(p).inner); | ||
} | ||
Self { inner } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,8 @@ | |
// Copyright: 2023, Rémi Bardon <[email protected]> | ||
// License: Mozilla Public License v2.0 (MPL v2.0) | ||
|
||
use rocket::Route; | ||
use crate::helpers::NestedRoutes; | ||
|
||
pub fn routes() -> Vec<(String, Vec<Route>)> { | ||
vec![ | ||
] | ||
pub fn routes() -> NestedRoutes { | ||
NestedRoutes::new() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,8 @@ | |
// Copyright: 2023, Rémi Bardon <[email protected]> | ||
// License: Mozilla Public License v2.0 (MPL v2.0) | ||
|
||
use rocket::Route; | ||
use crate::helpers::NestedRoutes; | ||
|
||
pub fn routes() -> Vec<(String, Vec<Route>)> { | ||
vec![ | ||
] | ||
pub fn routes() -> NestedRoutes { | ||
NestedRoutes::new() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,8 @@ | |
// Copyright: 2023, Rémi Bardon <[email protected]> | ||
// License: Mozilla Public License v2.0 (MPL v2.0) | ||
|
||
use rocket::Route; | ||
use crate::helpers::NestedRoutes; | ||
|
||
pub fn routes() -> Vec<(String, Vec<Route>)> { | ||
vec![ | ||
] | ||
pub fn routes() -> NestedRoutes { | ||
NestedRoutes::new() | ||
} |