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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#![warn(missing_docs)]
#![warn(unused_extern_crates)]
#![warn(unused_qualifications)]

//! Rust library for modifying macOS tags

use serde::{Deserialize, Serialize};
use std::{collections::HashSet, fmt, io, path::Path};
use thiserror::Error;

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
/// Represents a macOS tag
pub enum Tag {
    /// Gray tag color
    Gray,
    /// Green tag color
    Green,
    /// Purple tag color
    Purple,
    /// Blue tag color
    Blue,
    /// Yellow tag color
    Yellow,
    /// Red tag color
    Red,
    /// Orange tag color
    Orange,
    /// Custom tag name (uncolored)
    Custom(String),
}

#[derive(Debug, Error)]
/// Represents an error that can occur when working with tags
pub enum TagError {
    /// Error when working with extended attributes
    #[error("xattr operation failed")]
    XAttr(#[from] io::Error),
    /// Error when working with plist data
    #[error("plist operation failed")]
    Plist(#[from] plist::Error),
    /// Error when user-provided data is invalid
    #[error("tag metadata for `{0}` is invalid")]
    Invalid(String),
    /// Unknown error
    #[error("unknown error")]
    Unknown,
}

impl fmt::Display for Tag {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Gray => write!(f, "Gray\n1"),
            Self::Green => write!(f, "Green\n2"),
            Self::Purple => write!(f, "Purple\n3"),
            Self::Blue => write!(f, "Blue\n4"),
            Self::Yellow => write!(f, "Yellow\n5"),
            Self::Red => write!(f, "Red\n6"),
            Self::Orange => write!(f, "Orange\n7"),
            Self::Custom(val) => write!(f, "{}", val),
        }
    }
}

impl Tag {
    /// Converts a `String` into a `Tag`
    pub fn from_string(s: &str) -> Result<Self, TagError> {
        if s.contains('\n') {
            let tag = s
                .split_once('\n')
                .ok_or_else(|| TagError::Invalid(s.to_owned()))?;

            match tag {
                ("Gray", "1") => return Ok(Tag::Gray),
                ("Green", "2") => return Ok(Tag::Green),
                ("Purple", "3") => return Ok(Tag::Purple),
                ("Blue", "4") => return Ok(Tag::Blue),
                ("Yellow", "5") => return Ok(Tag::Yellow),
                ("Red", "6") => return Ok(Tag::Red),
                ("Orange", "7") => return Ok(Tag::Orange),
                _ => return Err(TagError::Invalid(s.to_owned())),
            }
        }

        Ok(Tag::Custom(s.to_string()))
    }
}

/// Adds a tag for provided file
///
/// # Examples
///
/// ```rust
/// use std::path::Path;
/// use macos_tags::{add_tag, Tag};
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let p = Path::new("./readme.md");
///     add_tag(p, Tag::Green)?;
///     Ok(())
/// }
/// ```
pub fn add_tag(path: &Path, tag: Tag) -> Result<HashSet<Tag>, TagError> {
    let tag_metadata =
        xattr::get(path, "com.apple.metadata:_kMDItemUserTags").map_err(TagError::XAttr)?;

    let parsed_tags = match tag_metadata {
        Some(t) => plist::from_bytes::<plist::Value>(&t).map_err(TagError::Plist)?,
        None => plist::Value::Array(vec![]),
    };

    match parsed_tags {
        plist::Value::Array(t) => {
            // Converting into HashSet because `dedup` doesn't seem to work
            let mut existing_tag_set = t.iter().fold(HashSet::new(), |mut acc, x| {
                if let plist::Value::String(s) = x {
                    acc.insert(s.to_owned());
                }
                acc
            });

            existing_tag_set.insert(tag.to_string());

            let tags_to_set = &existing_tag_set
                .iter()
                .map(|t| plist::Value::String(t.to_string()))
                .collect::<Vec<_>>();

            let final_tag_set = existing_tag_set
                .iter()
                .map(|t| Tag::from_string(t))
                .collect::<Result<HashSet<_>, TagError>>()?;

            let mut binary_buffer: Vec<u8> = vec![];
            plist::to_writer_binary(&mut binary_buffer, &tags_to_set).map_err(TagError::Plist)?;
            xattr::set(path, "com.apple.metadata:_kMDItemUserTags", &binary_buffer)
                .map_err(TagError::XAttr)?;
            Ok(final_tag_set)
        }
        _ => Err(TagError::Unknown),
    }
}

/// Sets all tags for provided file
///
/// # Examples
///
/// ```rust
/// use std::path::Path;
/// use macos_tags::{set_tags, Tag};
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let p = Path::new("./readme.md");
///     set_tags(p, [Tag::Green, Tag::Red].into())?;
///     Ok(())
/// }
/// ```
pub fn set_tags(path: &Path, tags: HashSet<Tag>) -> Result<HashSet<Tag>, TagError> {
    let tags_to_set = &tags
        .iter()
        .map(|t| plist::Value::String(t.to_string()))
        .collect::<Vec<_>>();

    let mut binary_buffer: Vec<u8> = vec![];
    plist::to_writer_binary(&mut binary_buffer, &tags_to_set).map_err(TagError::Plist)?;
    xattr::set(path, "com.apple.metadata:_kMDItemUserTags", &binary_buffer)
        .map_err(TagError::XAttr)?;

    Ok(tags)
}

/// Removes a tag for provided file
///
/// # Examples
///
/// ```rust
/// use std::path::Path;
/// use macos_tags::{remove_tag, Tag};
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let p = Path::new("./readme.md");
///     remove_tag(p, Tag::Green)?;
///     Ok(())
/// }
/// ```
pub fn remove_tag(path: &Path, tag: Tag) -> Result<HashSet<Tag>, TagError> {
    let tag_metadata =
        xattr::get(path, "com.apple.metadata:_kMDItemUserTags").map_err(TagError::XAttr)?;

    let parsed_tags = match tag_metadata {
        Some(t) => plist::from_bytes::<plist::Value>(&t).map_err(TagError::Plist)?,
        None => plist::Value::Array(vec![]),
    };

    match parsed_tags {
        plist::Value::Array(t) => {
            // Converting into HashSet because `dedup` doesn't seem to work
            let mut existing_tag_set = t.iter().fold(HashSet::new(), |mut acc, x| {
                if let plist::Value::String(s) = x {
                    acc.insert(s.to_owned());
                }
                acc
            });

            existing_tag_set.remove(&tag.to_string());

            let tags_to_set = &existing_tag_set
                .iter()
                .map(|t| plist::Value::String(t.to_string()))
                .collect::<Vec<_>>();

            let final_tag_set = existing_tag_set
                .iter()
                .map(|t| Tag::from_string(t))
                .collect::<Result<HashSet<_>, TagError>>()?;

            let mut binary_buffer: Vec<u8> = vec![];
            plist::to_writer_binary(&mut binary_buffer, &tags_to_set).map_err(TagError::Plist)?;
            xattr::set(path, "com.apple.metadata:_kMDItemUserTags", &binary_buffer)
                .map_err(TagError::XAttr)?;
            Ok(final_tag_set)
        }
        _ => Err(TagError::Unknown),
    }
}

/// Prunes all tags for provided file
///
/// # Examples
///
/// ```rust
/// use std::path::Path;
/// use macos_tags::{prune_tags, Tag};
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let p = Path::new("./readme.md");
///     prune_tags(p)?;
///     Ok(())
/// }
/// ```
pub fn prune_tags(path: &Path) -> Result<HashSet<Tag>, TagError> {
    xattr::remove(path, "com.apple.metadata:_kMDItemUserTags").map_err(TagError::XAttr)?;
    Ok(HashSet::<Tag>::with_capacity(0))
}

/// Reads tags for provided file
///
/// # Examples
///
/// ```rust
/// use std::path::Path;
/// use macos_tags::{read_tags, Tag};
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let p = Path::new("./readme.md");
///     read_tags(p)?;
///     Ok(())
/// }
/// ```
pub fn read_tags(path: &Path) -> Result<HashSet<Tag>, TagError> {
    let tag_metadata =
        xattr::get(path, "com.apple.metadata:_kMDItemUserTags").map_err(TagError::XAttr)?;

    let existing_tags = match tag_metadata {
        Some(t) => plist::from_bytes::<plist::Value>(&t).map_err(TagError::Plist)?,
        None => plist::Value::Array(vec![]),
    };

    match existing_tags {
        plist::Value::Array(t) => {
            let parsed_tags: HashSet<Tag> = t
                .iter()
                .filter_map(|t| {
                    if let plist::Value::String(s) = t {
                        let tag = Tag::from_string(s)
                            .map_err(|_| TagError::Invalid(s.to_owned()))
                            .ok()?;
                        Some(tag)
                    } else {
                        None
                    }
                })
                .collect::<HashSet<Tag>>();

            Ok(parsed_tags)
        }
        _ => Err(TagError::Unknown),
    }
}