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
use std::error::Error;
use std::fs::File;
use std::io::BufReader;
use std::path::Path;
use std::str::FromStr;

use clap::{
    crate_authors, crate_description, crate_name, crate_version, App, Arg, ArgGroup, ArgMatches,
};
use serde::Deserialize;
use term_table::{row::Row, table_cell::TableCell, Table, TableStyle};

#[derive(Deserialize, Debug, Hash, Eq, PartialEq)]
pub struct Show {
    #[serde(alias = "mal_id")]
    pub id: usize,
    pub title: String,
    pub url: Option<String>,
    #[serde(default)]
    pub opening_themes: Vec<String>,
    #[serde(default)]
    pub ending_themes: Vec<String>,
    #[serde(default, alias = "soundtrack")]
    pub other_soundtrack: Vec<String>,
}

pub enum OutputMode {
    Table,
    Readable,
    CSV,
}

impl OutputMode {
    pub fn from_matches(matches: &ArgMatches) -> Self {
        if matches.is_present("table") {
            Self::Table
        } else if matches.is_present("readable") {
            Self::Readable
        } else if matches.is_present("csv") {
            Self::CSV
        } else {
            Self::Readable
        }
    }
}

pub fn create_clap_app<'a>() -> App<'a, 'a> {
    App::new(crate_name!())
        .about(crate_description!())
        .author(crate_authors!())
        .version(crate_version!())
        .args(&[
            Arg::with_name("dictionary")
                .help("The list of all known shows")
                .takes_value(true)
                .short("d")
                // .long("dictionary")
                .required(true),
            Arg::with_name("list")
                .help("The subset of shows to choose from the dictionary")
                .takes_value(true)
                .short("l")
                // .long("list")
                .required(true),
            Arg::with_name("number")
                .help("The number of results to output")
                .long_help(
"The number of results to output
Note: The program is not guarranteed to output the number of results specified if it is not possible with the provided inputs."
                )
                .takes_value(true)
                .short("n")
                .index(1)
                .required(true)
                .validator(pos_int_validate),
            Arg::with_name("hard-fail")
                .help("Exit with exit code 1 on any error")
                .long_help(
"Exit with exit code 1 on any error
Note: this will not necessarily prevent some output from reaching stdout before exiting."
                )
                .long("hard-fail"),
        ])
        // Logging arguments
        .args(&[
            Arg::with_name("verbosity")
                .short("v")
                .multiple(true)
                .help("Increase message verbosity"),
            Arg::with_name("quiet")
                .short("q")
                .long("quiet")
                .help("Silence all output"),
            Arg::with_name("timestamp")
                .long("timestamp")
                .help("Prepend log lines with a timestamp")
                .takes_value(true)
                .possible_values(&["none", "sec", "ms", "ns"]),
        ])
        // Output format arguments
        .args(&[
            Arg::with_name("table")
                .help("Sets output to a formatted table")
                .short("t")
                .long("table"),
            Arg::with_name("table width")
                .help("The number of results to show")
                .takes_value(true)
                .long("table-width")
                .requires("table")
                .validator(pos_int_validate),
            Arg::with_name("readable")
                .help("Sets output to human readable text")
                .long("readable"),
            Arg::with_name("csv").help("Sets output to csv").long("csv"),
        ])
        .group(ArgGroup::with_name("display").args(&["table", "readable", "csv"]))
}

pub fn set_up_logging(matches: &ArgMatches) {
    let verbose = matches.occurrences_of("verbosity") as usize;
    let quiet = matches.is_present("quiet");
    let ts = matches
        .value_of("timestamp")
        .map(|v| {
            stderrlog::Timestamp::from_str(v).unwrap_or_else(|_| {
                clap::Error {
                    message: "invalid value for 'timestamp'".into(),
                    kind: clap::ErrorKind::InvalidValue,
                    info: None,
                }
                .exit()
            })
        })
        .unwrap_or(stderrlog::Timestamp::Off);

    stderrlog::new()
        .module(module_path!())
        .quiet(quiet)
        .verbosity(verbose + 1) // change verbosity with no -v to warn
        .timestamp(ts)
        .init()
        .unwrap()
}

pub fn read_json_file<P, T>(path: P) -> Result<T, Box<dyn Error>>
where
    P: AsRef<Path>,
    for<'de> T: Deserialize<'de>,
{
    // Open the file in read-only mode with buffer.
    let file = File::open(path)?;
    let reader = BufReader::new(file);

    // Read the JSON contents of the file as an instance of T
    let result = serde_json::from_reader(reader)?;

    // Return the `User`.
    Ok(result)
}

/// Checks if the value can be parsed as a positive, non-zero integer
fn pos_int_validate(value: String) -> Result<(), String> {
    let error_msg = "must be a positive, non-zero integer";
    let value = value.parse::<usize>().map_err(|_| error_msg.to_owned())?;
    if value == 0 {
        Err(error_msg.to_owned())
    } else {
        Ok(())
    }
}

pub fn create_table<'a>(matches: &'a ArgMatches) -> Table<'a> {
    let mut table = Table::new();

    use terminal_size::{terminal_size, Height, Width};
    let width = matches
        .value_of("table width")
        .map(|s| (Width(s.parse().unwrap()), Height(20)))
        .unwrap_or(terminal_size().unwrap_or((Width(60), Height(20))));
    let (Width(width), _) = width;
    table.max_column_width = width as _;

    // Set table style (hardcoded)
    // Note: should this option be exposed to users?
    table.style = TableStyle::rounded();

    table
}

pub fn output_theme(
    choice: &String,
    show: &Show,
    output_mode: &OutputMode,
    table: &mut Option<Table>,
) -> Result<(), Box<dyn Error>> {
    let song_type = if show.opening_themes.contains(choice) {
        "OP"
    } else if show.ending_themes.contains(choice) {
        "ED"
    } else {
        "ST"
    };

    match output_mode {
        OutputMode::Table => {
            // Unwrap is ok if we know it definetly exists
            table.as_mut().unwrap().add_row(Row::new(vec![
                TableCell::new(choice),
                TableCell::new(&show.title),
                TableCell::new(song_type),
            ]));
        }
        OutputMode::Readable => {
            println!("{} [{}] from {}", choice, song_type, show.title);
        }
        OutputMode::CSV => {
            let mut wtr = csv::Writer::from_writer(std::io::stdout());
            wtr.write_record(&[choice, song_type, &show.title])?;
            wtr.flush()?;
        }
    }

    Ok(())
}

/// Appends `other` to `first` if `other` is not empty
pub fn smart_append<T: Clone>(first: &mut Vec<T>, other: &Vec<T>) {
    if !other.is_empty() {
        first.append(&mut other.clone());
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn pos_int_validation() {
        assert!(pos_int_validate("1".to_owned()).is_ok());
        assert!(pos_int_validate("99".to_owned()).is_ok());
        assert!(pos_int_validate("-2".to_owned()).is_err());
        assert!(pos_int_validate("0".to_owned()).is_err());
    }

    fn smart_appending_template<T: Clone>(
        a: T,
        b: T,
        c: T,
        d: T,
        e: T,
        f: T,
    ) -> ((Vec<T>, Vec<T>), (Vec<T>, Vec<T>)) {
        let mut first = vec![a.clone(), b.clone(), c.clone()];
        let other = vec![d.clone(), e.clone(), f.clone()];
        let other_bckp = other.clone();
        let expected = vec![a, b, c, d, e, f];
        smart_append(&mut first, &other);
        ((first, expected), (other, other_bckp))
    }

    #[test]
    fn smart_appending() {
        let (first, second) = smart_appending_template(1, 2, 3, 4, 5, 6);
        assert_eq!(first.0, first.1);
        assert_eq!(second.0, second.1);
        let (first, second) = smart_appending_template(
            "a".to_owned(),
            "b".to_owned(),
            "c".to_owned(),
            "d".to_owned(),
            "e".to_owned(),
            "f".to_owned(),
        );
        assert_eq!(first.0, first.1);
        assert_eq!(second.0, second.1);
    }
}