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§
§impl Status
impl Status
pub const SwitchingProtocols: Status = _
pub const SwitchingProtocols: Status = _
Status
with code
101
.
pub const Processing: Status = _
pub const Processing: Status = _
Status
with code
102
.
pub const NonAuthoritativeInformation: Status = _
pub const NonAuthoritativeInformation: Status = _
Status
with code
203
.
pub const ResetContent: Status = _
pub const ResetContent: Status = _
Status
with code
205
.
pub const PartialContent: Status = _
pub const PartialContent: Status = _
Status
with code
206
.
pub const MultiStatus: Status = _
pub const MultiStatus: Status = _
Status
with code
207
.
pub const AlreadyReported: Status = _
pub const AlreadyReported: Status = _
Status
with code
208
.
pub const MultipleChoices: Status = _
pub const MultipleChoices: Status = _
Status
with code
300
.
pub const MovedPermanently: Status = _
pub const MovedPermanently: Status = _
Status
with code
301
.
pub const NotModified: Status = _
pub const NotModified: Status = _
Status
with code
304
.
pub const TemporaryRedirect: Status = _
pub const TemporaryRedirect: Status = _
Status
with code
307
.
pub const PermanentRedirect: Status = _
pub const PermanentRedirect: Status = _
Status
with code
308
.
pub const BadRequest: Status = _
pub const BadRequest: Status = _
Status
with code
400
.
Status
with code
401
.
pub const PaymentRequired: Status = _
pub const PaymentRequired: Status = _
Status
with code
402
.
pub const MethodNotAllowed: Status = _
pub const MethodNotAllowed: Status = _
Status
with code
405
.
pub const NotAcceptable: Status = _
pub const NotAcceptable: Status = _
Status
with code
406
.
pub const ProxyAuthenticationRequired: Status = _
pub const ProxyAuthenticationRequired: Status = _
Status
with code
407
.
pub const RequestTimeout: Status = _
pub const RequestTimeout: Status = _
Status
with code
408
.
pub const LengthRequired: Status = _
pub const LengthRequired: Status = _
Status
with code
411
.
pub const PreconditionFailed: Status = _
pub const PreconditionFailed: Status = _
Status
with code
412
.
pub const PayloadTooLarge: Status = _
pub const PayloadTooLarge: Status = _
Status
with code
413
.
pub const UriTooLong: Status = _
pub const UriTooLong: Status = _
Status
with code
414
.
pub const UnsupportedMediaType: Status = _
pub const UnsupportedMediaType: Status = _
Status
with code
415
.
pub const RangeNotSatisfiable: Status = _
pub const RangeNotSatisfiable: Status = _
Status
with code
416
.
pub const ExpectationFailed: Status = _
pub const ExpectationFailed: Status = _
Status
with code
417
.
pub const MisdirectedRequest: Status = _
pub const MisdirectedRequest: Status = _
Status
with code
421
.
pub const UnprocessableEntity: Status = _
pub const UnprocessableEntity: Status = _
Status
with code
422
.
pub const FailedDependency: Status = _
pub const FailedDependency: Status = _
Status
with code
424
.
pub const UpgradeRequired: Status = _
pub const UpgradeRequired: Status = _
Status
with code
426
.
pub const PreconditionRequired: Status = _
pub const PreconditionRequired: Status = _
Status
with code
428
.
pub const TooManyRequests: Status = _
pub const TooManyRequests: Status = _
Status
with code
429
.
pub const RequestHeaderFieldsTooLarge: Status = _
pub const RequestHeaderFieldsTooLarge: Status = _
Status
with code
431
.
Status
with code
451
.
pub const InternalServerError: Status = _
pub const InternalServerError: Status = _
Status
with code
500
.
pub const NotImplemented: Status = _
pub const NotImplemented: Status = _
Status
with code
501
.
pub const BadGateway: Status = _
pub const BadGateway: Status = _
Status
with code
502
.
Status
with code
503
.
pub const GatewayTimeout: Status = _
pub const GatewayTimeout: Status = _
Status
with code
504
.
pub const HttpVersionNotSupported: Status = _
pub const HttpVersionNotSupported: Status = _
Status
with code
505
.
pub const VariantAlsoNegotiates: Status = _
pub const VariantAlsoNegotiates: Status = _
Status
with code
506
.
pub const InsufficientStorage: Status = _
pub const InsufficientStorage: Status = _
Status
with code
507
.
pub const LoopDetected: Status = _
pub const LoopDetected: Status = _
Status
with code
508
.
pub const NotExtended: Status = _
pub const NotExtended: Status = _
Status
with code
510
.
pub const NetworkAuthenticationRequired: Status = _
pub const NetworkAuthenticationRequired: Status = _
Status
with code
511
.
pub 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);
pub 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);
pub 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());
pub 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);
pub 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§
§impl<'de> Deserialize<'de> for Status
impl<'de> Deserialize<'de> for Status
§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>,
§impl Ord for Status
impl Ord for Status
§impl PartialOrd for Status
impl PartialOrd for Status
source§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.
§impl Serialize for Status
impl Serialize for Status
§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
impl StructuralPartialEq 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> AsAny for Twhere
T: Any,
impl<T> AsAny for Twhere
T: Any,
fn as_any_ref(&self) -> &(dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)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> 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);