From 076033561fa15b1840597586be3468d42fc215ad Mon Sep 17 00:00:00 2001 From: Cathal Mullan Date: Mon, 26 Aug 2024 21:01:29 +0100 Subject: [PATCH] Refactor crate API --- benches/matchit_criterion.rs | 4 ++-- benches/matchit_divan.rs | 4 ++-- benches/path_tree_criterion.rs | 4 ++-- benches/path_tree_divan.rs | 4 ++-- examples/axum-fork/src/routing/path_router.rs | 2 +- examples/axum-fork/src/routing/url_params.rs | 2 +- examples/hyper/src/main.rs | 2 +- flake.nix | 1 + fuzz/fuzz_targets/insert.rs | 2 +- src/decode.rs | 2 +- src/errors.rs | 19 +++++++++++---- src/lib.rs | 24 +++++++++++++------ tests/common.rs | 8 ++----- tests/constraints.rs | 2 +- tests/encoding.rs | 2 +- tests/matchit_delete.rs | 2 +- tests/matchit_matches.rs | 2 +- tests/path_tree.rs | 2 +- tests/poem.rs | 2 +- tests/uncommon.rs | 2 +- 20 files changed, 54 insertions(+), 38 deletions(-) diff --git a/benches/matchit_criterion.rs b/benches/matchit_criterion.rs index 8faf0017..3c84935e 100644 --- a/benches/matchit_criterion.rs +++ b/benches/matchit_criterion.rs @@ -17,14 +17,14 @@ fn benchmark(criterion: &mut Criterion) { let mut group = criterion.benchmark_group("matchit benchmarks"); group.bench_function("matchit benchmarks/wayfind", |bencher| { - let mut router = wayfind::router::Router::new(); + let mut router = wayfind::Router::new(); for route in routes!(brackets) { router.insert(route, true).unwrap(); } bencher.iter(|| { for route in black_box(paths()) { - let path = wayfind::path::Path::new(route).unwrap(); + let path = wayfind::Path::new(route).unwrap(); let output = black_box(router.search(black_box(&path)).unwrap()); let _parameters: Vec<(&str, &str)> = black_box(output.parameters.iter().map(|p| (p.key, p.value)).collect()); diff --git a/benches/matchit_divan.rs b/benches/matchit_divan.rs index 62aee169..cf555092 100644 --- a/benches/matchit_divan.rs +++ b/benches/matchit_divan.rs @@ -19,14 +19,14 @@ fn main() { #[divan::bench(name = "wayfind")] fn wayfind(bencher: divan::Bencher) { - let mut router = wayfind::router::Router::new(); + let mut router = wayfind::Router::new(); for route in routes!(brackets) { router.insert(route, true).unwrap(); } bencher.bench(|| { for route in black_box(paths()) { - let path = wayfind::path::Path::new(route).unwrap(); + let path = wayfind::Path::new(route).unwrap(); let output = black_box(router.search(black_box(&path)).unwrap()); let _parameters: Vec<(&str, &str)> = black_box(output.parameters.iter().map(|p| (p.key, p.value)).collect()); diff --git a/benches/path_tree_criterion.rs b/benches/path_tree_criterion.rs index 26427e04..9ad559de 100644 --- a/benches/path_tree_criterion.rs +++ b/benches/path_tree_criterion.rs @@ -17,14 +17,14 @@ fn benchmark(criterion: &mut Criterion) { let mut group = criterion.benchmark_group("path-tree benchmarks"); group.bench_function("path-tree benchmarks/wayfind", |bencher| { - let mut router = wayfind::router::Router::new(); + let mut router = wayfind::Router::new(); for (index, route) in routes!(brackets).iter().enumerate() { router.insert(route, index).unwrap(); } bencher.iter(|| { for route in black_box(paths()) { - let path = wayfind::path::Path::new(route).unwrap(); + let path = wayfind::Path::new(route).unwrap(); let output = black_box(router.search(black_box(&path)).unwrap()); let _parameters: Vec<(&str, &str)> = black_box(output.parameters.iter().map(|p| (p.key, p.value)).collect()); diff --git a/benches/path_tree_divan.rs b/benches/path_tree_divan.rs index c1829681..0a077b91 100644 --- a/benches/path_tree_divan.rs +++ b/benches/path_tree_divan.rs @@ -19,14 +19,14 @@ fn main() { #[divan::bench(name = "wayfind")] fn wayfind(bencher: divan::Bencher) { - let mut router = wayfind::router::Router::new(); + let mut router = wayfind::Router::new(); for (index, route) in routes!(brackets).iter().enumerate() { router.insert(route, index).unwrap(); } bencher.bench(|| { for route in black_box(paths()) { - let path = wayfind::path::Path::new(route).unwrap(); + let path = wayfind::Path::new(route).unwrap(); let output = black_box(router.search(black_box(&path)).unwrap()); let _parameters: Vec<(&str, &str)> = black_box(output.parameters.iter().map(|p| (p.key, p.value)).collect()); diff --git a/examples/axum-fork/src/routing/path_router.rs b/examples/axum-fork/src/routing/path_router.rs index e3554b77..8fee6bfd 100644 --- a/examples/axum-fork/src/routing/path_router.rs +++ b/examples/axum-fork/src/routing/path_router.rs @@ -3,7 +3,7 @@ use axum_core::response::IntoResponse; use std::{borrow::Cow, collections::HashMap, convert::Infallible, fmt, sync::Arc}; use tower_layer::Layer; use tower_service::Service; -use wayfind::{errors::insert::InsertError, node::search::Match, path::Path, router::Router}; +use wayfind::{errors::InsertError, Match, Path, Router}; use super::{ future::RouteFuture, not_found::NotFound, strip_prefix::StripPrefix, url_params, Endpoint, diff --git a/examples/axum-fork/src/routing/url_params.rs b/examples/axum-fork/src/routing/url_params.rs index 2ea418a2..5d61221c 100644 --- a/examples/axum-fork/src/routing/url_params.rs +++ b/examples/axum-fork/src/routing/url_params.rs @@ -1,7 +1,7 @@ use crate::util::PercentDecodedStr; use http::Extensions; use std::sync::Arc; -use wayfind::node::search::Parameter; +use wayfind::Parameter; #[derive(Clone)] pub(crate) enum UrlParams { diff --git a/examples/hyper/src/main.rs b/examples/hyper/src/main.rs index cb28876b..b73ed125 100644 --- a/examples/hyper/src/main.rs +++ b/examples/hyper/src/main.rs @@ -15,7 +15,7 @@ use std::{ sync::Arc, }; use tokio::{net::TcpListener, task::JoinSet}; -use wayfind::{node::search::Parameter, path::Path, router::Router}; +use wayfind::{Parameter, Path, Router}; type BoxFuture<'a> = Pin< Box< diff --git a/flake.nix b/flake.nix index 17659def..d82f8eba 100644 --- a/flake.nix +++ b/flake.nix @@ -61,6 +61,7 @@ }) sccache cargo-insta + cargo-watch # Benchmarking cargo-codspeed diff --git a/fuzz/fuzz_targets/insert.rs b/fuzz/fuzz_targets/insert.rs index 6739a686..d8f259eb 100644 --- a/fuzz/fuzz_targets/insert.rs +++ b/fuzz/fuzz_targets/insert.rs @@ -3,7 +3,7 @@ use libfuzzer_sys::fuzz_target; fuzz_target!(|data: &[u8]| { - let mut router = wayfind::router::Router::new(); + let mut router = wayfind::Router::new(); if let Ok(route) = std::str::from_utf8(data) { let _ = router.insert(route, true); } diff --git a/src/decode.rs b/src/decode.rs index 095d7a7c..9c3e97b4 100644 --- a/src/decode.rs +++ b/src/decode.rs @@ -1,7 +1,7 @@ use crate::errors::decode::DecodeError; use std::borrow::Cow; -pub(crate) fn percent_decode(input: &[u8]) -> Result, DecodeError> { +pub fn percent_decode(input: &[u8]) -> Result, DecodeError> { if !input.contains(&b'%') { return Ok(Cow::Borrowed(input)); } diff --git a/src/errors.rs b/src/errors.rs index 8f150938..b801843d 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,5 +1,14 @@ -pub mod constraint; -pub mod decode; -pub mod delete; -pub mod insert; -pub mod route; +pub(crate) mod constraint; +pub use constraint::ConstraintError; + +pub(crate) mod decode; +pub use decode::DecodeError; + +pub(crate) mod delete; +pub use delete::DeleteError; + +pub(crate) mod insert; +pub use insert::InsertError; + +pub(crate) mod route; +pub use route::RouteError; diff --git a/src/lib.rs b/src/lib.rs index cf3e8551..4c500201 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,9 +1,19 @@ -//! TODO +//! Hello world! + +pub(crate) mod constraints; +pub use constraints::Constraint; + +pub(crate) mod decode; -pub mod constraints; -pub mod decode; pub mod errors; -pub mod node; -pub mod parts; -pub mod path; -pub mod router; + +pub(crate) mod node; +pub use node::search::{Match, Parameter}; + +pub(crate) mod parts; + +pub(crate) mod path; +pub use path::Path; + +pub(crate) mod router; +pub use router::Router; diff --git a/tests/common.rs b/tests/common.rs index 87eb177b..e42dd788 100644 --- a/tests/common.rs +++ b/tests/common.rs @@ -1,9 +1,5 @@ use std::{fmt::Debug, sync::Arc}; -use wayfind::{ - node::search::{Match, Parameter}, - path::Path, - router::Router, -}; +use wayfind::{Match, Parameter, Path, Router}; #[macro_export] macro_rules! assert_router_matches { @@ -28,7 +24,7 @@ macro_rules! assert_router_matches { value: $value, params: vec![ $( - $( wayfind::node::search::Parameter { + $( wayfind::Parameter { key: $param_key, value: $param_value, } ),+ diff --git a/tests/constraints.rs b/tests/constraints.rs index 3a3d972a..70322ca3 100644 --- a/tests/constraints.rs +++ b/tests/constraints.rs @@ -1,7 +1,7 @@ #![allow(clippy::too_many_lines)] use std::error::Error; -use wayfind::{constraints::Constraint, router::Router}; +use wayfind::{Constraint, Router}; #[path = "./common.rs"] mod common; diff --git a/tests/encoding.rs b/tests/encoding.rs index 4d6c8f67..4341678d 100644 --- a/tests/encoding.rs +++ b/tests/encoding.rs @@ -1,5 +1,5 @@ use std::error::Error; -use wayfind::router::Router; +use wayfind::Router; #[path = "./common.rs"] mod common; diff --git a/tests/matchit_delete.rs b/tests/matchit_delete.rs index cf797b5a..2304e2aa 100644 --- a/tests/matchit_delete.rs +++ b/tests/matchit_delete.rs @@ -4,7 +4,7 @@ #![allow(clippy::too_many_lines, clippy::cognitive_complexity)] use std::error::Error; -use wayfind::router::Router; +use wayfind::Router; #[test] fn normalized() -> Result<(), Box> { diff --git a/tests/matchit_matches.rs b/tests/matchit_matches.rs index 24edcf9c..4775091e 100644 --- a/tests/matchit_matches.rs +++ b/tests/matchit_matches.rs @@ -4,7 +4,7 @@ #![allow(clippy::too_many_lines)] use std::error::Error; -use wayfind::router::Router; +use wayfind::Router; #[path = "./common.rs"] mod common; diff --git a/tests/path_tree.rs b/tests/path_tree.rs index 92595ef7..63a4f9a1 100644 --- a/tests/path_tree.rs +++ b/tests/path_tree.rs @@ -4,7 +4,7 @@ #![allow(clippy::too_many_lines)] use std::error::Error; -use wayfind::router::Router; +use wayfind::Router; #[path = "./common.rs"] mod common; diff --git a/tests/poem.rs b/tests/poem.rs index 45549ea0..feed337d 100644 --- a/tests/poem.rs +++ b/tests/poem.rs @@ -4,7 +4,7 @@ #![allow(clippy::too_many_lines)] use std::error::Error; -use wayfind::{constraints::Constraint, router::Router}; +use wayfind::{Constraint, Router}; #[path = "./common.rs"] mod common; diff --git a/tests/uncommon.rs b/tests/uncommon.rs index aa3bcad0..28898faa 100644 --- a/tests/uncommon.rs +++ b/tests/uncommon.rs @@ -1,7 +1,7 @@ #![allow(clippy::too_many_lines)] use std::error::Error; -use wayfind::router::Router; +use wayfind::Router; #[path = "./common.rs"] mod common;