Skip to content

Commit

Permalink
wip: polish
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioBenitez committed Aug 24, 2024
1 parent 48703da commit ea17efa
Show file tree
Hide file tree
Showing 14 changed files with 170 additions and 159 deletions.
1 change: 1 addition & 0 deletions core/codegen/src/attribute/route/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub struct Arguments {
/// The parsed `#[route(..)]` attribute.
#[derive(Debug, FromMeta)]
pub struct Attribute {
#[meta(naked)]
pub uri: RouteUri,
pub method: Option<SpanWrapped<Method>>,
pub data: Option<SpanWrapped<Dynamic>>,
Expand Down
18 changes: 13 additions & 5 deletions core/codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,18 @@ macro_rules! route_attribute {
/// * [`patch`] - `PATCH` specific route
///
/// Additionally, [`route`] allows the method and uri to be explicitly
/// specified:
/// specified, and for the method to be omitted entirely, matching any
/// method:
///
/// ```rust
/// # #[macro_use] extern crate rocket;
/// #
/// #[route(GET, uri = "/")]
/// #[route("/", method = GET)]
/// fn get_index() -> &'static str {
/// "Hello, world!"
/// }
///
/// #[route("/")]
/// fn index() -> &'static str {
/// "Hello, world!"
/// }
Expand Down Expand Up @@ -171,7 +177,9 @@ macro_rules! route_attribute {
/// The generic route attribute is defined as:
///
/// ```text
/// generic-route := METHOD ',' 'uri' '=' route
/// generic-route := route (',' method)?
///
/// method := 'method' '=' METHOD
/// ```
///
/// # Typing Requirements
Expand Down Expand Up @@ -1161,12 +1169,12 @@ pub fn derive_uri_display_path(input: TokenStream) -> TokenStream {
/// assert_eq!(my_routes.len(), 2);
///
/// let index_route = &my_routes[0];
/// assert_eq!(index_route.method, Method::Get);
/// assert_eq!(index_route.method, Some(Method::Get));
/// assert_eq!(index_route.name.as_ref().unwrap(), "index");
/// assert_eq!(index_route.uri.path(), "/");
///
/// let hello_route = &my_routes[1];
/// assert_eq!(hello_route.method, Method::Post);
/// assert_eq!(hello_route.method, Some(Method::Post));
/// assert_eq!(hello_route.name.as_ref().unwrap(), "hello");
/// assert_eq!(hello_route.uri.path(), "/hi/<person>");
/// ```
Expand Down
2 changes: 1 addition & 1 deletion core/codegen/tests/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ fn post1(
}

#[route(
"/<a>/<name>/name/<path..>?sky=blue&<sky>&<query..>",
method = POST,
uri = "/<a>/<name>/name/<path..>?sky=blue&<sky>&<query..>",
format = "json",
data = "<simple>",
rank = 138
Expand Down
1 change: 0 additions & 1 deletion core/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ tokio-stream = { version = "0.1.6", features = ["signal", "time"] }
cookie = { version = "0.18", features = ["percent-encode"] }
futures = { version = "0.3.30", default-features = false, features = ["std"] }
state = "0.6"
rustc-hash = "1.1"

# tracing
tracing = { version = "0.1.40", default-features = false, features = ["std", "attributes"] }
Expand Down
15 changes: 6 additions & 9 deletions core/lib/fuzz/targets/collision-matching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ struct ArbitraryRequestData<'a> {

#[derive(Arbitrary)]
struct ArbitraryRouteData<'a> {
method: ArbitraryMethod,
method: Option<ArbitraryMethod>,
uri: ArbitraryRouteUri<'a>,
format: Option<ArbitraryMediaType>,
}

impl std::fmt::Debug for ArbitraryRouteData<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ArbitraryRouteData")
.field("method", &self.method.0)
.field("method", &self.method.map(|v| v.0))
.field("base", &self.uri.0.base())
.field("unmounted", &self.uri.0.unmounted().to_string())
.field("uri", &self.uri.0.to_string())
Expand Down Expand Up @@ -59,12 +59,14 @@ impl<'c, 'a: 'c> ArbitraryRequestData<'a> {

impl<'a> ArbitraryRouteData<'a> {
fn into_route(self) -> Route {
let mut r = Route::ranked(0, self.method.0, &self.uri.0.to_string(), dummy_handler);
let method = self.method.map(|m| m.0);
let mut r = Route::ranked(0, method, &self.uri.0.to_string(), dummy_handler);
r.format = self.format.map(|f| f.0);
r
}
}

#[derive(Clone, Copy)]
struct ArbitraryMethod(Method);

struct ArbitraryOrigin<'a>(Origin<'a>);
Expand All @@ -79,12 +81,7 @@ struct ArbitraryRouteUri<'a>(RouteUri<'a>);

impl<'a> Arbitrary<'a> for ArbitraryMethod {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
let all_methods = &[
Method::Get, Method::Put, Method::Post, Method::Delete, Method::Options,
Method::Head, Method::Trace, Method::Connect, Method::Patch
];

Ok(ArbitraryMethod(*u.choose(all_methods)?))
Ok(ArbitraryMethod(*u.choose(Method::ALL_VARIANTS)?))
}

fn size_hint(_: usize) -> (usize, Option<usize>) {
Expand Down
6 changes: 3 additions & 3 deletions core/lib/src/phase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use figment::Figment;
use crate::listener::Endpoint;
use crate::shutdown::Stages;
use crate::{Catcher, Config, Rocket, Route};
use crate::router::Router;
use crate::router::{Router, Finalized};
use crate::fairing::Fairings;

mod private {
Expand Down Expand Up @@ -100,7 +100,7 @@ phases! {
/// represents a fully built and finalized application server ready for
/// launch into orbit. See [`Rocket#ignite`] for full details.
Ignite (#[derive(Debug)] Igniting) {
pub(crate) router: Router,
pub(crate) router: Router<Finalized>,
pub(crate) fairings: Fairings,
pub(crate) figment: Figment,
pub(crate) config: Config,
Expand All @@ -114,7 +114,7 @@ phases! {
/// An instance of `Rocket` in this phase is typed as [`Rocket<Orbit>`] and
/// represents a running application.
Orbit (#[derive(Debug)] Orbiting) {
pub(crate) router: Router,
pub(crate) router: Router<Finalized>,
pub(crate) fairings: Fairings,
pub(crate) figment: Figment,
pub(crate) config: Config,
Expand Down
15 changes: 8 additions & 7 deletions core/lib/src/rocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,9 +557,10 @@ impl Rocket<Build> {

// Initialize the router; check for collisions.
let mut router = Router::new();
self.routes.clone().into_iter().for_each(|r| router.add_route(r));
self.catchers.clone().into_iter().for_each(|c| router.add_catcher(c));
router.finalize().map_err(|(r, c)| ErrorKind::Collisions { routes: r, catchers: c, })?;
self.routes.clone().into_iter().for_each(|r| router.routes.push(r));
self.catchers.clone().into_iter().for_each(|c| router.catchers.push(c));
let router = router.finalize()
.map_err(|(r, c)| ErrorKind::Collisions { routes: r, catchers: c, })?;

// Finally, freeze managed state for faster access later.
self.state.freeze();
Expand Down Expand Up @@ -840,8 +841,8 @@ impl<P: Phase> Rocket<P> {
pub fn routes(&self) -> impl Iterator<Item = &Route> {
match self.0.as_ref() {
StateRef::Build(p) => Either::Left(p.routes.iter()),
StateRef::Ignite(p) => Either::Right(p.router.routes()),
StateRef::Orbit(p) => Either::Right(p.router.routes()),
StateRef::Ignite(p) => Either::Right(p.router.routes.iter()),
StateRef::Orbit(p) => Either::Right(p.router.routes.iter()),
}
}

Expand Down Expand Up @@ -871,8 +872,8 @@ impl<P: Phase> Rocket<P> {
pub fn catchers(&self) -> impl Iterator<Item = &Catcher> {
match self.0.as_ref() {
StateRef::Build(p) => Either::Left(p.catchers.iter()),
StateRef::Ignite(p) => Either::Right(p.router.catchers()),
StateRef::Orbit(p) => Either::Right(p.router.catchers()),
StateRef::Ignite(p) => Either::Right(p.router.catchers.iter()),
StateRef::Orbit(p) => Either::Right(p.router.catchers.iter()),
}
}

Expand Down
8 changes: 4 additions & 4 deletions core/lib/src/route/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::sentinel::Sentry;
///
/// let route = routes![route_name].remove(0);
/// assert_eq!(route.name.unwrap(), "route_name");
/// assert_eq!(route.method, Method::Get);
/// assert_eq!(route.method, Some(Method::Get));
/// assert_eq!(route.uri, "/route/<path..>?query");
/// assert_eq!(route.rank, 2);
/// assert_eq!(route.format.unwrap(), MediaType::JSON);
Expand Down Expand Up @@ -203,7 +203,7 @@ impl Route {
/// // this is a route matching requests to `GET /`
/// let index = Route::new(Method::Get, "/", handler);
/// assert_eq!(index.rank, -9);
/// assert_eq!(index.method, Method::Get);
/// assert_eq!(index.method, Some(Method::Get));
/// assert_eq!(index.uri, "/");
/// ```
#[track_caller]
Expand Down Expand Up @@ -233,12 +233,12 @@ impl Route {
///
/// let foo = Route::ranked(1, Method::Post, "/foo?bar", handler);
/// assert_eq!(foo.rank, 1);
/// assert_eq!(foo.method, Method::Post);
/// assert_eq!(foo.method, Some(Method::Post));
/// assert_eq!(foo.uri, "/foo?bar");
///
/// let foo = Route::ranked(None, Method::Post, "/foo?bar", handler);
/// assert_eq!(foo.rank, -12);
/// assert_eq!(foo.method, Method::Post);
/// assert_eq!(foo.method, Some(Method::Post));
/// assert_eq!(foo.uri, "/foo?bar");
/// ```
#[track_caller]
Expand Down
Loading

0 comments on commit ea17efa

Please sign in to comment.