pub struct Status {
pub code: u16,
}
Expand description
Structure representing an HTTP status: an integer code.
A Status
should rarely be created directly. Instead, an associated
constant should be used; one is declared for every status defined in the
HTTP standard. If a custom status code must be created, note that it is
not possible to set a custom reason phrase.
use rocket::http::Status;
// Create a status from a known constant.
let ok = Status::Ok;
assert_eq!(ok.code, 200);
assert_eq!(ok.reason(), Some("OK"));
let not_found = Status::NotFound;
assert_eq!(not_found.code, 404);
assert_eq!(not_found.reason(), Some("Not Found"));
// Or from a status code: `reason()` returns the phrase when known.
let gone = Status::new(410);
assert_eq!(gone.code, 410);
assert_eq!(gone.reason(), Some("Gone"));
// `reason()` returns `None` when unknown.
let custom = Status::new(599);
assert_eq!(custom.code, 599);
assert_eq!(custom.reason(), None);
Responding
To set a custom Status
on a response, use a response::status
responder, which enforces correct status-based responses. Alternatively,
respond with (Status, T)
where T: Responder
, but beware that the
response may be invalid if it requires additional headers.
use rocket::http::Status;
#[get("/")]
fn index() -> (Status, &'static str) {
(Status::NotFound, "Hey, there's no index!")
}
(De)serialization
Status
is both Serialize
and Deserialize
, represented as a u16
. For
example, Status::Ok
(de)serializes from/to 200
. Any integer in the
range [100, 600)
is allowed to deserialize into a Status
.`
use serde::{Serialize, Deserialize};
use rocket::http::Status;
#[derive(Deserialize, Serialize)]
struct Foo {
status: Status,
}
Fields§
§code: u16
The HTTP status code associated with this status.
Implementations§
source§impl Status
impl Status
sourcepub const SwitchingProtocols: Status = _
pub const SwitchingProtocols: Status = _
Status
with code
101
.
sourcepub const Processing: Status = _
pub const Processing: Status = _
Status
with code
102
.
sourcepub const NonAuthoritativeInformation: Status = _
pub const NonAuthoritativeInformation: Status = _
Status
with code
203
.
sourcepub const ResetContent: Status = _
pub const ResetContent: Status = _
Status
with code
205
.
sourcepub const PartialContent: Status = _
pub const PartialContent: Status = _
Status
with code
206
.
sourcepub const MultiStatus: Status = _
pub const MultiStatus: Status = _
Status
with code
207
.
sourcepub const AlreadyReported: Status = _
pub const AlreadyReported: Status = _
Status
with code
208
.
sourcepub const MultipleChoices: Status = _
pub const MultipleChoices: Status = _
Status
with code
300
.
sourcepub const MovedPermanently: Status = _
pub const MovedPermanently: Status = _
Status
with code
301
.
sourcepub const NotModified: Status = _
pub const NotModified: Status = _
Status
with code
304
.
sourcepub const TemporaryRedirect: Status = _
pub const TemporaryRedirect: Status = _
Status
with code
307
.
sourcepub const PermanentRedirect: Status = _
pub const PermanentRedirect: Status = _
Status
with code
308
.
sourcepub const BadRequest: Status = _
pub const BadRequest: Status = _
Status
with code
400
.
Status
with code
401
.
sourcepub const PaymentRequired: Status = _
pub const PaymentRequired: Status = _
Status
with code
402
.
sourcepub const MethodNotAllowed: Status = _
pub const MethodNotAllowed: Status = _
Status
with code
405
.
sourcepub const NotAcceptable: Status = _
pub const NotAcceptable: Status = _
Status
with code
406
.
sourcepub const ProxyAuthenticationRequired: Status = _
pub const ProxyAuthenticationRequired: Status = _
Status
with code
407
.
sourcepub const RequestTimeout: Status = _
pub const RequestTimeout: Status = _
Status
with code
408
.
sourcepub const LengthRequired: Status = _
pub const LengthRequired: Status = _
Status
with code
411
.
sourcepub const PreconditionFailed: Status = _
pub const PreconditionFailed: Status = _
Status
with code
412
.
sourcepub const PayloadTooLarge: Status = _
pub const PayloadTooLarge: Status = _
Status
with code
413
.
sourcepub const UriTooLong: Status = _
pub const UriTooLong: Status = _
Status
with code
414
.
sourcepub const UnsupportedMediaType: Status = _
pub const UnsupportedMediaType: Status = _
Status
with code
415
.
sourcepub const RangeNotSatisfiable: Status = _
pub const RangeNotSatisfiable: Status = _
Status
with code
416
.
sourcepub const ExpectationFailed: Status = _
pub const ExpectationFailed: Status = _
Status
with code
417
.
sourcepub const MisdirectedRequest: Status = _
pub const MisdirectedRequest: Status = _
Status
with code
421
.
sourcepub const UnprocessableEntity: Status = _
pub const UnprocessableEntity: Status = _
Status
with code
422
.
sourcepub const FailedDependency: Status = _
pub const FailedDependency: Status = _
Status
with code
424
.
sourcepub const UpgradeRequired: Status = _
pub const UpgradeRequired: Status = _
Status
with code
426
.
sourcepub const PreconditionRequired: Status = _
pub const PreconditionRequired: Status = _
Status
with code
428
.
sourcepub const TooManyRequests: Status = _
pub const TooManyRequests: Status = _
Status
with code
429
.
sourcepub const RequestHeaderFieldsTooLarge: Status = _
pub const RequestHeaderFieldsTooLarge: Status = _
Status
with code
431
.
Status
with code
451
.
sourcepub const InternalServerError: Status = _
pub const InternalServerError: Status = _
Status
with code
500
.
sourcepub const NotImplemented: Status = _
pub const NotImplemented: Status = _
Status
with code
501
.
sourcepub const BadGateway: Status = _
pub const BadGateway: Status = _
Status
with code
502
.
Status
with code
503
.
sourcepub const GatewayTimeout: Status = _
pub const GatewayTimeout: Status = _
Status
with code
504
.
sourcepub const HttpVersionNotSupported: Status = _
pub const HttpVersionNotSupported: Status = _
Status
with code
505
.
sourcepub const VariantAlsoNegotiates: Status = _
pub const VariantAlsoNegotiates: Status = _
Status
with code
506
.
sourcepub const InsufficientStorage: Status = _
pub const InsufficientStorage: Status = _
Status
with code
507
.
sourcepub const LoopDetected: Status = _
pub const LoopDetected: Status = _
Status
with code
508
.
sourcepub const NotExtended: Status = _
pub const NotExtended: Status = _
Status
with code
510
.
sourcepub const NetworkAuthenticationRequired: Status = _
pub const NetworkAuthenticationRequired: Status = _
Status
with code
511
.
sourcepub const fn new(code: u16) -> Status
pub const fn new(code: u16) -> Status
Creates a new Status
with code
. This should be used only to
construct non-standard HTTP statuses. Use an associated constant for
standard statuses.
Example
Create a custom 299
status:
use rocket::http::Status;
let custom = Status::new(299);
assert_eq!(custom.code, 299);
sourcepub const fn class(self) -> StatusClass
pub const fn class(self) -> StatusClass
Returns the class of a given status.
Example
use rocket::http::{Status, StatusClass};
let processing = Status::Processing;
assert_eq!(processing.class(), StatusClass::Informational);
let ok = Status::Ok;
assert_eq!(ok.class(), StatusClass::Success);
let see_other = Status::SeeOther;
assert_eq!(see_other.class(), StatusClass::Redirection);
let not_found = Status::NotFound;
assert_eq!(not_found.class(), StatusClass::ClientError);
let internal_error = Status::InternalServerError;
assert_eq!(internal_error.class(), StatusClass::ServerError);
let custom = Status::new(600);
assert_eq!(custom.class(), StatusClass::Unknown);
sourcepub const fn from_code(code: u16) -> Option<Status>
pub const fn from_code(code: u16) -> Option<Status>
Returns a Status given a standard status code code
. If code
is
not a known status code, None
is returned.
Example
Create a Status
from a known code
:
use rocket::http::Status;
let not_found = Status::from_code(404);
assert_eq!(not_found, Some(Status::NotFound));
Create a Status
from an unknown code
:
use rocket::http::Status;
let unknown = Status::from_code(600);
assert!(unknown.is_none());
sourcepub const fn reason(&self) -> Option<&'static str>
pub const fn reason(&self) -> Option<&'static str>
Returns the canonical reason phrase if self
corresponds to a
canonical, known status code. Otherwise, returns None
.
Example
Reason phrase from a known code
:
use rocket::http::Status;
assert_eq!(Status::Created.reason(), Some("Created"));
assert_eq!(Status::new(200).reason(), Some("OK"));
Absent phrase from an unknown code
:
use rocket::http::Status;
assert_eq!(Status::new(499).reason(), None);
sourcepub const fn reason_lossy(&self) -> &'static str
pub const fn reason_lossy(&self) -> &'static str
Returns the canonical reason phrase if self
corresponds to a
canonical, known status code, or an unspecified but relevant reason
phrase otherwise.
Example
use rocket::http::Status;
assert_eq!(Status::NotFound.reason_lossy(), "Not Found");
assert_eq!(Status::new(100).reason_lossy(), "Continue");
assert!(!Status::new(699).reason_lossy().is_empty());
Trait Implementations§
source§impl<'de> Deserialize<'de> for Status
impl<'de> Deserialize<'de> for Status
source§fn deserialize<D>(
deserializer: D
) -> Result<Status, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>( deserializer: D ) -> Result<Status, <D as Deserializer<'de>>::Error>where D: Deserializer<'de>,
source§impl<S, E> IntoOutcome<S, (Status, E), Status> for Result<S, E>
impl<S, E> IntoOutcome<S, (Status, E), Status> for Result<S, E>
source§fn into_outcome(self, status: Status) -> Outcome<S, E>
fn into_outcome(self, status: Status) -> Outcome<S, E>
self
into an Outcome
. If self
represents a success, an
Outcome::Success
is returned. Otherwise, an Outcome::Failure
is
returned with failure
as the inner value.source§fn or_forward(self, status: Status) -> Outcome<S, E>
fn or_forward(self, status: Status) -> Outcome<S, E>
self
into an Outcome
. If self
represents a success, an
Outcome::Success
is returned. Otherwise, an Outcome::Forward
is
returned with forward
as the inner value.source§impl Ord for Status
impl Ord for Status
source§impl PartialEq<Status> for Status
impl PartialEq<Status> for Status
source§impl PartialOrd<Status> for Status
impl PartialOrd<Status> for Status
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<'r> Responder<'r, 'static> for Status
impl<'r> Responder<'r, 'static> for Status
The response generated by Status
depends on the status code itself. The
table below summarizes the functionality:
Status Code Range | Response |
---|---|
[400, 599] | Forwards to catcher for given status. |
100, [200, 205] | Empty with status of self . |
All others. | Invalid. Errors to 500 catcher. |
In short, a client or server error status codes will forward to the
corresponding error catcher, a successful status code less than 206
or
100
responds with any empty body and the given status code, and all other
status code emit an error message and forward to the 500
(internal server
error) catcher.
source§impl<'a> Serialize for Status
impl<'a> Serialize for Status
source§fn serialize<S>(
&self,
serializer: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>( &self, serializer: S ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where S: Serializer,
impl Copy for Status
impl Eq for Status
Auto Trait Implementations§
impl RefUnwindSafe for Status
impl Send for Status
impl Sync for Status
impl Unpin for Status
impl UnwindSafe for Status
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.source§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoCollection<T> for T
impl<T> IntoCollection<T> for T
source§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere T: ?Sized,
source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the foreground set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red()
and
green()
, which have the same functionality but are
pithier.
Example
Set foreground color to white using fg()
:
use yansi::{Paint, Color};
painted.fg(Color::White);
Set foreground color to white using white()
.
use yansi::Paint;
painted.white();
source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Returns self
with the
fg()
set to
Color::BrightYellow
.
Example
println!("{}", value.bright_yellow());
source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Returns self
with the
fg()
set to
Color::BrightMagenta
.
Example
println!("{}", value.bright_magenta());
source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the background set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red()
and
on_green()
, which have the same functionality but
are pithier.
Example
Set background color to red using fg()
:
use yansi::{Paint, Color};
painted.bg(Color::Red);
Set background color to red using on_red()
.
use yansi::Paint;
painted.on_red();
source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightBlack
.
Example
println!("{}", value.on_bright_black());
source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightGreen
.
Example
println!("{}", value.on_bright_green());
source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightYellow
.
Example
println!("{}", value.on_bright_yellow());
source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightBlue
.
Example
println!("{}", value.on_bright_blue());
source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightMagenta
.
Example
println!("{}", value.on_bright_magenta());
source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightCyan
.
Example
println!("{}", value.on_bright_cyan());
source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightWhite
.
Example
println!("{}", value.on_bright_white());
source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute
value
.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold()
and
underline()
, which have the same functionality
but are pithier.
Example
Make text bold using attr()
:
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);
Make text bold using using bold()
.
use yansi::Paint;
painted.bold();
source§fn underline(&self) -> Painted<&T>
fn underline(&self) -> Painted<&T>
Returns self
with the
attr()
set to
Attribute::Underline
.
Example
println!("{}", value.underline());
source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Returns self
with the
attr()
set to
Attribute::RapidBlink
.
Example
println!("{}", value.rapid_blink());
source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi
Quirk
value
.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask()
and
wrap()
, which have the same functionality but are
pithier.
Example
Enable wrapping using .quirk()
:
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);
Enable wrapping using wrap()
.
use yansi::Paint;
painted.wrap();
source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition
value
applies. Replaces any previous condition.
See the crate level docs for more details.
Example
Enable styling painted
only when both stdout
and stderr
are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);