misskey_util/builder/
misc.rs

1#[cfg(feature = "12-27-0")]
2use crate::Error;
3
4#[cfg(feature = "12-27-0")]
5use misskey_api::endpoint;
6#[cfg(feature = "12-27-0")]
7use misskey_core::Client;
8#[cfg(feature = "12-27-0")]
9use url::Url;
10
11/// Builder for the [`build_notification`][`crate::ClientExt::build_notification`] method.
12#[cfg(feature = "12-27-0")]
13#[cfg_attr(docsrs, doc(cfg(feature = "12-27-0")))]
14pub struct NotificationBuilder<C> {
15    client: C,
16    request: endpoint::notifications::create::Request,
17}
18
19#[cfg(feature = "12-27-0")]
20#[cfg_attr(docsrs, doc(cfg(feature = "12-27-0")))]
21impl<C> NotificationBuilder<C> {
22    /// Creates a builder with the client.
23    pub fn new(client: C) -> Self {
24        let request = endpoint::notifications::create::Request {
25            body: String::new(),
26            header: None,
27            icon: None,
28        };
29        NotificationBuilder { client, request }
30    }
31
32    /// Gets the request object for reuse.
33    pub fn as_request(&self) -> &endpoint::notifications::create::Request {
34        &self.request
35    }
36
37    /// Sets the body text of the notification.
38    pub fn body(&mut self, body: impl Into<String>) -> &mut Self {
39        self.request.body = body.into();
40        self
41    }
42
43    /// Sets the header text of the notification.
44    pub fn header(&mut self, header: impl Into<String>) -> &mut Self {
45        self.request.header.replace(header.into());
46        self
47    }
48
49    /// Sets the icon of the notification.
50    pub fn icon(&mut self, icon: Url) -> &mut Self {
51        self.request.icon.replace(icon);
52        self
53    }
54}
55
56#[cfg(feature = "12-27-0")]
57#[cfg_attr(docsrs, doc(cfg(feature = "12-27-0")))]
58impl<C: Client> NotificationBuilder<C> {
59    /// Creates the notification.
60    pub async fn create(&self) -> Result<(), Error<C::Error>> {
61        self.client
62            .request(&self.request)
63            .await
64            .map_err(Error::Client)?
65            .into_result()?;
66        Ok(())
67    }
68}