[−][src]Struct rocket::response::Flash
Sets a "flash" cookie that will be removed when it is accessed. The anologous request type is FlashMessage.
This type makes it easy to send messages across requests. It is typically used for "status" messages after redirects. For instance, if a user attempts to visit a page he/she does not have access to, you may want to redirect the user to a safe place and show a message indicating what happened on the redirected page. The message should only persist for a single request. This can be accomplished with this type.
Usage
Each Flash
message consists of a name
and some msg
contents. A generic
constructor (new) can be used to construct a message with any
name, while the warning, success, and
error constructors create messages with the corresponding
names.
Messages can be retrieved on the request side via the FlashMessage type and the name and msg methods.
Response
The Responder
implementation for Flash
sets the message cookie and then
uses the passed in responder res
to complete the response. In other words,
it simply sets a cookie and delagates the rest of the response handling to
the wrapped responder.
Example
The following complete Rocket application illustrates the use of a Flash
message on both the request and response sides.
use rocket::response::{Flash, Redirect}; use rocket::request::FlashMessage; use rocket::http::RawStr; #[post("/login/<name>")] fn login(name: &RawStr) -> Result<&'static str, Flash<Redirect>> { if name == "special_user" { Ok("Hello, special user!") } else { Err(Flash::error(Redirect::to("/"), "Invalid username.")) } } #[get("/")] fn index(flash: Option<FlashMessage>) -> String { flash.map(|msg| format!("{}: {}", msg.name(), msg.msg())) .unwrap_or_else(|| "Welcome!".to_string()) } fn main() { rocket::ignite().mount("/", routes![login, index]).launch(); }
On the response side (in login
), a Flash
error message is set if some
fictional authentication failed, and the user is redirected to "/"
. On the
request side (in index
), the handler emits the flash message if there is
one and otherwise emits a standard welcome message. Note that if the user
were to refresh the index page after viewing a flash message, the user would
receive the standard welcome message.
Methods
impl<'r, R: Responder<'r>> Flash<R>
[src]
impl<'r, R: Responder<'r>> Flash<R>
pub fn new<N: AsRef<str>, M: AsRef<str>>(res: R, name: N, msg: M) -> Flash<R>
[src]
pub fn new<N: AsRef<str>, M: AsRef<str>>(res: R, name: N, msg: M) -> Flash<R>
Constructs a new Flash
message with the given name
, msg
, and
underlying responder
.
Examples
Construct a "suggestion" message with contents "Try this out!" that redirects to "/".
use rocket::response::{Redirect, Flash}; let msg = Flash::new(Redirect::to("/"), "suggestion", "Try this out!");
pub fn success<S: AsRef<str>>(responder: R, msg: S) -> Flash<R>
[src]
pub fn success<S: AsRef<str>>(responder: R, msg: S) -> Flash<R>
Constructs a "success" Flash
message with the given responder
and
msg
.
Examples
Construct a "success" message with contents "It worked!" that redirects to "/".
use rocket::response::{Redirect, Flash}; let msg = Flash::success(Redirect::to("/"), "It worked!");
pub fn warning<S: AsRef<str>>(responder: R, msg: S) -> Flash<R>
[src]
pub fn warning<S: AsRef<str>>(responder: R, msg: S) -> Flash<R>
Constructs a "warning" Flash
message with the given responder
and
msg
.
Examples
Construct a "warning" message with contents "Watch out!" that redirects to "/".
use rocket::response::{Redirect, Flash}; let msg = Flash::warning(Redirect::to("/"), "Watch out!");
pub fn error<S: AsRef<str>>(responder: R, msg: S) -> Flash<R>
[src]
pub fn error<S: AsRef<str>>(responder: R, msg: S) -> Flash<R>
Constructs an "error" Flash
message with the given responder
and
msg
.
Examples
Construct an "error" message with contents "Whoops!" that redirects to "/".
use rocket::response::{Redirect, Flash}; let msg = Flash::error(Redirect::to("/"), "Whoops!");
impl Flash<()>
[src]
impl Flash<()>
pub fn name(&self) -> &str
[src]
pub fn name(&self) -> &str
Returns the name
of this message.
pub fn msg(&self) -> &str
[src]
pub fn msg(&self) -> &str
Returns the msg
contents of this message.
Trait Implementations
impl<'a, 'r> FromRequest<'a, 'r> for Flash<()>
[src]
impl<'a, 'r> FromRequest<'a, 'r> for Flash<()>
Retrieves a flash message from a flash cookie and deletes the flash cookie.
If there is no flash cookie, an empty Err
is returned.
The suggested use is through an Option
and the FlashMessage
type alias
in request
: Option<FlashMessage>
.
type Error = ()
The associated error to be returned if derivation fails.
fn from_request(request: &'a Request<'r>) -> Outcome<Self, Self::Error>
[src]
fn from_request(request: &'a Request<'r>) -> Outcome<Self, Self::Error>
Derives an instance of Self
from the incoming request metadata. Read more
impl<'r, R: Responder<'r>> Responder<'r> for Flash<R>
[src]
impl<'r, R: Responder<'r>> Responder<'r> for Flash<R>
Sets the message cookie and then uses the wrapped responder to complete the
response. In other words, simply sets a cookie and delagates the rest of the
response handling to the wrapped responder. As a result, the Outcome
of
the response is the Outcome
of the wrapped Responder
.
fn respond_to(self, req: &Request) -> Result<Response<'r>, Status>
[src]
fn respond_to(self, req: &Request) -> Result<Response<'r>, Status>
Returns Ok
if a Response
could be generated successfully. Otherwise, returns an Err
with a failing Status
. Read more
impl<R: Debug> Debug for Flash<R>
[src]
impl<R: Debug> Debug for Flash<R>
Auto Trait Implementations
Blanket Implementations
impl<T, U> Into for T where
U: From<T>,
[src]
impl<T, U> Into for T where
U: From<T>,
impl<T> From for T
[src]
impl<T> From for T
impl<T, U> TryFrom for T where
T: From<U>,
[src]
impl<T, U> TryFrom for T where
T: From<U>,
type Error = !
try_from
)The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
try_from
)Performs the conversion.
impl<T> Borrow for T where
T: ?Sized,
[src]
impl<T> Borrow for T where
T: ?Sized,
impl<T> BorrowMut for T where
T: ?Sized,
[src]
impl<T> BorrowMut for T where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
impl<T, U> TryInto for T where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
try_from
)The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
try_from
)Performs the conversion.
impl<T> Any for T where
T: 'static + ?Sized,
[src]
impl<T> Any for T where
T: 'static + ?Sized,
fn get_type_id(&self) -> TypeId
[src]
fn get_type_id(&self) -> TypeId
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static
Gets the TypeId
of self
. Read more
impl<T> Typeable for T where
T: Any,
impl<T> Typeable for T where
T: Any,