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
//! Helpers for writing Alfred script filter JSON output (Alfred 3)
//!
//! # Example
//!
//! ### JSON output (Alfred 3)
//!
//! ```
//! extern crate alfred;
//!
//! use std::io::{self, Write};
//!
//! fn write_items() -> io::Result<()> {
//!     alfred::json::write_items(io::stdout(), &[
//!         alfred::Item::new("Item 1"),
//!         alfred::ItemBuilder::new("Item 2")
//!                             .subtitle("Subtitle")
//!                             .into_item(),
//!         alfred::ItemBuilder::new("Item 3")
//!                             .arg("Argument")
//!                             .subtitle("Subtitle")
//!                             .icon_filetype("public.folder")
//!                             .into_item()
//!     ])
//! }
//!
//! fn main() {
//!     match write_items() {
//!         Ok(()) => {},
//!         Err(err) => {
//!             let _ = writeln!(&mut io::stderr(), "Error writing items: {}", err);
//!         }
//!     }
//! }
//! ```

use ::{Item, ItemType, Modifier, Icon};
use rustc_serialize::json::{Json, ToJson};
use std::collections::BTreeMap;
use std::io;
use std::io::prelude::*;

/// Writes a complete JSON document representing the `Item`s to the `Write`
///
/// The `Write` is flushed after the JSON document is written.
pub fn write_items<W: Write>(w: W, items: &[Item]) -> io::Result<()> {
    let mut w = w;
    let mut root = BTreeMap::new();
    root.insert("items".to_string(), Json::Array(items.into_iter().map(ToJson::to_json).collect()));
    try!(write!(&mut w, "{}", Json::Object(root)));
    w.flush()
}

impl<'a> ToJson for Item<'a> {
    fn to_json(&self) -> Json {
        let mut d = BTreeMap::new();
        d.insert("title".to_string(), self.title.to_json());
        if let Some(ref subtitle) = self.subtitle {
            d.insert("subtitle".to_string(), subtitle.to_json());
        }
        if let Some(ref icon) = self.icon {
            d.insert("icon".to_string(), icon.to_json());
        }
        if let Some(ref uid) = self.uid {
            d.insert("uid".to_string(), uid.to_json());
        }
        if let Some(ref arg) = self.arg {
            d.insert("arg".to_string(), arg.to_json());
        }
        match self.type_ {
            ItemType::Default => {}
            ItemType::File => {
                d.insert("type".to_string(), "file".to_json());
            }
            ItemType::FileSkipCheck => {
                d.insert("type".to_string(), "file:skipcheck".to_json());
            }
        }
        if !self.valid {
            d.insert("valid".to_string(), false.to_json());
        }
        if let Some(ref autocomplete) = self.autocomplete {
            d.insert("autocomplete".to_string(), autocomplete.to_json());
        }
        if self.text_copy.is_some() || self.text_large_type.is_some() {
            let mut text = BTreeMap::new();
            if let Some(ref text_copy) = self.text_copy {
                text.insert("copy".to_string(), text_copy.to_json());
            }
            if let Some(ref text_large_type) = self.text_large_type {
                text.insert("largetype".to_string(), text_large_type.to_json());
            }
            d.insert("text".to_string(), text.to_json());
        }
        if let Some(ref url) = self.quicklook_url {
            d.insert("quicklookurl".to_string(), url.to_json());
        }
        if !self.modifiers.is_empty() {
            let mut mods = BTreeMap::new();
            for (modifier, data) in self.modifiers.iter() {
                let key = match *modifier {
                    Modifier::Command => "cmd",
                    Modifier::Option => "alt",
                    Modifier::Control => "ctrl",
                    Modifier::Shift => "shift",
                    Modifier::Fn => "fn"
                }.to_string();
                let mut mod_ = BTreeMap::new();
                if let Some(ref subtitle) = data.subtitle {
                    mod_.insert("subtitle".to_string(), subtitle.to_json());
                }
                if let Some(ref arg) = data.arg {
                    mod_.insert("arg".to_string(), arg.to_json());
                }
                if let Some(valid) = data.valid {
                    mod_.insert("valid".to_string(), valid.to_json());
                }
                mods.insert(key, mod_.to_json());
            }
            d.insert("mods".to_string(), mods.to_json());
        }
        Json::Object(d)
    }
}

impl<'a> ToJson for Icon<'a> {
    fn to_json(&self) -> Json {
        let mut d = BTreeMap::new();
        match *self {
            Icon::Path(ref s) => {
                d.insert("path".to_string(), s.to_json());
            }
            Icon::File(ref s) => {
                d.insert("type".to_string(), "fileicon".to_json());
                d.insert("path".to_string(), s.to_json());
            }
            Icon::FileType(ref s) => {
                d.insert("type".to_string(), "filetype".to_json());
                d.insert("path".to_string(), s.to_json());
            }
        }
        d.to_json()
    }
}