diff --git a/core/lib/src/response/flash.rs b/core/lib/src/response/flash.rs index af26c97a..dc665458 100644 --- a/core/lib/src/response/flash.rs +++ b/core/lib/src/response/flash.rs @@ -1,5 +1,3 @@ -use std::convert::AsRef; - use time::Duration; use serde::ser::{Serialize, Serializer, SerializeStruct}; @@ -27,14 +25,14 @@ const FLASH_COOKIE_DELIM: char = ':'; /// /// # Usage /// -/// Each `Flash` message consists of a `name` and some `msg` contents. A generic -/// constructor ([new](#method.new)) can be used to construct a message with any -/// name, while the [warning](#method.warning), [success](#method.success), and +/// Each `Flash` message consists of a `kind` and `message`. A generic +/// constructor ([new](#method.new)) can be used to construct a message of any +/// kind, while the [warning](#method.warning), [success](#method.success), and /// [error](#method.error) constructors create messages with the corresponding -/// names. +/// kinds. /// /// Messages can be retrieved on the request side via the [`FlashMessage`] type -/// and the [name](#method.name) and [msg](#method.msg) methods. +/// and the [kind](#method.kind) and [message](#method.message) methods. /// /// # Response /// @@ -58,13 +56,13 @@ const FLASH_COOKIE_DELIM: char = ':'; /// if name == "special_user" { /// Ok("Hello, special user!") /// } else { -/// Err(Flash::error(Redirect::to("/"), "Invalid username.")) +/// Err(Flash::error(Redirect::to(uri!(index)), "Invalid username.")) /// } /// } /// /// #[get("/")] -/// fn index(flash: Option) -> String { -/// flash.map(|msg| format!("{}: {}", msg.name(), msg.msg())) +/// fn index(flash: Option>) -> String { +/// flash.map(|flash| format!("{}: {}", flash.kind(), flash.message())) /// .unwrap_or_else(|| "Welcome!".to_string()) /// } /// ``` @@ -77,8 +75,8 @@ const FLASH_COOKIE_DELIM: char = ':'; /// receive the standard welcome message. #[derive(Debug)] pub struct Flash { - pub name: String, - pub message: String, + kind: String, + message: String, consumed: AtomicBool, inner: R, } @@ -91,15 +89,15 @@ pub struct Flash { /// there is a flash cookie present (set by the `Flash` `Responder`), a /// `FlashMessage` request guard will succeed. /// -/// The flash cookie is cleared if either the [`name()`] or [`msg()`] method is +/// The flash cookie is cleared if either the [`kind()`] or [`message()`] method is /// called. If neither method is called, the flash cookie is not cleared. /// -/// [`name()`]: Flash::name() -/// [`msg()`]: Flash::msg() +/// [`kind()`]: Flash::kind() +/// [`message()`]: Flash::message() pub type FlashMessage<'a> = crate::response::Flash<&'a CookieJar<'a>>; impl Flash { - /// Constructs a new `Flash` message with the given `name`, `msg`, and + /// Constructs a new `Flash` message with the given `kind`, `message`, and /// underlying `responder`. /// /// # Examples @@ -111,19 +109,19 @@ impl Flash { /// use rocket::response::{Redirect, Flash}; /// /// # #[allow(unused_variables)] - /// let msg = Flash::new(Redirect::to("/"), "suggestion", "Try this out!"); + /// let message = Flash::new(Redirect::to("/"), "suggestion", "Try this out!"); /// ``` - pub fn new, M: AsRef>(res: R, name: N, msg: M) -> Flash { + pub fn new, M: Into>(res: R, kind: K, message: M) -> Flash { Flash { - name: name.as_ref().to_string(), - message: msg.as_ref().to_string(), + kind: kind.into(), + message: message.into(), consumed: AtomicBool::default(), inner: res, } } /// Constructs a "success" `Flash` message with the given `responder` and - /// `msg`. + /// `message`. /// /// # Examples /// @@ -134,14 +132,14 @@ impl Flash { /// use rocket::response::{Redirect, Flash}; /// /// # #[allow(unused_variables)] - /// let msg = Flash::success(Redirect::to("/"), "It worked!"); + /// let message = Flash::success(Redirect::to("/"), "It worked!"); /// ``` - pub fn success>(responder: R, msg: S) -> Flash { - Flash::new(responder, "success", msg) + pub fn success>(responder: R, message: S) -> Flash { + Flash::new(responder, "success", message.into()) } /// Constructs a "warning" `Flash` message with the given `responder` and - /// `msg`. + /// `message`. /// /// # Examples /// @@ -152,14 +150,14 @@ impl Flash { /// use rocket::response::{Redirect, Flash}; /// /// # #[allow(unused_variables)] - /// let msg = Flash::warning(Redirect::to("/"), "Watch out!"); + /// let message = Flash::warning(Redirect::to("/"), "Watch out!"); /// ``` - pub fn warning>(responder: R, msg: S) -> Flash { - Flash::new(responder, "warning", msg) + pub fn warning>(responder: R, message: S) -> Flash { + Flash::new(responder, "warning", message.into()) } /// Constructs an "error" `Flash` message with the given `responder` and - /// `msg`. + /// `message`. /// /// # Examples /// @@ -170,15 +168,15 @@ impl Flash { /// use rocket::response::{Redirect, Flash}; /// /// # #[allow(unused_variables)] - /// let msg = Flash::error(Redirect::to("/"), "Whoops!"); + /// let message = Flash::error(Redirect::to("/"), "Whoops!"); /// ``` - pub fn error>(responder: R, msg: S) -> Flash { - Flash::new(responder, "error", msg) + pub fn error>(responder: R, message: S) -> Flash { + Flash::new(responder, "error", message.into()) } fn cookie(&self) -> Cookie<'static> { let content = format!("{}{}{}{}", - self.name.len(), FLASH_COOKIE_DELIM, self.name, self.message); + self.kind.len(), FLASH_COOKIE_DELIM, self.kind, self.message); Cookie::build(FLASH_COOKIE_NAME, content) .max_age(Duration::minutes(5)) @@ -192,19 +190,18 @@ impl Flash { /// the response is the `Outcome` of the wrapped `Responder`. impl<'r, 'o: 'r, R: Responder<'r, 'o>> Responder<'r, 'o> for Flash { fn respond_to(self, req: &'r Request<'_>) -> response::Result<'o> { - trace_!("Flash: setting message: {}:{}", self.name, self.message); req.cookies().add(self.cookie()); self.inner.respond_to(req) } } -impl<'a> FlashMessage<'a> { +impl<'r> FlashMessage<'r> { /// Constructs a new message with the given name and message for the given /// request. - fn named<'r: 'a>(name: &str, msg: &str, req: &'a Request<'r>) -> FlashMessage<'a> { + fn named>(kind: S, message: S, req: &'r Request<'_>) -> Self { Flash { - name: name.to_string(), - message: msg.to_string(), + kind: kind.into(), + message: message.into(), consumed: AtomicBool::new(false), inner: req.cookies(), } @@ -218,14 +215,20 @@ impl<'a> FlashMessage<'a> { } } - /// Returns the `name` of this message. - pub fn name(&self) -> &str { + /// Returns a tuple of `(kind, message)`, consuming `self`. + pub fn into_inner(self) -> (String, String) { self.clear_cookie_if_needed(); - &self.name + (self.kind, self.message) } - /// Returns the `msg` contents of this message. - pub fn msg(&self) -> &str { + /// Returns the `kind` of this message. + pub fn kind(&self) -> &str { + self.clear_cookie_if_needed(); + &self.kind + } + + /// Returns the `message` contents of this message. + pub fn message(&self) -> &str { self.clear_cookie_if_needed(); &self.message } @@ -260,11 +263,11 @@ impl<'r> FromRequest<'r> for FlashMessage<'r> { } } -impl Serialize for Flash { +impl Serialize for FlashMessage<'_> { fn serialize(&self, ser: S) -> Result { let mut flash = ser.serialize_struct("Flash", 2)?; - flash.serialize_field("name", &self.name)?; - flash.serialize_field("message", &self.message)?; + flash.serialize_field("kind", self.kind())?; + flash.serialize_field("message", self.message())?; flash.end() } } diff --git a/core/lib/tests/flash-lazy-removes-issue-466.rs b/core/lib/tests/flash-lazy-removes-issue-466.rs index 7cf374a0..ff30b83a 100644 --- a/core/lib/tests/flash-lazy-removes-issue-466.rs +++ b/core/lib/tests/flash-lazy-removes-issue-466.rs @@ -17,7 +17,7 @@ fn unused(flash: Option>) -> Option<()> { #[get("/use")] fn used(flash: Option>) -> Option { - flash.map(|flash| flash.msg().into()) + flash.map(|f| f.message().into()) } mod flash_lazy_remove_tests {