nustify/
notification.rs

1#[cfg(feature="imgur")]
2use {
3    crate::error::Error,
4    std::borrow::Cow
5};
6
7/// A out-going notification. May be sent multiple times with the `nustify::send` function.
8#[derive(Debug, Clone)]
9pub struct Notification {
10    pub(crate) title: Option<String>,
11    pub(crate) message: String,
12    pub(crate) extra: Option<String>,
13}
14
15impl Notification {
16    /// Create a new notification with a optional title.
17    ///
18    /// # Examples
19    ///
20    /// ```rust
21    /// let notification = Notification::new(None, "Hello from Rust".to_owned());
22    /// ```
23    pub fn new(title: Option<String>, message: String) -> Self {
24        Self {
25            title,
26            message,
27            extra: None
28        }
29    }
30}
31
32/// A builder helper used to create a simple notification or a more complex one (link, image).
33pub struct Builder(Notification);
34
35impl Builder {
36    /// Create a builder that will resolve to a notification with the provided content and an empty title.
37    pub fn new(message: String) -> Self {
38        Self(Notification::new(None, message))
39    }
40
41    /// Set the title of the notification.
42    ///
43    /// # Examples
44    /// ```rust
45    /// let notification = Builder::new("Rusty content".to_owned())
46    ///     .title("Hello from Rust".to_owned())
47    ///     .build();
48    /// ```
49    pub fn title(mut self, title: String) -> Self {
50        self.0.title = Some(title);
51        self
52    }
53
54    /// Set the `value3` of the notification.
55    ///
56    /// # Examples
57    /// ```rust
58    /// let notification = Builder::new("Rusty content".to_owned())
59    ///     .extra("https://i.imgur.com/SFmiPRo.png".to_owned())
60    ///    .build();
61    /// ```
62    pub fn extra(mut self, extra: String) -> Self {
63        self.0.extra = Some(extra);
64        self
65    }
66
67    /// Set the image URL of the notification (same as calling `extra`).
68    ///
69    /// # Examples
70    /// ```rust
71    /// let notification = Builder::new("Rusty content".to_owned())
72    ///     .image_url("https://i.imgur.com/SFmiPRo.png".to_owned())
73    ///     .build();
74    /// ```
75    pub fn image_url(self, url: String) -> Self {
76        self.extra(url)
77    }
78
79    /// Set the link that the notification will follow when tapped (same as calling `extra`).
80    ///
81    /// # Examples
82    /// ```rust
83    /// let notification = Builder::new("Rusty content".to_owned())
84    ///     .url("https://www.rust-lang.org".to_owned())
85    ///     .build();
86    /// ```
87    pub fn url(self, url: String) -> Self {
88        self.extra(url)
89    }
90
91    /// Upload an image to Imgur and use the URL returned by the API as the image URL.
92    ///
93    /// # Examples
94    /// ```rust
95    /// let image_data = std::fs::read("crab.png")?;
96    /// let notification = Builder::new("A nice uploaded image".to_owned())
97    ///     .imgur_image("MY_IMGUR_KEY", image_data).await?
98    ///     .build();
99    /// ```
100    ///
101    /// # Features
102    /// This method requires the `imgur` feature.
103    #[cfg(feature="imgur")]
104    pub async fn imgur_image<T>(self, client_id: &str, image: T) -> Result<Self, Error>
105    where
106        T: Into<Cow<'static, [u8]>>
107    {
108        Ok(
109            self.image_url(
110                crate::imgur::upload(client_id, image).await?
111            )
112        )
113    }
114
115    /// Build the notification using all the settings previously provided.
116    pub fn build(self) -> Notification {
117        self.0
118    }
119}