tide_flash/
response_ext.rs

1use crate::{Flash, FlashMessage};
2
3pub trait ResponseFlashExt {
4    fn flash(&mut self, level: &str, msg: String);
5    fn flash_success<S: Into<String>>(&mut self, msg: S);
6    fn flash_info<S: Into<String>>(&mut self, msg: S);
7    fn flash_warn<S: Into<String>>(&mut self, msg: S);
8    fn flash_error<S: Into<String>>(&mut self, msg: S);
9    fn flash_debug<S: Into<String>>(&mut self, msg: S);
10}
11
12impl ResponseFlashExt for tide::Response {
13    fn flash(&mut self, level: &str, message: String) {
14        let mut messages = if let Some(flash) = self.ext::<Flash>() {
15            flash.clone().messages
16        } else {
17            Vec::new()
18        };
19        messages.push(FlashMessage {
20            level: String::from(level),
21            message,
22        });
23        println!("{:?}", messages);
24        self.insert_ext::<Flash>(Flash { messages });
25    }
26
27    fn flash_success<S: Into<String>>(&mut self, msg: S) {
28        self.flash("success", msg.into());
29    }
30
31    fn flash_info<S: Into<String>>(&mut self, msg: S) {
32        self.flash("info", msg.into());
33    }
34
35    fn flash_warn<S: Into<String>>(&mut self, msg: S) {
36        self.flash("warn", msg.into());
37    }
38
39    fn flash_error<S: Into<String>>(&mut self, msg: S) {
40        self.flash("error", msg.into());
41    }
42
43    fn flash_debug<S: Into<String>>(&mut self, msg: S) {
44        self.flash("debug", msg.into());
45    }
46}