Skip to content

Commit

Permalink
feat(cli): added additional options to the cli crate
Browse files Browse the repository at this point in the history
  • Loading branch information
niomate committed May 23, 2022
1 parent 24e2678 commit db144c5
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 16 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion scrollrack-cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "scrollrack-cli"
version = "0.1.0"
version = "0.2.0"
edition = "2021"

[dependencies]
Expand Down
11 changes: 9 additions & 2 deletions scrollrack-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
}

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::<output::SortByName>(cards_by_set, &outfile),
Expand Down
2 changes: 1 addition & 1 deletion scrollrack-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "scrollrack-core"
version = "0.1.0"
version = "0.2.0"
edition = "2021"

[dependencies]
Expand Down
22 changes: 13 additions & 9 deletions scrollrack-core/src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,48 +13,53 @@ 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()
}
}

pub struct SortByDate;
impl SetInfoSortKey for SortByDate {
impl SetInfoOrder for SortByDate {
type ReturnType = NaiveDate;
fn get_key(set_info: &SetInfo) -> NaiveDate {
set_info
.set_uri()
.fetch()
.unwrap()
.released_at
// Year in which MTG was first released
.unwrap_or(NaiveDate::from_yo(1993, 1))
}
}

pub fn write_to_file<P>(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())
Expand Down
2 changes: 1 addition & 1 deletion scrollrack-core/src/query_stuff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)
})
Expand Down

0 comments on commit db144c5

Please sign in to comment.