Skip to main content

clipboard_watcher/
formats.rs

1use crate::*;
2
3/// A struct that represents a clipboard format.
4#[derive(Debug, Clone)]
5pub struct Format {
6  pub(crate) name: Arc<str>,
7  #[cfg(not(target_os = "macos"))]
8  pub(crate) id: u32,
9  #[cfg(target_os = "macos")]
10  pub(crate) id: objc2::rc::Retained<objc2_foundation::NSString>,
11}
12
13impl Format {
14  /// Returns the name of the format
15  #[must_use]
16  #[inline]
17  pub fn name(&self) -> &str {
18    &self.name
19  }
20}
21
22/// A struct that represents the list of formats currently available on the clipboard.
23#[derive(Default, Debug)]
24pub struct Formats {
25  pub(crate) data: Vec<Format>,
26}
27
28impl FromIterator<Format> for Formats {
29  fn from_iter<T: IntoIterator<Item = Format>>(iter: T) -> Self {
30    Self {
31      data: iter.into_iter().collect(),
32    }
33  }
34}
35
36impl IntoIterator for Formats {
37  type Item = Format;
38  type IntoIter = std::vec::IntoIter<Format>;
39
40  #[inline]
41  fn into_iter(self) -> Self::IntoIter {
42    self.data.into_iter()
43  }
44}
45
46impl<'a> IntoIterator for &'a Formats {
47  type Item = &'a Format;
48  type IntoIter = std::slice::Iter<'a, Format>;
49
50  #[inline]
51  fn into_iter(self) -> Self::IntoIter {
52    self.data.iter()
53  }
54}
55
56impl Formats {
57  #[inline]
58  pub fn iter(&self) -> std::slice::Iter<'_, Format> {
59    self.data.iter()
60  }
61
62  #[cfg(not(target_os = "macos"))]
63  #[must_use]
64  #[inline]
65  pub(crate) fn contains_id(&self, id: u32) -> bool {
66    self.data.iter().any(|d| d.id == id)
67  }
68}