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 Ord for Status
impl Ord for Status
Source§impl PartialOrd for Status
impl PartialOrd for Status
Source§impl<'r> Responder<'r, 'static> for Status
The response generated by Status
depends on the status code itself. The
table below summarizes the functionality:
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 Freeze for Status
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<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
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> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§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>
Returns self
with the
fg()
set to
Color::BrightBlack
.
§Example
println!("{}", value.bright_black());
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>
Returns self
with the
fg()
set to
Color::BrightGreen
.
§Example
println!("{}", value.bright_green());
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>
Returns self
with the
fg()
set to
Color::BrightWhite
.
§Example
println!("{}", value.bright_white());
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 clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting()
due to conflicts with Vec::clear()
.
The clear()
method will be removed in a future release.
fn clear(&self) -> Painted<&T>
resetting()
due to conflicts with Vec::clear()
.
The clear()
method will be removed in a future release.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);