Skip to content

Commit a6f0c9d

Browse files
committed
status: add CONTENT_TOO_LARGE and UNPROCESSABLE_CONTENT aliases
RFC9110 changed phrases for status code 413 and 422: * 413 Payload Too Large → Content Too Large * 422 Unprocessable Entity → Unprocessable Content Introduce CONTENT_TOO_LARGE and UNPROCESSABLE_CONTENT StatusCode const items to reflect those changes and update phrases used in those status code. While at it, update phrase for status 203 to ‘Non-Authoritative Information’ (with a dash).
1 parent 8c1fb20 commit a6f0c9d

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

src/status.rs

+27-7
Original file line numberDiff line numberDiff line change
@@ -302,25 +302,41 @@ macro_rules! status_codes {
302302
(
303303
$(
304304
$(#[$docs:meta])*
305-
($num:expr, $konst:ident, $phrase:expr);
305+
($num:expr, $name:ident $(aka $alias:ident)*, $phrase:literal);
306306
)+
307307
) => {
308308
impl StatusCode {
309309
$(
310310
$(#[$docs])*
311-
pub const $konst: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked($num) });
311+
pub const $name: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked($num) });
312+
$(
313+
status_codes! {
314+
@alias $alias = $name;
315+
concat!("Alias of [`", stringify!($name),
316+
"`](`Self::", stringify!($name), "`).")
317+
}
318+
)*
312319
)+
313320

314321
}
315322

316323
fn canonical_reason(num: u16) -> Option<&'static str> {
317324
match num {
325+
// Make sure none of the numbers are < 100 or > 999.
326+
0..=99 | 1000..=u16::MAX => None,
318327
$(
319328
$num => Some($phrase),
320329
)+
321330
_ => None
322331
}
323332
}
333+
};
334+
335+
// Work around rustc 1.49 not supporting #[doc = concat!(...)]. With newer
336+
// rustc this can be inlined.
337+
(@alias $alias:ident = $name:ident; $doc:expr) => {
338+
#[doc = $doc]
339+
pub const $alias: StatusCode = StatusCode::$name;
324340
}
325341
}
326342

@@ -346,7 +362,7 @@ status_codes! {
346362
(202, ACCEPTED, "Accepted");
347363
/// 203 Non-Authoritative Information
348364
/// [[RFC9110, Section 15.3.4](https://datatracker.ietf.org/doc/html/rfc9110#section-15.3.4)]
349-
(203, NON_AUTHORITATIVE_INFORMATION, "Non Authoritative Information");
365+
(203, NON_AUTHORITATIVE_INFORMATION, "Non-Authoritative Information");
350366
/// 204 No Content
351367
/// [[RFC9110, Section 15.3.5](https://datatracker.ietf.org/doc/html/rfc9110#section-15.3.5)]
352368
(204, NO_CONTENT, "No Content");
@@ -431,9 +447,11 @@ status_codes! {
431447
/// 412 Precondition Failed
432448
/// [[RFC9110, Section 15.5.13](https://datatracker.ietf.org/doc/html/rfc9110#section-15.5.13)]
433449
(412, PRECONDITION_FAILED, "Precondition Failed");
434-
/// 413 Payload Too Large
450+
/// 413 Content Too Large
435451
/// [[RFC9110, Section 15.5.14](https://datatracker.ietf.org/doc/html/rfc9110#section-15.5.14)]
436-
(413, PAYLOAD_TOO_LARGE, "Payload Too Large");
452+
///
453+
/// Prior to RFC9110 phrase for this status was ‘Payload Too Large’.
454+
(413, CONTENT_TOO_LARGE aka PAYLOAD_TOO_LARGE, "Content Too Large");
437455
/// 414 URI Too Long
438456
/// [[RFC9110, Section 15.5.15](https://datatracker.ietf.org/doc/html/rfc9110#section-15.5.15)]
439457
(414, URI_TOO_LONG, "URI Too Long");
@@ -453,9 +471,11 @@ status_codes! {
453471
/// 421 Misdirected Request
454472
/// [[RFC9110, Section 15.5.20](https://datatracker.ietf.org/doc/html/rfc9110#section-15.5.20)]
455473
(421, MISDIRECTED_REQUEST, "Misdirected Request");
456-
/// 422 Unprocessable Entity
474+
/// 422 Unprocessable Content
457475
/// [[RFC9110, Section 15.5.21](https://datatracker.ietf.org/doc/html/rfc9110#section-15.5.21)]
458-
(422, UNPROCESSABLE_ENTITY, "Unprocessable Entity");
476+
///
477+
/// Prior to RFC9110 phrase for this status was ‘Unprocessable Entity’.
478+
(422, UNPROCESSABLE_CONTENT aka UNPROCESSABLE_ENTITY, "Unprocessable Content");
459479
/// 423 Locked
460480
/// [[RFC4918, Section 11.3](https://datatracker.ietf.org/doc/html/rfc4918#section-11.3)]
461481
(423, LOCKED, "Locked");

0 commit comments

Comments
 (0)