[go: up one dir, main page]

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!")
}

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<Self>,

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

impl PartialEq<Status> 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<Status> 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,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

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

source§

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

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

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

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

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

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>,

const: unstable · 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>,

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.
const: unstable · 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.
const: unstable · source§

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

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

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

fn with_current_subscriber(self) -> WithDispatch<Self>

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