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
use std::{io, process::Command};

use super::ensure_success;

/// Remove a notification previously shown with id
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RemoveHandle {
  id: Option<String>,
}

impl RemoveHandle {
  #[must_use]
  pub fn new(id: Option<String>) -> Self {
    Self { id }
  }

  #[must_use]
  pub fn id(&self) -> Option<&String> {
    self.id.as_ref()
  }

  /// Runs `termux-notification-remove` command if `id` present
  ///
  /// # Errors
  ///
  /// Returns an error if command status not success
  pub fn remove(&self) -> io::Result<()> {
    if let Some(mut cmd) = self.to_command() {
      ensure_success(&cmd.output()?)?;
    }
    Ok(())
  }

  /// Builds `termux-notification-remove` command
  #[must_use]
  pub fn to_command(&self) -> Option<Command> {
    let mut cmd = Command::new("termux-notification-remove");
    cmd.arg(self.id()?);
    Some(cmd)
  }
}