termux_notification/
remove_handle.rs

1use std::{io, process::Command};
2
3use super::ensure_success;
4
5/// Remove a notification previously shown with id
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub struct RemoveHandle {
8  id: Option<String>,
9}
10
11impl RemoveHandle {
12  #[must_use]
13  pub fn new(id: Option<String>) -> Self {
14    Self { id }
15  }
16
17  #[must_use]
18  pub fn id(&self) -> Option<&String> {
19    self.id.as_ref()
20  }
21
22  /// Runs `termux-notification-remove` command if `id` present
23  ///
24  /// # Errors
25  ///
26  /// Returns an error if command status not success
27  pub fn remove(&self) -> io::Result<()> {
28    if let Some(mut cmd) = self.to_command() {
29      ensure_success(&cmd.output()?)?;
30    }
31    Ok(())
32  }
33
34  /// Builds `termux-notification-remove` command
35  #[must_use]
36  pub fn to_command(&self) -> Option<Command> {
37    let mut cmd = Command::new("termux-notification-remove");
38    cmd.arg(self.id()?);
39    Some(cmd)
40  }
41}