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
// Copyright (C) 2020 Michael Herstine <sp1ff@pobox.com>
//
// This file is part of pin.
//
// pin is free software: you can redistribute it and/or modify it under the terms of the GNU General
// Public License as published by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// pin is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
// Public License for more details.
//
// You should have received a copy of the GNU General Public License along with pin.  If not, see
// <http://www.gnu.org/licenses/>.

//! pin -- A command-line client for Pinboard & Instapaper

pub mod error_from;
pub mod instapaper;
pub mod pinboard;
pub mod vars;

use instapaper::Instapaper;
use pinboard::Pinboard;

use serde::Deserialize;
use snafu::{Backtrace, GenerateBacktrace, Snafu};
use strfmt::strfmt;

use std::cmp::max;
use std::collections::HashMap;

////////////////////////////////////////////////////////////////////////////////////////////////////

#[derive(Debug, Snafu)]
pub enum Error {
    #[snafu(display("{}", cause))]
    Other {
        #[snafu(source(true))]
        cause: Box<dyn std::error::Error>,
        #[snafu(backtrace(true))]
        back: Backtrace,
    },
}

error_from!(instapaper::Error);
error_from!(pinboard::Error);
error_from!(std::io::Error);

pub type Result<T> = std::result::Result<T, Error>;

////////////////////////////////////////////////////////////////////////////////////////////////////
//                                      `pin' Configuration                                       //
////////////////////////////////////////////////////////////////////////////////////////////////////

/// A Target is a pre-defined set of Pinboard tags together with the "read only" flag; i.e. a
/// pre-configured "place" at Pinboard to which a link may be sent
#[derive(Debug, Deserialize)]
pub struct Target {
    pub read_later: bool,
    pub send_to_insty: bool,
    pub tags: Vec<String>,
}

/// Application configuration; by default read from ~/.pin (but that location can be overriden
/// on the command-line), but also assembled from command-line option & the environment.
#[derive(Debug, Deserialize)]
pub struct Config {
    /// Pinboard API token
    #[serde(default)]
    pub token: String,
    /// Predefined Pinboard tagsets to apply to links
    #[serde(default)]
    pub targets: HashMap<String, Target>,
    /// Instapaper username
    #[serde(default)]
    pub username: String,
    /// Instapaper password
    #[serde(default)]
    pub password: String,
}

impl Config {
    pub fn new() -> Config {
        Config {
            token: String::from(""),
            targets: HashMap::new(),
            username: String::from(""),
            password: String::from(""),
        }
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////

/// Get all Pinboard.in tags & pretty-print 'em
pub fn get_tags<W: std::io::Write, C: pinboard::Pinboard>(
    out: &mut W,
    client: &mut C,
    alpha: bool,
    desc: bool,
    csv: bool,
) -> Result<()> {
    let mut tags = client.get_all_tags()?;
    let max_lens = match csv {
        true => None,
        false => {
            let (mut max_tag, mut max_count) = (0, 0);
            for (tag, count) in &tags {
                if tag.len() > max_tag {
                    max_tag = tag.len();
                }
                if *count > max_count {
                    max_count = *count;
                }
            }
            Some((max_tag, max(9, (max_count as f64).log10() as usize + 1)))
        }
    };
    if alpha {
        tags.sort_by(|lhs, rhs| lhs.0.cmp(&rhs.0));
    } else {
        tags.sort_by(|lhs, rhs| lhs.1.cmp(&rhs.1));
    }
    if desc {
        tags.reverse();
    }

    match max_lens {
        Some((max_tag_len, max_use_count)) => {
            // We're pretty-printing. This is the first time Rust has disappointed me: macros
            // like `format!` and `writeln!` require that the first parameter be a string
            // literal.  This forces me to use some sort of templating crate. The ones at which
            // I glanced seem heavy-weight & aimed at HTML generation. `strfmt', OTOH, seems
            // _too_ basic.

            let rule = format!(
                "+{}+{}+",
                String::from_utf8(vec![b'-'; max_tag_len + 2]).unwrap(),
                String::from_utf8(vec![b'-'; max_use_count + 2]).unwrap()
            );

            let mut fmtvars: HashMap<String, usize> = HashMap::new();
            fmtvars.insert(String::from("1"), max_tag_len);
            fmtvars.insert(String::from("2"), max_use_count);
            let fmt = strfmt("| {{tag:<{1}}} | {{uc:{2}}} |", &fmtvars).unwrap();

            let mut hdrvars: HashMap<String, &str> = HashMap::new();
            hdrvars.insert(String::from("tag"), "Tag");
            hdrvars.insert(String::from("uc"), "Use Count");
            writeln!(out, "{}", strfmt(&fmt, &hdrvars).unwrap())?;

            writeln!(out, "{}", rule)?;

            for tag in &tags {
                let s = format!("{}", tag.1);
                let mut vars: HashMap<String, &str> = HashMap::new();
                vars.insert(String::from("tag"), &tag.0);
                vars.insert(String::from("uc"), &s);
                writeln!(out, "{}", strfmt(&fmt, &vars).unwrap())?;
            }

            writeln!(out, "{}", rule)?;
        }
        None => {
            // We're printing in CSV
            for tag in &tags {
                writeln!(out, "{},{}", tag.0, tag.1)?;
            }
        }
    }

    Ok(())
}

#[cfg(test)]
mod tests {

    use super::*;

    struct MockPins {
        tags: std::vec::Vec<(String, usize)>,
    }

    impl MockPins {
        pub fn new(tags: std::vec::Vec<(String, usize)>) -> MockPins {
            MockPins { tags: tags }
        }
    }

    impl crate::pinboard::Pinboard for MockPins {
        fn get_all_tags(&self) -> crate::pinboard::Result<std::vec::Vec<(String, usize)>> {
            Ok(self.tags.clone())
        }
        fn rename_tag(&mut self, old: &str, new: &str) -> crate::pinboard::Result<String> {
            let oldidx = match self.tags.iter().position(|x| x.0 == old) {
                None => return Ok("done".to_owned()),
                Some(oldidx) => oldidx,
            };
            match self.tags.iter().position(|x| x.0 == new) {
                Some(newidx) => {
                    self.tags[newidx].1 += self.tags[oldidx].1;
                    self.tags.remove(oldidx);
                }
                None => self.tags[oldidx].0 = String::from(new),
            }

            return Ok("done".to_owned());
        }
        fn send<I: Iterator<Item = String>>(
            &mut self,
            _url: &str,
            _title: &str,
            _tags: I,
            _rl: bool,
        ) -> crate::pinboard::Result<String> {
            return Ok("done".to_owned());
        }
    }

    #[test]
    fn get_tags_smoke_test_num_asc_pp() {
        let mut buf: Vec<u8> = vec![]; // implements Write
        let mut pins = MockPins::new(vec![(String::from("foo"), 1), (String::from("bar"), 2)]);
        let result = get_tags(&mut buf, &mut pins, false, false, false);

        assert!(result.is_ok());

        let golden = r#"| Tag | Use Count |
+-----+-----------+
| foo |         1 |
| bar |         2 |
+-----+-----------+
"#;
        assert!(golden == std::str::from_utf8(&buf).unwrap());
    }

    #[test]
    fn get_tags_smoke_test_num_asc_csv() {
        let mut buf: Vec<u8> = vec![]; // implements Write
        let mut pins = MockPins::new(vec![(String::from("foo"), 1), (String::from("bar"), 2)]);
        let result = get_tags(&mut buf, &mut pins, false, false, true);

        assert!(result.is_ok());

        let golden = r#"foo,1
bar,2
"#;
        assert!(golden == std::str::from_utf8(&buf).unwrap());
    }

    #[test]
    fn get_tags_smoke_test_num_desc_pp() {
        let mut buf: Vec<u8> = vec![]; // implements Write
        let mut pins = MockPins::new(vec![(String::from("foo"), 1), (String::from("bar"), 2)]);
        let result = get_tags(&mut buf, &mut pins, false, true, false);

        assert!(result.is_ok());

        let golden = r#"| Tag | Use Count |
+-----+-----------+
| bar |         2 |
| foo |         1 |
+-----+-----------+
"#;
        assert!(golden == std::str::from_utf8(&buf).unwrap());
    }

    #[test]
    fn get_tags_smoke_test_num_desc_csv() {
        let mut buf: Vec<u8> = vec![]; // implements Write
        let mut pins = MockPins::new(vec![(String::from("foo"), 1), (String::from("bar"), 2)]);
        let result = get_tags(&mut buf, &mut pins, false, true, true);

        assert!(result.is_ok());

        let golden = r#"bar,2
foo,1
"#;
        assert!(golden == std::str::from_utf8(&buf).unwrap());
    }
} // End module `test'.

/// Rename a tag
pub fn rename_tag<W: std::io::Write>(
    out: &mut W,
    client: &mut pinboard::Client,
    from: &str,
    to: &str,
) -> Result<()> {
    Ok(writeln!(out, "{}", client.rename_tag(from, to)?)?)
}

/// Send a link
pub fn send_link<W: std::io::Write, I: Iterator<Item = String>>(
    _out: &mut W,
    pin_client: &mut pinboard::Client,
    insta_client: Option<&mut instapaper::Client>,
    url: &str,
    title: &str,
    read_later: bool,
    tags: I,
) -> Result<()> {
    pin_client.send(url, title, tags, read_later)?;
    if let Some(cli) = insta_client {
        cli.send(url, Some(title))?;
    }
    Ok(())
}