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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
//! ⚡ Supercharge your Alfred workflows by building them in Rust!
//!
//! # Introduction
//!
//! This crate provides types for developing script filter Alfred workflows in
//! Rust. Additionally, this project includes the `powerpack-cli` crate which
//! contains a command-line tool to help build and install your workflows.
//!
//! Types in this crate closely mirror the script filter JSON format. View the
//! official documentation for that [here][fmt].
//!
//! [fmt]: https://www.alfredapp.com/help/workflows/inputs/script-filter/json/
//!
//! # Examples
//!
//! Each row in an Alfred script filter result is represented by an [`Item`]. A
//! workflow must output a sequence of items to stdout using the [`output()`]
//! function.
//!
//! ```
//! use std::iter;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let item = powerpack::Item::new("Example title")
//!     .subtitle("example subtitle")
//!     .arg("example");
//!
//! powerpack::output(iter::once(item))?;
//! # Ok(())
//! # }
//! ```

pub mod env;

use std::collections::HashMap;
use std::io;
use std::path::PathBuf;

use serde::ser::SerializeStruct;
use serde::{Serialize, Serializer};

fn is_default<T: Default + PartialEq>(t: &T) -> bool {
    t == &T::default()
}

////////////////////////////////////////////////////////////////////////////////
// Definitions
////////////////////////////////////////////////////////////////////////////////

/// Alias for a lean clone-on-write string.
pub type String<'a> = beef::lean::Cow<'a, str>;

/// A keyboard modifier.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize)]
pub enum ModifierKey {
    /// ⌘
    #[serde(rename = "cmd")]
    Command,
    /// ⌥
    #[serde(rename = "alt")]
    Option,
    /// ⌃
    #[serde(rename = "ctrl")]
    Control,
    /// ⇧
    #[serde(rename = "shift")]
    Shift,
    /// fn
    #[serde(rename = "fn")]
    Function,
}

/// An icon displayed in the result row.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Icon<'a> {
    /// Load an image from a path.
    Path(PathBuf),
    /// Extract the icon from a file.
    File(PathBuf),
    /// Uniform Type Identifier (UTI) icon.
    FileType(String<'a>),
}

/// The type of item.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize)]
pub enum Kind {
    #[serde(rename = "default")]
    Default,
    #[serde(rename = "file")]
    File,
    #[serde(rename = "file:skipcheck")]
    FileSkipCheck,
}

/// The copied or large type text.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize)]
pub struct Text<'a> {
    /// Defines the text the user will get when copying the item (⌘+C).
    copy: Option<String<'a>>,

    /// Defines the text the user will see in large type (⌘+L).
    #[serde(rename = "largetype")]
    large_type: Option<String<'a>>,
}

/// The settings for when a modifier key is pressed.
#[derive(Debug, Clone, Default, PartialEq, Eq, Hash, Serialize)]
pub struct ModifierData<'a> {
    /// The subtitle displayed in the result row.
    #[serde(skip_serializing_if = "Option::is_none")]
    subtitle: Option<String<'a>>,

    /// The argument which is passed through to the output.
    #[serde(skip_serializing_if = "Option::is_none")]
    arg: Option<String<'a>>,

    /// The icon displayed in the result row when the modifier is pressed.
    #[serde(skip_serializing_if = "Option::is_none")]
    icon: Option<Icon<'a>>,

    /// Mark whether the item is valid when the modifier is pressed.
    #[serde(skip_serializing_if = "Option::is_none")]
    valid: Option<bool>,
}

/// An Alfred script filter item.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
pub struct Item<'a> {
    /// The title displayed in the result row.
    title: String<'a>,

    /// The subtitle displayed in the result row.
    #[serde(skip_serializing_if = "Option::is_none")]
    subtitle: Option<String<'a>>,

    /// A unique identifier for the item.
    #[serde(skip_serializing_if = "Option::is_none")]
    uid: Option<String<'a>>,

    /// The argument which is passed through to the output.
    #[serde(skip_serializing_if = "Option::is_none")]
    arg: Option<String<'a>>,

    /// The icon displayed in the result row.
    #[serde(skip_serializing_if = "Option::is_none")]
    icon: Option<Icon<'a>>,

    /// Whether this item is valid or not.
    #[serde(skip_serializing_if = "Option::is_none")]
    valid: Option<bool>,

    /// Enables you to define what Alfred matches against.
    #[serde(rename = "match", skip_serializing_if = "Option::is_none")]
    matches: Option<String<'a>>,

    /// Populates the search field when the user auto-completes the result.
    #[serde(skip_serializing_if = "Option::is_none")]
    autocomplete: Option<String<'a>>,

    /// The type of item.
    #[serde(rename = "type", skip_serializing_if = "is_default")]
    kind: Kind,

    /// Control how the modifier keys react.
    #[serde(rename = "mods", skip_serializing_if = "HashMap::is_empty")]
    modifiers: HashMap<ModifierKey, ModifierData<'a>>,

    /// Defines the copied or large type text for this item.
    #[serde(skip_serializing_if = "Option::is_none")]
    text: Option<Text<'a>>,

    /// A Quick Look URL which will be shown if the user uses Quick Look (⌘+Y).
    #[serde(rename = "quicklookurl", skip_serializing_if = "Option::is_none")]
    quicklook_url: Option<String<'a>>,
}

/// The output of a workflow (i.e. input for the script filter)
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
pub struct Output<'a> {
    /// Each row item.
    items: Vec<Item<'a>>,
}

////////////////////////////////////////////////////////////////////////////////
// Implementations
////////////////////////////////////////////////////////////////////////////////

impl Serialize for Icon<'_> {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        match self {
            Self::Path(path) => {
                let mut s = serializer.serialize_struct("Icon", 1)?;
                s.serialize_field("path", &path)?;
                s.end()
            }
            Self::File(path) => {
                let mut s = serializer.serialize_struct("Icon", 2)?;
                s.serialize_field("type", "fileicon")?;
                s.serialize_field("path", &path)?;
                s.end()
            }
            Self::FileType(string) => {
                let mut s = serializer.serialize_struct("Icon", 2)?;
                s.serialize_field("type", "filetype")?;
                s.serialize_field("path", &string)?;
                s.end()
            }
        }
    }
}

impl<'a> Icon<'a> {
    pub fn new(path: impl Into<PathBuf>) -> Self {
        Self::Path(path.into())
    }

    pub fn from_file(path: impl Into<PathBuf>) -> Self {
        Self::File(path.into())
    }

    pub fn from_file_type(path: impl Into<String<'a>>) -> Self {
        Self::FileType(path.into())
    }
}

impl Default for Kind {
    fn default() -> Self {
        Self::Default
    }
}

macro_rules! setter {
    ($name:ident) => {
        setter! { $name, Option<String<'a>> }
    };
    ($name:ident, Option<$ty:ty>) => {
        pub fn $name(mut self, value: impl Into<$ty>) -> Self {
            self.$name = Some(value.into());
            self
        }
    };
    ($name:ident, $ty:ty) => {
        pub fn $name(mut self, value: impl Into<$ty>) -> Self {
            self.$name = value.into();
            self
        }
    };
}

impl<'a> ModifierData<'a> {
    /// Create a new modifier data.
    ///
    /// # Examples
    ///
    /// ```
    /// # use powerpack::ModifierData;
    /// let data = ModifierData::new();
    /// ```
    pub fn new() -> Self {
        Self::default()
    }

    setter! { subtitle }
    setter! { arg }
    setter! { icon, Option<Icon<'a>> }
    setter! { valid, Option<bool> }
}

impl<'a> Item<'a> {
    /// Create a new item.
    ///
    /// # Examples
    ///
    /// ```
    /// # use powerpack::Item;
    /// let item = Item::new("something");
    /// ```
    pub fn new(title: impl Into<String<'a>>) -> Self {
        Self {
            title: title.into(),
            ..Self::default()
        }
    }

    setter! { subtitle }
    setter! { uid }
    setter! { arg }
    setter! { icon, Option<Icon<'a>> }
    setter! { valid, Option<bool> }
    setter! { matches }
    setter! { autocomplete }
    setter! { kind, Kind }
    setter! { text, Option<Text<'a>> }
    setter! { quicklook_url }

    pub fn modifier(mut self, key: ModifierKey, data: ModifierData<'a>) -> Self {
        self.modifiers.insert(key, data);
        self
    }
}

impl<'a> Output<'a> {
    pub fn items<I>(mut self, iter: I) -> Self
    where
        I: IntoIterator<Item = Item<'a>>,
    {
        self.items = iter.into_iter().collect();
        self
    }

    pub fn write<W: io::Write>(&self, w: W) -> serde_json::Result<()> {
        serde_json::to_writer(w, self)
    }
}

/// Shortcut function to output a list of items to stdout.
pub fn output<'a, I>(items: I) -> serde_json::Result<()>
where
    I: IntoIterator<Item = Item<'a>>,
{
    Output::default().items(items).write(io::stdout())
}