@@ -16,15 +16,29 @@ pub fn gen_outfile_name(in_name: &str) -> String {
16
16
)
17
17
}
18
18
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
+
19
27
pub trait SetInfoOrder {
20
28
type ReturnType : Ord ;
21
29
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
+ }
22
36
}
23
37
24
38
pub struct SortByName ;
25
39
impl SetInfoOrder for SortByName {
26
40
type ReturnType = String ;
27
- fn get_key ( set_info : & SetInfo ) -> String {
41
+ fn get_key ( set_info : & SetInfo ) -> Self :: ReturnType {
28
42
set_info. set_name ( ) . to_string ( )
29
43
}
30
44
}
@@ -33,7 +47,7 @@ pub struct SortByDate;
33
47
impl SetInfoOrder for SortByDate {
34
48
type ReturnType = NaiveDate ;
35
49
// TODO: Cache set infos ?
36
- fn get_key ( _set_info : & SetInfo ) -> NaiveDate {
50
+ fn get_key ( _set_info : & SetInfo ) -> Self :: ReturnType {
37
51
// set_info
38
52
// .set_uri()
39
53
// .fetch()
@@ -45,36 +59,51 @@ impl SetInfoOrder for SortByDate {
45
59
}
46
60
}
47
61
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
+ }
52
76
}
53
77
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
57
80
where
58
81
P : SetInfoOrder ,
59
82
{
60
83
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] ) ) )
76
86
. join ( "\n \n " )
77
87
}
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
+ }
78
107
}
79
108
80
109
#[ derive( tabled:: Tabled ) ]
@@ -96,31 +125,17 @@ impl From<&ScryfallCardWrapper> for RenameLater {
96
125
97
126
pub struct OutputTable ;
98
127
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 ( )
117
136
}
118
137
}
119
138
120
- pub fn render < F : OutputFormat , P : SetInfoOrder > ( c : CardsBySet ) -> String {
121
- F :: render :: < P > ( c)
122
- }
123
-
124
139
pub fn write_to_file ( data : & str , path : & str ) -> Result < ( ) > {
125
140
let mut outfile = File :: create ( path) ?;
126
141
Ok ( outfile. write_all ( data. as_bytes ( ) ) ?)
0 commit comments