diff --git a/Cargo.lock b/Cargo.lock index 86267f4..95c8c84 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -577,7 +577,7 @@ checksum = "f3f6f92acf49d1b98f7a81226834412ada05458b7364277387724a237f062695" [[package]] name = "scrollrack-cli" -version = "0.1.0" +version = "0.2.0" dependencies = [ "clap", "scrollrack-core", @@ -585,7 +585,7 @@ dependencies = [ [[package]] name = "scrollrack-core" -version = "0.1.0" +version = "0.2.0" dependencies = [ "chrono", "itertools", diff --git a/scrollrack-cli/Cargo.toml b/scrollrack-cli/Cargo.toml index 828bf5b..6050aab 100644 --- a/scrollrack-cli/Cargo.toml +++ b/scrollrack-cli/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "scrollrack-cli" -version = "0.1.0" +version = "0.2.0" edition = "2021" [dependencies] diff --git a/scrollrack-cli/src/main.rs b/scrollrack-cli/src/main.rs index 2828a1a..b1da89b 100644 --- a/scrollrack-cli/src/main.rs +++ b/scrollrack-cli/src/main.rs @@ -16,18 +16,25 @@ enum Ordering { struct Args { #[clap(help = "Path to the card list")] path: String, - #[clap(short, long, arg_enum, default_value_t=Ordering::ALPHA, help="Specifies in which order the sets should be printed in the output file")] + #[clap(short='O', long, arg_enum, default_value_t=Ordering::ALPHA, help="Specifies in which order the sets should be printed in the output file")] ordering: Ordering, #[clap(short, long, help = "Output sets per card instead of cards per set")] inverted: bool, + #[clap(short, long, help = "Path to the output file")] + output: Option, } fn main() -> Result<(), String> { let args = Args::parse(); + let cards_by_set = parse_card_infos(&args.path) .map(query_and_merge_all) .map_err(|err| format!("Error: {}", err))?; - let outfile = output::gen_outfile_name(&args.path); + + let outfile = match args.output { + Some(path) => path, + None => output::gen_outfile_name(&args.path), + }; match args.ordering { Ordering::ALPHA => output::write_to_file::(cards_by_set, &outfile), diff --git a/scrollrack-core/Cargo.toml b/scrollrack-core/Cargo.toml index 48c9da6..2ca5c32 100644 --- a/scrollrack-core/Cargo.toml +++ b/scrollrack-core/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "scrollrack-core" -version = "0.1.0" +version = "0.2.0" edition = "2021" [dependencies] diff --git a/scrollrack-core/src/output.rs b/scrollrack-core/src/output.rs index 1364ca6..f16b4e1 100644 --- a/scrollrack-core/src/output.rs +++ b/scrollrack-core/src/output.rs @@ -2,7 +2,6 @@ use crate::cardinfo::SetInfo; use crate::query_stuff::CardsBySet; use chrono::naive::NaiveDate; use itertools::Itertools; -use std::error; use std::fs::File; use std::io::Write; use std::path::Path; @@ -14,13 +13,13 @@ pub fn gen_outfile_name(in_name: &str) -> String { ) } -pub trait SetInfoSortKey { +pub trait SetInfoOrder { type ReturnType: Ord; fn get_key(set_info: &SetInfo) -> Self::ReturnType; } pub struct SortByName; -impl SetInfoSortKey for SortByName { +impl SetInfoOrder for SortByName { type ReturnType = String; fn get_key(set_info: &SetInfo) -> String { set_info.set_name().to_string() @@ -28,7 +27,7 @@ impl SetInfoSortKey for SortByName { } pub struct SortByDate; -impl SetInfoSortKey for SortByDate { +impl SetInfoOrder for SortByDate { type ReturnType = NaiveDate; fn get_key(set_info: &SetInfo) -> NaiveDate { set_info @@ -36,26 +35,31 @@ impl SetInfoSortKey for SortByDate { .fetch() .unwrap() .released_at + // Year in which MTG was first released .unwrap_or(NaiveDate::from_yo(1993, 1)) } } pub fn write_to_file

(cards_by_set: CardsBySet, path: &str) -> Result<(), String> where - P: SetInfoSortKey, + P: SetInfoOrder, { let mut outfile = File::create(path).map_err(|err| format!("Could not open file: {}", &err))?; let out_string: String = cards_by_set .keys() .sorted_by_key(|set_info| P::get_key(set_info)) .map(|k| { - format!("{}:\n", k.set_name()) - + &cards_by_set[k] + format!( + "{}:\n{}", + k.set_name(), + &cards_by_set[k] .iter() - .map(|card| format!("\t- {}", card.name)) + .sorted_by_key(|card| &card.name) + .map(|card| format!("\t- {} (#{})", card.name, card.collector_number)) .join("\n") + ) }) - .collect(); + .join("\n\n"); outfile .write_all(out_string.as_bytes()) diff --git a/scrollrack-core/src/query_stuff.rs b/scrollrack-core/src/query_stuff.rs index 86f63d7..6ce63bb 100644 --- a/scrollrack-core/src/query_stuff.rs +++ b/scrollrack-core/src/query_stuff.rs @@ -15,7 +15,7 @@ pub fn query(info: CardInfo) -> Vec<(SetInfo, card::Card)> { res.iter() .map(|scryinfo| { ( - SetInfo::new(&scryinfo.set_name, scryinfo.set_uri.clone()), + SetInfo::new(&scryinfo.set_name, scryinfo.set_uri.to_owned()), scryinfo.to_owned(), ) })