@@ -4,10 +4,10 @@ mod tiers_table;
4
4
use anyhow:: Context ;
5
5
use chrono:: { NaiveDate , Utc } ;
6
6
use handlebars:: { handlebars_helper, Handlebars } ;
7
- use itertools:: { Itertools , Position } ;
8
7
use opts:: Config ;
9
8
use rustup_available_packages:: { cache:: FsCache , table:: Table , AvailabilityData , Downloader } ;
10
9
use serde:: Serialize ;
10
+ use std:: collections:: HashMap ;
11
11
use std:: {
12
12
fmt:: Display ,
13
13
fs:: { create_dir_all, File } ,
@@ -107,15 +107,13 @@ fn packages_json(
107
107
pkgs : impl IntoIterator < Item = impl Display > ,
108
108
path : impl AsRef < Path > ,
109
109
) -> io:: Result < ( ) > {
110
- let mut f = File :: create ( path) ?;
111
- writeln ! ( f, "[" ) ?;
112
- pkgs. into_iter ( )
113
- . with_position ( )
114
- . try_for_each ( |pkg| match pkg {
115
- Position :: Only ( pkg) | Position :: Last ( pkg) => writeln ! ( f, "\" {}\" " , pkg) ,
116
- Position :: First ( pkg) | Position :: Middle ( pkg) => writeln ! ( f, "\" {}\" ," , pkg) ,
117
- } ) ?;
118
- writeln ! ( f, "]" )
110
+ let contents = serde_json:: to_vec (
111
+ & pkgs
112
+ . into_iter ( )
113
+ . map ( |p| p. to_string ( ) )
114
+ . collect :: < Vec < String > > ( ) ,
115
+ ) ?;
116
+ std:: fs:: write ( path, contents)
119
117
}
120
118
121
119
fn generate_fs_tree (
@@ -148,24 +146,30 @@ fn generate_fs_tree(
148
146
// or output corrupt data.
149
147
if dates. len ( ) == row. availability_list . len ( ) {
150
148
let path = target_path. join ( & format ! ( "{}.json" , pkg) ) ;
151
- let mut f = File :: create ( & path)
152
- . with_context ( || format ! ( "Can't create file {}" , path. display( ) ) ) ?;
153
- write ! ( f, "{{" ) ?;
154
- for ( date, avail) in dates. iter ( ) . zip ( row. availability_list . iter ( ) ) {
155
- write ! ( f, "\" {}\" :{}," , date. format( "%Y-%m-%d" ) , avail) ?;
156
- }
157
- if let Some ( date) = row. last_available {
158
- write ! ( f, "\" last_available\" :\" {}\" " , date. format( "%Y-%m-%d" ) ) ?;
159
- } else {
160
- write ! ( f, "\" last_available\" :null" ) ?;
161
- }
162
- write ! ( f, "}}" ) ?;
149
+
150
+ let contents = serde_json:: to_vec_pretty ( & TargetPkg {
151
+ availability : dates
152
+ . iter ( )
153
+ . zip ( row. availability_list . iter ( ) )
154
+ . map ( |( date, avail) | ( date. format ( "%Y-%m-%d" ) . to_string ( ) , * avail) )
155
+ . collect ( ) ,
156
+ last_available : row. last_available . map ( |d| d. format ( "%Y-%m-%d" ) . to_string ( ) ) ,
157
+ } ) ?;
158
+ std:: fs:: write ( & path, contents)
159
+ . with_context ( || format ! ( "Can't write file {}" , path. display( ) ) ) ?;
163
160
}
164
161
}
165
162
}
166
163
Ok ( ( ) )
167
164
}
168
165
166
+ #[ derive( serde:: Serialize ) ]
167
+ struct TargetPkg {
168
+ #[ serde( flatten) ]
169
+ availability : HashMap < String , bool > ,
170
+ last_available : Option < String > ,
171
+ }
172
+
169
173
fn main ( ) -> anyhow:: Result < ( ) > {
170
174
let cmd_opts = CmdOpts :: from_args ( ) ;
171
175
let config = match cmd_opts {
0 commit comments