1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#[cfg(feature="imgur")]
use crate::error::Error;

#[cfg(feature="imgur")]
use std::borrow::Cow;

#[derive(Debug, Clone)]
pub struct Notification {
    pub(crate) title: Option<String>,
    pub(crate) message: String,
    pub(crate) extra: Option<String>,
}

impl Notification {
    pub fn new(title: Option<String>, message: String) -> Self {
        Self {
            title,
            message,
            extra: None
        }
    }
}

pub struct Builder(Notification);

impl Builder {
    pub fn new(message: String) -> Self {
        Self(Notification::new(None, message))
    }

    pub fn title(mut self, title: String) -> Self {
        self.0.title = Some(title);
        self
    }

    pub fn extra(mut self, extra: String) -> Self {
        self.0.extra = Some(extra);
        self
    }

    pub fn image_url(self, url: String) -> Self {
        self.extra(url)
    }

    pub fn url(self, url: String) -> Self {
        self.extra(url)
    }

    #[cfg(feature="imgur")]
    pub async fn imgur_image<T>(self, client_id: &str, image: T) -> Result<Self, Error>
    where
        T: Into<Cow<'static, [u8]>>
    {
        Ok(
            self.image_url(
                crate::imgur::upload(client_id, image).await?
            )
        )
    }

    pub fn build(self) -> Notification {
        self.0
    }
}