Skip to content

Commit

Permalink
fix: clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
mtt committed Sep 4, 2023
1 parent f05fc9f commit b0c8218
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 25 deletions.
12 changes: 2 additions & 10 deletions crates/router/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ impl Routes {
self.routes.iter()
}

pub fn iter(&self) -> impl Iterator<Item = &Route> {
self.routes.iter()
}

/// Provides the **first** route that can handle the given path.
/// This only works because the routes are already sorted.
/// Because a '/a/b' route may be served by:
Expand All @@ -71,7 +67,7 @@ impl Routes {
/// - /[id]/[other].wasm
/// - /[id]/[..all].wasm
pub fn retrieve_best_route<'a>(&'a self, path: &str) -> Option<&'a Route> {
self.routes.iter().find(|r| r.can_manage(path))
self.iter().find(|r| r.can_manage(path))
}

/// Defines a prefix in the context of the application.
Expand Down Expand Up @@ -129,11 +125,7 @@ mod tests {

sorted_router.routes.sort();

let router_paths: Vec<&String> = router.routes.iter().map(|r| &r.path).collect();
let sorted_router_paths: Vec<&String> =
sorted_router.routes.iter().map(|r| &r.path).collect();

assert_eq!(router_paths, sorted_router_paths);
assert_eq!(router.routes, sorted_router.routes);
}

#[test]
Expand Down
16 changes: 8 additions & 8 deletions crates/router/src/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use lazy_static::lazy_static;
use regex::Regex;
use route_type::{
RouteType,
RouteType::{Dynamic, Satic, Tail},
RouteType::{Dynamic, Static, Tail},
};
use segment::Segment;
use std::{
Expand Down Expand Up @@ -37,7 +37,7 @@ lazy_static! {
/// api/index.wasm => /api
/// api/v2/ping.wasm => /api/v2/ping
/// ```
#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct Route {
/// The wasm module that will manage the route
pub handler: PathBuf,
Expand Down Expand Up @@ -160,7 +160,7 @@ impl Route {
let path_number_of_segments = path.chars().filter(|&c| c == '/').count();

match self.route_type {
Satic {
Static {
number_of_segments: _,
} => self.path == path,
Dynamic { number_of_segments } if number_of_segments != path_number_of_segments => {
Expand Down Expand Up @@ -196,7 +196,7 @@ impl Route {
/// Check if the current route is dynamic
pub fn is_dynamic(&self) -> bool {
match self.route_type {
Satic { .. } => false,
Static { .. } => false,
Dynamic { .. } => true,
Tail { .. } => true,
}
Expand All @@ -207,15 +207,15 @@ impl Ord for Route {
fn cmp(&self, other: &Self) -> Ordering {
match (&self.route_type, &other.route_type) {
(
Satic {
Static {
number_of_segments: a,
},
Satic {
Static {
number_of_segments: b,
},
) => a.cmp(b),
(Satic { .. }, _) => Less,
(_, Satic { .. }) => Greater,
(Static { .. }, _) => Less,
(_, Static { .. }) => Greater,
(
Dynamic {
number_of_segments: a,
Expand Down
12 changes: 6 additions & 6 deletions crates/router/src/route/route_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

/// Represents the type of a route.
///
/// - `Satic`: Represents a static route with a fixed number of segments.
/// - `Static`: Represents a static route with a fixed number of segments.
/// - `Dynamic`: Represents a dynamic route with a fixed number of segments.
/// - `Tail`: Represents a tail route with a variable number of segments. It may also contain dynamic segments.
#[derive(PartialEq, Eq, Debug)]
#[derive(PartialEq, Eq, Debug, Clone)]
pub enum RouteType {
Satic { number_of_segments: usize },
Static { number_of_segments: usize },
Dynamic { number_of_segments: usize },
Tail { number_of_segments: usize },
}
Expand All @@ -20,7 +20,7 @@ impl From<&String> for RouteType {
} else if route_path.contains("/[") {
RouteType::Dynamic { number_of_segments }
} else {
RouteType::Satic { number_of_segments }
RouteType::Static { number_of_segments }
}
}
}
Expand All @@ -36,7 +36,7 @@ mod tests {

assert_eq!(
route_type,
RouteType::Satic {
RouteType::Static {
number_of_segments: 1
}
);
Expand Down Expand Up @@ -76,7 +76,7 @@ mod tests {
// Default to static if the format is not recognized
assert_eq!(
route_type,
RouteType::Satic {
RouteType::Static {
number_of_segments: 1
}
);
Expand Down
2 changes: 1 addition & 1 deletion crates/router/src/route/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use std::cmp::Ordering;

#[derive(PartialEq, Eq, PartialOrd, Debug)]
#[derive(PartialEq, Eq, PartialOrd, Debug, Clone)]
pub enum Segment {
/// A static segment in the URL path.
/// Static segments are fixed and don't contain any parameter.
Expand Down

0 comments on commit b0c8218

Please sign in to comment.