use crate::router::Router;
pub trait RouteDefinition {
fn register(router: &mut Router);
}
pub struct RouteGroup {
prefix: String,
routes: Vec<RouteEntry>,
}
#[derive(Clone)]
pub struct RouteEntry {
pub method: String,
pub path: String,
pub name: Option<String>,
}
impl RouteGroup {
pub fn new(prefix: &str) -> Self {
Self {
prefix: prefix.to_string(),
routes: Vec::new(),
}
}
pub fn get(mut self, path: &str) -> Self {
self.routes.push(RouteEntry {
method: "GET".to_string(),
path: format!("{}{}", self.prefix, path),
name: None,
});
self
}
pub fn post(mut self, path: &str) -> Self {
self.routes.push(RouteEntry {
method: "POST".to_string(),
path: format!("{}{}", self.prefix, path),
name: None,
});
self
}
pub fn put(mut self, path: &str) -> Self {
self.routes.push(RouteEntry {
method: "PUT".to_string(),
path: format!("{}{}", self.prefix, path),
name: None,
});
self
}
pub fn delete(mut self, path: &str) -> Self {
self.routes.push(RouteEntry {
method: "DELETE".to_string(),
path: format!("{}{}", self.prefix, path),
name: None,
});
self
}
pub fn routes(&self) -> &[RouteEntry] {
&self.routes
}
pub fn prefix(&self) -> &str {
&self.prefix
}
}
pub struct ApiVersion {
pub version: String,
pub prefix: String,
}
impl ApiVersion {
pub fn new(version: u32) -> Self {
Self {
version: format!("v{}", version),
prefix: format!("/api/v{}", version),
}
}
pub fn prefix(&self) -> &str {
&self.prefix
}
}
pub fn resource_routes(name: &str) -> RouteGroup {
RouteGroup::new(&format!("/{}", name))
.get("") .get("/:id") .post("") .put("/:id") .delete("/:id") }