Skip to content

Commit ff500c7

Browse files
author
Daniel Gusenburger
committed
added key to sort by card amount per set
1 parent 554c20b commit ff500c7

File tree

3 files changed

+77
-62
lines changed

3 files changed

+77
-62
lines changed

scrollrack-cli/src/main.rs

+14-16
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ use clap::ArgEnum;
22
use clap::Parser;
33

44
use scrollrack_core::card_query::CardQuery;
5-
use scrollrack_core::output;
5+
use scrollrack_core::output::{
6+
gen_outfile_name, render, write_to_file, OutputFormat, OutputItemList, OutputTable,
7+
SortByCardAmount, SortByDate, SortByName,
8+
};
69
use scrollrack_core::parse;
710

811
use anyhow::Result;
@@ -11,6 +14,7 @@ use anyhow::Result;
1114
enum Ordering {
1215
ALPHA,
1316
DATE,
17+
AMOUNT,
1418
}
1519

1620
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ArgEnum, Debug)]
@@ -38,8 +42,6 @@ struct Args {
3842
async fn main() -> Result<()> {
3943
let args = Args::parse();
4044

41-
println!("{:?}", args);
42-
4345
let lines = parse::read_lines(&args.path)?;
4446

4547
let cards_by_set = CardQuery::default()
@@ -48,23 +50,19 @@ async fn main() -> Result<()> {
4850

4951
let outfile = match args.output {
5052
Some(path) => path,
51-
None => output::gen_outfile_name(&args.path),
53+
None => gen_outfile_name(&args.path),
5254
};
5355

5456
let out_string = match (args.format, args.ordering) {
55-
(Output::LIST, Ordering::ALPHA) => {
56-
output::render::<output::OutputItemList, output::SortByName>(cards_by_set)
57-
}
58-
(Output::LIST, Ordering::DATE) => {
59-
output::render::<output::OutputItemList, output::SortByDate>(cards_by_set)
60-
}
61-
(Output::TABLE, Ordering::ALPHA) => {
62-
output::render::<output::OutputTable, output::SortByName>(cards_by_set)
63-
}
64-
(Output::TABLE, Ordering::DATE) => {
65-
output::render::<output::OutputTable, output::SortByDate>(cards_by_set)
57+
(Output::LIST, Ordering::ALPHA) => render::<OutputItemList, SortByName>(&cards_by_set),
58+
(Output::LIST, Ordering::DATE) => render::<OutputItemList, SortByDate>(&cards_by_set),
59+
(Output::LIST, Ordering::AMOUNT) => {
60+
OutputItemList::render::<SortByCardAmount>(&cards_by_set)
6661
}
62+
(Output::TABLE, Ordering::ALPHA) => render::<OutputTable, SortByName>(&cards_by_set),
63+
(Output::TABLE, Ordering::DATE) => render::<OutputTable, SortByDate>(&cards_by_set),
64+
(Output::TABLE, Ordering::AMOUNT) => render::<OutputTable, SortByCardAmount>(&cards_by_set),
6765
};
6866

69-
output::write_to_file(&out_string, &outfile)
67+
write_to_file(&out_string, &outfile)
7068
}

scrollrack-core/src/output.rs

+61-46
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,29 @@ pub fn gen_outfile_name(in_name: &str) -> String {
1616
)
1717
}
1818

19+
pub fn render<F, P>(cards_by_set: &CardsBySet) -> String
20+
where
21+
F: OutputFormat,
22+
P: SetInfoOrder,
23+
{
24+
F::render::<P>(cards_by_set)
25+
}
26+
1927
pub trait SetInfoOrder {
2028
type ReturnType: Ord;
2129
fn get_key(set_info: &SetInfo) -> Self::ReturnType;
30+
fn get_combined_key(
31+
set_info: &SetInfo,
32+
cards_in_set: &Vec<ScryfallCardWrapper>,
33+
) -> Self::ReturnType {
34+
Self::get_key(set_info)
35+
}
2236
}
2337

2438
pub struct SortByName;
2539
impl SetInfoOrder for SortByName {
2640
type ReturnType = String;
27-
fn get_key(set_info: &SetInfo) -> String {
41+
fn get_key(set_info: &SetInfo) -> Self::ReturnType {
2842
set_info.set_name().to_string()
2943
}
3044
}
@@ -33,7 +47,7 @@ pub struct SortByDate;
3347
impl SetInfoOrder for SortByDate {
3448
type ReturnType = NaiveDate;
3549
// TODO: Cache set infos ?
36-
fn get_key(_set_info: &SetInfo) -> NaiveDate {
50+
fn get_key(_set_info: &SetInfo) -> Self::ReturnType {
3751
// set_info
3852
// .set_uri()
3953
// .fetch()
@@ -45,36 +59,51 @@ impl SetInfoOrder for SortByDate {
4559
}
4660
}
4761

48-
pub trait OutputFormat {
49-
fn render<P>(c: CardsBySet) -> String
50-
where
51-
P: SetInfoOrder;
62+
pub struct SortByCardAmount;
63+
impl SetInfoOrder for SortByCardAmount {
64+
type ReturnType = i32;
65+
66+
fn get_key(set_info: &SetInfo) -> Self::ReturnType {
67+
panic!("To use SortByCardAmount as a key, use get_combined_key instead.")
68+
}
69+
70+
fn get_combined_key(
71+
set_info: &SetInfo,
72+
cards_in_set: &Vec<ScryfallCardWrapper>,
73+
) -> Self::ReturnType {
74+
-(cards_in_set.len() as i32)
75+
}
5276
}
5377

54-
pub struct OutputItemList;
55-
impl OutputFormat for OutputItemList {
56-
fn render<P>(c: CardsBySet) -> String
78+
pub trait OutputFormat {
79+
fn render<P>(c: &CardsBySet) -> String
5780
where
5881
P: SetInfoOrder,
5982
{
6083
c.keys()
61-
.sorted_by_key(|set_info| P::get_key(set_info))
62-
.map(|k| {
63-
format!(
64-
"{}:\n{}",
65-
k.set_name(),
66-
c[k].iter()
67-
.sorted_by_key(|card| card.card_name())
68-
.map(|card| if k.virtual_set() {
69-
"\t - ".to_owned() + card.card_name()
70-
} else {
71-
"\t - ".to_owned() + &card.format_detailed()
72-
})
73-
.join("\n")
74-
)
75-
})
84+
.sorted_by_key(|set_info| P::get_combined_key(set_info, &c[set_info]))
85+
.map(|k| format!("{}:\n{}", k.set_name(), Self::render_set(k, &c[k])))
7686
.join("\n\n")
7787
}
88+
89+
fn render_set(set_info: &SetInfo, cards: &Vec<ScryfallCardWrapper>) -> String;
90+
}
91+
92+
pub struct OutputItemList;
93+
impl OutputFormat for OutputItemList {
94+
fn render_set(set_info: &SetInfo, cards: &Vec<ScryfallCardWrapper>) -> String {
95+
cards
96+
.iter()
97+
.sorted_by_key(|card| card.card_name())
98+
.map(|card| {
99+
if set_info.virtual_set() {
100+
"\t - ".to_owned() + card.card_name()
101+
} else {
102+
"\t - ".to_owned() + &card.format_detailed()
103+
}
104+
})
105+
.join("\n")
106+
}
78107
}
79108

80109
#[derive(tabled::Tabled)]
@@ -96,31 +125,17 @@ impl From<&ScryfallCardWrapper> for RenameLater {
96125

97126
pub struct OutputTable;
98127
impl OutputFormat for OutputTable {
99-
fn render<P>(c: CardsBySet) -> String
100-
where
101-
P: SetInfoOrder,
102-
{
103-
c.keys()
104-
.sorted_by_key(|set_info| P::get_key(set_info))
105-
.map(|k| {
106-
format!(
107-
"{}:\n{}",
108-
k.set_name(),
109-
Table::new(
110-
c[k].iter()
111-
.sorted_by_key(|card| card.card_name())
112-
.map(|c| Into::<RenameLater>::into(c))
113-
)
114-
)
115-
})
116-
.join("\n\n")
128+
fn render_set(_: &SetInfo, cards: &Vec<ScryfallCardWrapper>) -> String {
129+
Table::new(
130+
cards
131+
.iter()
132+
.sorted_by_key(|card| card.card_name())
133+
.map(|c| Into::<RenameLater>::into(c)),
134+
)
135+
.to_string()
117136
}
118137
}
119138

120-
pub fn render<F: OutputFormat, P: SetInfoOrder>(c: CardsBySet) -> String {
121-
F::render::<P>(c)
122-
}
123-
124139
pub fn write_to_file(data: &str, path: &str) -> Result<()> {
125140
let mut outfile = File::create(path)?;
126141
Ok(outfile.write_all(data.as_bytes())?)

scrollrack-core/src/rules/postprocess.rs

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ impl Combine {
4545
.extend(cards.to_vec().into_iter());
4646
}
4747

48+
// TODO: Aggregate card prices to average over each occurrence in the combined sets
49+
4850
for (key, value) in cards_by_set.iter() {
4951
if !sets_to_combine.contains(&key) {
5052
combined

0 commit comments

Comments
 (0)