Skip to content

Commit 00da4df

Browse files
committed
Documentation and minor fixes
- Add with_type to catcher - Documents CatcherType, RouteType, and the associated methods in LocalResponse.
1 parent 78cb502 commit 00da4df

File tree

3 files changed

+51
-14
lines changed

3 files changed

+51
-14
lines changed

core/lib/src/catcher/catcher.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@ use crate::catcher::{Handler, BoxFuture};
99

1010
use yansi::Paint;
1111

12-
// We could also choose to require a Debug impl?
13-
/// A generic trait for route types. This should be automatically implemented on the structs
14-
/// generated by the codegen for each route.
12+
/// A generic trait for catcher types. This should be automatically implemented on the structs
13+
/// generated by the codegen for each catcher, and manually implemented on custom cater types.
1514
///
16-
/// It may also be desirable to add an option for other routes to define a RouteType. This
17-
/// would likely just be a case of adding an alternate constructor to the Route type.
15+
/// Use the `Catcher::with_type::<T>()` method to set the catcher type.
1816
pub trait CatcherType: Any + Send + Sync + 'static { }
1917

2018
/// An error catching route.
@@ -165,6 +163,8 @@ impl Catcher {
165163
///
166164
/// Panics if `code` is not in the HTTP status code error range `[400,
167165
/// 600)`.
166+
///
167+
/// If applicable, `with_type` should also be called to set the route type for testing
168168
#[inline(always)]
169169
pub fn new<S, H>(code: S, handler: H) -> Catcher
170170
where S: Into<Option<u16>>, H: Handler
@@ -183,6 +183,13 @@ impl Catcher {
183183
}
184184
}
185185

186+
/// Marks this catcher with the specified type. For a custom catcher type, i.e. something that can
187+
/// be passed to `.register()`, it should be that type to make identification easier.
188+
pub fn with_type<T: CatcherType>(mut self) -> Self {
189+
self.catcher_type = Some(TypeId::of::<T>());
190+
self
191+
}
192+
186193
/// Maps the `base` of this catcher using `mapper`, returning a new
187194
/// `Catcher` with the returned base.
188195
///

core/lib/src/local/response.rs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ macro_rules! pub_response_impl {
180180
self._into_msgpack() $(.$suffix)?
181181
}
182182

183-
/// Checks if a route was routed by a specific route type
183+
/// Checks if a route was routed by a specific route type. This only returns true if the route
184+
/// actually generated a response, and a catcher was not run.
184185
///
185186
/// # Example
186187
///
@@ -194,6 +195,12 @@ macro_rules! pub_response_impl {
194195
/// assert!(response.routed_by::<index>())
195196
/// # });
196197
/// ```
198+
///
199+
/// # Other Route types
200+
///
201+
/// [`FileServer`](crate::fs::FileServer) implementes `RouteType`, so a route that should
202+
/// return a static file can be checked against it. Libraries which provide a Route type should
203+
/// implement `RouteType`, see [`RouteType`](crate::route::RouteType) for more information.
197204
pub fn routed_by<T: crate::route::RouteType>(&self) -> bool {
198205
if let Some(route_type) = self._request().route().map(|r| r.route_type).flatten() {
199206
route_type == std::any::TypeId::of::<T>()
@@ -208,14 +215,18 @@ macro_rules! pub_response_impl {
208215
///
209216
/// ```rust
210217
/// # use rocket::get;
211-
/// #[get("/")]
212-
/// fn index() -> &'static str { "Hello World" }
218+
/// #[catch(404)]
219+
/// fn default_404() -> &'static str { "Hello World" }
213220
#[doc = $doc_prelude]
214221
/// # Client::_test(|_, _, response| {
215222
/// let response: LocalResponse = response;
216-
/// assert!(response.routed_by::<index>())
223+
/// assert!(response.caught_by::<default_404>())
217224
/// # });
218225
/// ```
226+
///
227+
/// # Rocket's default catcher
228+
///
229+
/// The default catcher has a `CatcherType` of [`DefaultCatcher`](crate::catcher::DefaultCatcher)
219230
pub fn caught_by<T: crate::catcher::CatcherType>(&self) -> bool {
220231
if let Some(catcher_type) = self._request().catcher().map(|r| r.catcher_type).flatten() {
221232
catcher_type == std::any::TypeId::of::<T>()
@@ -224,6 +235,22 @@ macro_rules! pub_response_impl {
224235
}
225236
}
226237

238+
/// Checks if a route was caught by a catcher
239+
///
240+
/// # Example
241+
///
242+
/// ```rust
243+
/// # use rocket::get;
244+
#[doc = $doc_prelude]
245+
/// # Client::_test(|_, _, response| {
246+
/// let response: LocalResponse = response;
247+
/// assert!(response.was_caught())
248+
/// # });
249+
/// ```
250+
pub fn was_caught(&self) -> bool {
251+
self._request().catcher().is_some()
252+
}
253+
227254
#[cfg(test)]
228255
#[allow(dead_code)]
229256
fn _ensure_impls_exist() {

core/lib/src/route/route.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@ use crate::http::{uri, MediaType, Method};
99
use crate::route::{BoxFuture, Handler, RouteUri};
1010
use crate::sentinel::Sentry;
1111

12-
// We could also choose to require a Debug impl?
1312
/// A generic trait for route types. This should be automatically implemented on the structs
14-
/// generated by the codegen for each route.
13+
/// generated by the codegen for each route, and manually implemented on custom route types.
1514
///
16-
/// It may also be desirable to add an option for other routes to define a RouteType. This
17-
/// would likely just be a case of adding an alternate constructor to the Route type.
15+
/// Use the `Route::with_type::<T>()` method to set the route type.
1816
pub trait RouteType: Any + Send + Sync + 'static { }
1917

2018
/// A request handling route.
@@ -225,6 +223,8 @@ impl Route {
225223
/// assert_eq!(index.method, Method::Get);
226224
/// assert_eq!(index.uri, "/");
227225
/// ```
226+
///
227+
/// If applicable, `with_type` should also be called to set the route type for testing
228228
#[track_caller]
229229
pub fn new<H: Handler>(method: Method, uri: &str, handler: H) -> Route {
230230
Route::ranked(None, method, uri, handler)
@@ -255,6 +255,8 @@ impl Route {
255255
/// assert_eq!(foo.method, Method::Post);
256256
/// assert_eq!(foo.uri, "/foo?bar");
257257
/// ```
258+
///
259+
/// If applicable, `with_type` should also be called to set the route type for testing
258260
#[track_caller]
259261
pub fn ranked<H, R>(rank: R, method: Method, uri: &str, handler: H) -> Route
260262
where
@@ -275,7 +277,8 @@ impl Route {
275277
}
276278
}
277279

278-
/// Marks this route with the specified type
280+
/// Marks this route with the specified type. For a custom route type, i.e. something that can
281+
/// be passed to `.mount()`, it should be that type to make identification easier.
279282
pub fn with_type<T: RouteType>(mut self) -> Self {
280283
self.route_type = Some(TypeId::of::<T>());
281284
self

0 commit comments

Comments
 (0)