Skip to content

Commit

Permalink
Add method router.
Browse files Browse the repository at this point in the history
  • Loading branch information
CathalMullan committed Nov 17, 2024
1 parent 5bab4c8 commit a97f192
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 12 deletions.
2 changes: 2 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ pub use search::SearchError;
pub use crate::routers::path::errors::{
PathConstraintError, PathDeleteError, PathInsertError, PathRouteError, PathSearchError,
};

pub use crate::routers::method::errors::{MethodDeleteError, MethodInsertError, MethodSearchError};
10 changes: 9 additions & 1 deletion src/errors/delete.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use super::PathDeleteError;
use crate::routers::method::errors::MethodDeleteError;
use core::{error::Error, fmt::Display};

/// Errors relating to attempting to delete a route from a [`Router`](crate::Router).
#[derive(Debug, PartialEq, Eq)]
pub enum DeleteError {
/// A [`PathDeleteError`] occurred.
PathDeleteError(PathDeleteError),
MethodDeleteError(MethodDeleteError),
}

impl Error for DeleteError {}
Expand All @@ -14,6 +15,7 @@ impl Display for DeleteError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::PathDeleteError(error) => error.fmt(f),
Self::MethodDeleteError(error) => error.fmt(f),
}
}
}
Expand All @@ -23,3 +25,9 @@ impl From<PathDeleteError> for DeleteError {
Self::PathDeleteError(error)
}
}

impl From<MethodDeleteError> for DeleteError {
fn from(error: MethodDeleteError) -> Self {
Self::MethodDeleteError(error)
}
}
11 changes: 9 additions & 2 deletions src/errors/insert.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::routers::path::errors::PathInsertError;
use crate::routers::{method::errors::MethodInsertError, path::errors::PathInsertError};
use core::{error::Error, fmt::Display};

/// Errors relating to attempting to insert a route into a [`Router`](crate::Router).
#[derive(Debug, PartialEq, Eq)]
pub enum InsertError {
/// A [`PathInsertError`] occurred.
PathInsertError(PathInsertError),
MethodInsertError(MethodInsertError),
}

impl Error for InsertError {}
Expand All @@ -14,6 +14,7 @@ impl Display for InsertError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::PathInsertError(error) => error.fmt(f),
Self::MethodInsertError(error) => error.fmt(f),
}
}
}
Expand All @@ -23,3 +24,9 @@ impl From<PathInsertError> for InsertError {
Self::PathInsertError(error)
}
}

impl From<MethodInsertError> for InsertError {
fn from(error: MethodInsertError) -> Self {
Self::MethodInsertError(error)
}
}
10 changes: 9 additions & 1 deletion src/errors/search.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use super::PathSearchError;
use crate::routers::method::errors::MethodSearchError;
use core::{error::Error, fmt::Display};

/// Errors relating to attempting to search for a match in a [`Router`](crate::Router).
#[derive(Debug, PartialEq, Eq)]
pub enum SearchError {
/// A [`PathSearchError`] occurred.
PathSearchError(PathSearchError),
MethodSearchError(MethodSearchError),
}

impl Error for SearchError {}
Expand All @@ -14,6 +15,7 @@ impl Display for SearchError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::PathSearchError(error) => error.fmt(f),
Self::MethodSearchError(error) => error.fmt(f),
}
}
}
Expand All @@ -23,3 +25,9 @@ impl From<PathSearchError> for SearchError {
Self::PathSearchError(error)
}
}

impl From<MethodSearchError> for SearchError {
fn from(error: MethodSearchError) -> Self {
Self::MethodSearchError(error)
}
}
20 changes: 18 additions & 2 deletions src/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,31 @@ use crate::{decode::percent_decode, errors::RouteError};
use alloc::{
borrow::ToOwned,
string::{String, ToString},
vec::Vec,
};
use http::Method;

/// A route that can be inserted into a [`Router`](`crate::Router`).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Route<'r> {
pub(crate) route: &'r str,
pub(crate) methods: Option<Vec<Method>>,
}

/// Builder pattern for creating a [`Route`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RouteBuilder<'r> {
route: Option<&'r str>,
methods: Option<Vec<Method>>,
}

impl<'r> RouteBuilder<'r> {
#[must_use]
pub const fn new() -> Self {
Self { route: None }
Self {
route: None,
methods: None,
}
}

#[must_use]
Expand All @@ -28,6 +35,12 @@ impl<'r> RouteBuilder<'r> {
self
}

#[must_use]
pub fn methods(mut self, methods: Vec<Method>) -> Self {
self.methods = Some(methods);
self
}

/// Builds a new [`Route`] instance from the builder.
///
/// # Errors
Expand All @@ -44,7 +57,10 @@ impl<'r> RouteBuilder<'r> {
})?;
}

Ok(Route { route })
Ok(Route {
route,
methods: self.methods,
})
}
}

Expand Down
18 changes: 14 additions & 4 deletions src/routers.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
use crate::{
errors::{DeleteError, InsertError, SearchError},
id::RouteId,
map::RouteMap,
Request, Route,
};
use path::{PathMatch, PathRouter};
use hashbrown::HashMap;
use method::MethodRouter;
use path::{PathData, PathMatch, PathRouter};

pub mod method;
pub mod path;

pub type Match<'r, 'p, T> = PathMatch<'r, 'p, T>;

#[derive(Clone)]
pub struct Router<'r, T> {
pub path: PathRouter<'r>,
data: RouteMap<T>,
pub method: MethodRouter<T>,

/// Stored data.
data: HashMap<RouteId, T>,
}

impl<'r, T> Router<'r, T> {
#[must_use]
pub fn new() -> Self {
Self {
path: PathRouter::new(),
data: RouteMap::default(),
method: MethodRouter::new(),

data: HashMap::new(),
}
}

Expand All @@ -30,6 +37,8 @@ impl<'r, T> Router<'r, T> {
let id = RouteId::new();

self.path.insert(route, id)?;
self.method.insert(route, id)?;

self.data.insert(id, value);

Ok(())
Expand All @@ -38,6 +47,7 @@ impl<'r, T> Router<'r, T> {
#[allow(clippy::missing_errors_doc)]
pub fn delete(&mut self, route: &Route<'r>) -> Result<(), DeleteError> {
let path_data = self.path.delete(route)?;
let method_delete = self.method.delete(route)?;

let path_id = path_data.id;
self.data.remove(&path_id);
Expand Down
36 changes: 36 additions & 0 deletions src/routers/method.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use crate::Route;
use errors::{MethodDeleteError, MethodInsertError, MethodSearchError};
use http::Method;

pub mod errors;

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum MethodData<T> {}

#[derive(Debug, Eq, PartialEq)]
pub struct PathMatch<T> {}

#[derive(Clone)]
pub struct MethodRouter<T> {}

impl<T> MethodRouter<T> {
pub fn new() -> Self {
Self {}
}

pub fn insert<'r>(
&mut self,
route: &Route<'r>,
value: Option<T>,
) -> Result<(), MethodInsertError> {
Ok(())
}

pub fn delete<'r>(&mut self, route: &Route<'r>) -> Result<MethodData<T>, MethodDeleteError> {
Ok(())
}

pub fn search(&self, method: Method) -> Result<Option<MethodMatch<T>>, MethodSearchError> {
Ok(None)
}
}
8 changes: 8 additions & 0 deletions src/routers/method/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pub mod delete;
pub use delete::MethodDeleteError;

pub mod insert;
pub use insert::MethodInsertError;

pub mod search;
pub use search::MethodSearchError;
12 changes: 12 additions & 0 deletions src/routers/method/errors/delete.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use core::{error::Error, fmt::Display};

#[derive(Debug, PartialEq, Eq)]
pub enum MethodDeleteError {}

impl Error for MethodDeleteError {}

impl Display for MethodDeleteError {
fn fmt(&self, _: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
Ok(())
}
}
12 changes: 12 additions & 0 deletions src/routers/method/errors/insert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use core::{error::Error, fmt::Display};

#[derive(Debug, PartialEq, Eq)]
pub enum MethodInsertError {}

impl Error for MethodInsertError {}

impl Display for MethodInsertError {
fn fmt(&self, _: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
Ok(())
}
}
12 changes: 12 additions & 0 deletions src/routers/method/errors/search.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use core::{error::Error, fmt::Display};

#[derive(Debug, PartialEq, Eq)]
pub enum MethodSearchError {}

impl Error for MethodSearchError {}

impl Display for MethodSearchError {
fn fmt(&self, _: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
Ok(())
}
}
3 changes: 1 addition & 2 deletions src/routers/path/errors/route.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use alloc::string::String;

use crate::errors::EncodingError;
use alloc::string::String;

/// Errors relating to malformed routes.
#[derive(Debug, PartialEq, Eq)]
Expand Down

0 comments on commit a97f192

Please sign in to comment.