Struct rocket_http::Status

source ·
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

source

pub const Continue: Status = _

Status with code 100 .

source

pub const SwitchingProtocols: Status = _

Status with code 101 .

source

pub const Processing: Status = _

Status with code 102 .

source

pub const Ok: Status = _

Status with code 200 .

source

pub const Created: Status = _

Status with code 201 .

source

pub const Accepted: Status = _

Status with code 202 .

source

pub const NonAuthoritativeInformation: Status = _

Status with code 203 .

source

pub const NoContent: Status = _

Status with code 204 .

source

pub const ResetContent: Status = _

Status with code 205 .

source

pub const PartialContent: Status = _

Status with code 206 .

source

pub const MultiStatus: Status = _

Status with code 207 .

source

pub const AlreadyReported: Status = _

Status with code 208 .

source

pub const ImUsed: Status = _

Status with code 226 .

source

pub const MultipleChoices: Status = _

Status with code 300 .

source

pub const MovedPermanently: Status = _

Status with code 301 .

source

pub const Found: Status = _

Status with code 302 .

source

pub const SeeOther: Status = _

Status with code 303 .

source

pub const NotModified: Status = _

Status with code 304 .

source

pub const UseProxy: Status = _

Status with code 305 .

source

pub const TemporaryRedirect: Status = _

Status with code 307 .

source

pub const PermanentRedirect: Status = _

Status with code 308 .

source

pub const BadRequest: Status = _

Status with code 400 .

source

pub const Unauthorized: Status = _

Status with code 401 .

source

pub const PaymentRequired: Status = _

Status with code 402 .

source

pub const Forbidden: Status = _

Status with code 403 .

source

pub const NotFound: Status = _

Status with code 404 .

source

pub const MethodNotAllowed: Status = _

Status with code 405 .

source

pub const NotAcceptable: Status = _

Status with code 406 .

source

pub const ProxyAuthenticationRequired: Status = _

Status with code 407 .

source

pub const RequestTimeout: Status = _

Status with code 408 .

source

pub const Conflict: Status = _

Status with code 409 .

source

pub const Gone: Status = _

Status with code 410 .

source

pub const LengthRequired: Status = _

Status with code 411 .

source

pub const PreconditionFailed: Status = _

Status with code 412 .

source

pub const PayloadTooLarge: Status = _

Status with code 413 .

source

pub const UriTooLong: Status = _

Status with code 414 .

source

pub const UnsupportedMediaType: Status = _

Status with code 415 .

source

pub const RangeNotSatisfiable: Status = _

Status with code 416 .

source

pub const ExpectationFailed: Status = _

Status with code 417 .

source

pub const ImATeapot: Status = _

Status with code 418 .

source

pub const MisdirectedRequest: Status = _

Status with code 421 .

source

pub const UnprocessableEntity: Status = _

Status with code 422 .

source

pub const Locked: Status = _

Status with code 423 .

source

pub const FailedDependency: Status = _

Status with code 424 .

source

pub const UpgradeRequired: Status = _

Status with code 426 .

source

pub const PreconditionRequired: Status = _

Status with code 428 .

source

pub const TooManyRequests: Status = _

Status with code 429 .

source

pub const RequestHeaderFieldsTooLarge: Status = _

Status with code 431 .

source

pub const UnavailableForLegalReasons: Status = _

Status with code 451 .

source

pub const InternalServerError: Status = _

Status with code 500 .

source

pub const NotImplemented: Status = _

Status with code 501 .

source

pub const BadGateway: Status = _

Status with code 502 .

source

pub const ServiceUnavailable: Status = _

Status with code 503 .

source

pub const GatewayTimeout: Status = _

Status with code 504 .

source

pub const HttpVersionNotSupported: Status = _

Status with code 505 .

source

pub const VariantAlsoNegotiates: Status = _

Status with code 506 .

source

pub const InsufficientStorage: Status = _

Status with code 507 .

source

pub const LoopDetected: Status = _

Status with code 508 .

source

pub const NotExtended: Status = _

Status with code 510 .

source

pub const NetworkAuthenticationRequired: Status = _

Status with code 511 .

source

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);
source

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);
source

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());
source

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);
source

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 Clone for Status

source§

fn clone(&self) -> Status

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Status

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Status

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl Display for Status

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Hash for Status

source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl Ord for Status

source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq for Status

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for Status

source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Copy for Status

source§

impl Eq for Status

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<Q, K> Comparable<K> for Qwhere Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
§

impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoCollection<T> for T

source§

fn into_collection<A>(self) -> SmallVec<A>where A: Array<Item = T>,

Converts self into a collection.
source§

fn mapped<U, F, A>(self, f: F) -> SmallVec<A>where F: FnMut(T) -> U, A: Array<Item = U>,

§

impl<T> Paint for Twhere T: ?Sized,

§

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();
§

fn primary(&self) -> Painted<&T>

Returns self with the fg() set to [Color::Primary].

Example
println!("{}", value.primary());
§

fn fixed(&self, color: u8) -> Painted<&T>

Returns self with the fg() set to [Color::Fixed].

Example
println!("{}", value.fixed(color));
§

fn rgb(&self, r: u8, g: u8, b: u8) -> Painted<&T>

Returns self with the fg() set to [Color::Rgb].

Example
println!("{}", value.rgb(r, g, b));
§

fn black(&self) -> Painted<&T>

Returns self with the fg() set to [Color::Black].

Example
println!("{}", value.black());
§

fn red(&self) -> Painted<&T>

Returns self with the fg() set to [Color::Red].

Example
println!("{}", value.red());
§

fn green(&self) -> Painted<&T>

Returns self with the fg() set to [Color::Green].

Example
println!("{}", value.green());
§

fn yellow(&self) -> Painted<&T>

Returns self with the fg() set to [Color::Yellow].

Example
println!("{}", value.yellow());
§

fn blue(&self) -> Painted<&T>

Returns self with the fg() set to [Color::Blue].

Example
println!("{}", value.blue());
§

fn magenta(&self) -> Painted<&T>

Returns self with the fg() set to [Color::Magenta].

Example
println!("{}", value.magenta());
§

fn cyan(&self) -> Painted<&T>

Returns self with the fg() set to [Color::Cyan].

Example
println!("{}", value.cyan());
§

fn white(&self) -> Painted<&T>

Returns self with the fg() set to [Color::White].

Example
println!("{}", value.white());
§

fn bright_black(&self) -> Painted<&T>

Returns self with the fg() set to [Color::BrightBlack].

Example
println!("{}", value.bright_black());
§

fn bright_red(&self) -> Painted<&T>

Returns self with the fg() set to [Color::BrightRed].

Example
println!("{}", value.bright_red());
§

fn bright_green(&self) -> Painted<&T>

Returns self with the fg() set to [Color::BrightGreen].

Example
println!("{}", value.bright_green());
§

fn bright_yellow(&self) -> Painted<&T>

Returns self with the fg() set to [Color::BrightYellow].

Example
println!("{}", value.bright_yellow());
§

fn bright_blue(&self) -> Painted<&T>

Returns self with the fg() set to [Color::BrightBlue].

Example
println!("{}", value.bright_blue());
§

fn bright_magenta(&self) -> Painted<&T>

Returns self with the fg() set to [Color::BrightMagenta].

Example
println!("{}", value.bright_magenta());
§

fn bright_cyan(&self) -> Painted<&T>

Returns self with the fg() set to [Color::BrightCyan].

Example
println!("{}", value.bright_cyan());
§

fn bright_white(&self) -> Painted<&T>

Returns self with the fg() set to [Color::BrightWhite].

Example
println!("{}", value.bright_white());
§

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();
§

fn on_primary(&self) -> Painted<&T>

Returns self with the bg() set to [Color::Primary].

Example
println!("{}", value.on_primary());
§

fn on_fixed(&self, color: u8) -> Painted<&T>

Returns self with the bg() set to [Color::Fixed].

Example
println!("{}", value.on_fixed(color));
§

fn on_rgb(&self, r: u8, g: u8, b: u8) -> Painted<&T>

Returns self with the bg() set to [Color::Rgb].

Example
println!("{}", value.on_rgb(r, g, b));
§

fn on_black(&self) -> Painted<&T>

Returns self with the bg() set to [Color::Black].

Example
println!("{}", value.on_black());
§

fn on_red(&self) -> Painted<&T>

Returns self with the bg() set to [Color::Red].

Example
println!("{}", value.on_red());
§

fn on_green(&self) -> Painted<&T>

Returns self with the bg() set to [Color::Green].

Example
println!("{}", value.on_green());
§

fn on_yellow(&self) -> Painted<&T>

Returns self with the bg() set to [Color::Yellow].

Example
println!("{}", value.on_yellow());
§

fn on_blue(&self) -> Painted<&T>

Returns self with the bg() set to [Color::Blue].

Example
println!("{}", value.on_blue());
§

fn on_magenta(&self) -> Painted<&T>

Returns self with the bg() set to [Color::Magenta].

Example
println!("{}", value.on_magenta());
§

fn on_cyan(&self) -> Painted<&T>

Returns self with the bg() set to [Color::Cyan].

Example
println!("{}", value.on_cyan());
§

fn on_white(&self) -> Painted<&T>

Returns self with the bg() set to [Color::White].

Example
println!("{}", value.on_white());
§

fn on_bright_black(&self) -> Painted<&T>

Returns self with the bg() set to [Color::BrightBlack].

Example
println!("{}", value.on_bright_black());
§

fn on_bright_red(&self) -> Painted<&T>

Returns self with the bg() set to [Color::BrightRed].

Example
println!("{}", value.on_bright_red());
§

fn on_bright_green(&self) -> Painted<&T>

Returns self with the bg() set to [Color::BrightGreen].

Example
println!("{}", value.on_bright_green());
§

fn on_bright_yellow(&self) -> Painted<&T>

Returns self with the bg() set to [Color::BrightYellow].

Example
println!("{}", value.on_bright_yellow());
§

fn on_bright_blue(&self) -> Painted<&T>

Returns self with the bg() set to [Color::BrightBlue].

Example
println!("{}", value.on_bright_blue());
§

fn on_bright_magenta(&self) -> Painted<&T>

Returns self with the bg() set to [Color::BrightMagenta].

Example
println!("{}", value.on_bright_magenta());
§

fn on_bright_cyan(&self) -> Painted<&T>

Returns self with the bg() set to [Color::BrightCyan].

Example
println!("{}", value.on_bright_cyan());
§

fn on_bright_white(&self) -> Painted<&T>

Returns self with the bg() set to [Color::BrightWhite].

Example
println!("{}", value.on_bright_white());
§

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();
§

fn bold(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute::Bold].

Example
println!("{}", value.bold());
§

fn dim(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute::Dim].

Example
println!("{}", value.dim());
§

fn italic(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute::Italic].

Example
println!("{}", value.italic());
§

fn underline(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute::Underline].

Example
println!("{}", value.underline());

Returns self with the attr() set to [Attribute::Blink].

Example
println!("{}", value.blink());

Returns self with the attr() set to [Attribute::RapidBlink].

Example
println!("{}", value.rapid_blink());
§

fn invert(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute::Invert].

Example
println!("{}", value.invert());
§

fn conceal(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute::Conceal].

Example
println!("{}", value.conceal());
§

fn strike(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute::Strike].

Example
println!("{}", value.strike());
§

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();
§

fn mask(&self) -> Painted<&T>

Returns self with the quirk() set to [Quirk::Mask].

Example
println!("{}", value.mask());
§

fn wrap(&self) -> Painted<&T>

Returns self with the quirk() set to [Quirk::Wrap].

Example
println!("{}", value.wrap());
§

fn linger(&self) -> Painted<&T>

Returns self with the quirk() set to [Quirk::Linger].

Example
println!("{}", value.linger());
§

fn clear(&self) -> Painted<&T>

Returns self with the quirk() set to [Quirk::Clear].

Example
println!("{}", value.clear());
§

fn bright(&self) -> Painted<&T>

Returns self with the quirk() set to [Quirk::Bright].

Example
println!("{}", value.bright());
§

fn on_bright(&self) -> Painted<&T>

Returns self with the quirk() set to [Quirk::OnBright].

Example
println!("{}", value.on_bright());
§

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);
§

fn new(self) -> Painted<Self>where Self: Sized,

Create a new [Painted] with a default [Style]. Read more
§

fn paint<S>(&self, style: S) -> Painted<&Self>where S: Into<Style>,

Apply a style wholesale to self. Any previous style is replaced. Read more
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more