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 03ef042
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 144 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
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
Loading

0 comments on commit 03ef042

Please sign in to comment.