@@ -3,14 +3,24 @@ use anyhow::Result;
33use clap:: ArgMatches ;
44use schema:: {
55 bundle:: Bundle ,
6- index:: { Index , Indices } ,
6+ index:: { Index , IndexMap } ,
77} ;
88
99pub async fn index_command ( matches : & ArgMatches ) -> Result < ( ) > {
1010 let dir = std:: fs:: read_dir ( matches. value_of ( "directory" ) . unwrap ( ) ) ?;
1111
1212 let fs_out = matches. value_of ( "output" ) . unwrap ( ) ;
1313
14+ let mut index_map = IndexMap :: new ( ) ;
15+
16+ if matches. is_present ( "index" ) {
17+ let index_io = std:: fs:: read ( matches. value_of ( "index" ) . unwrap ( ) ) ?;
18+
19+ let parsed_indices: IndexMap = serde_json:: from_slice ( & index_io) ?;
20+
21+ index_map = parsed_indices;
22+ }
23+
1424 let entries = dir
1525 . filter ( |i| i. is_ok ( ) )
1626 . map ( |i| i. unwrap ( ) )
@@ -19,20 +29,38 @@ pub async fn index_command(matches: &ArgMatches) -> Result<()> {
1929 . map ( |i| i. path ( ) )
2030 . collect :: < Vec < _ > > ( ) ;
2131
22- let mut indices = Indices :: new ( ) ;
32+ let mut diffs = 0u64 ;
2333
2434 for entry in & entries {
2535 let content = std:: fs:: read ( entry) ?;
2636
2737 let bundle: Bundle = serde_json:: from_slice ( & content) ?;
2838
29- indices. push ( Index {
30- meta : bundle. meta ,
31- source : bundle. source ,
32- } ) ;
39+ match index_map. get ( & bundle. meta . name ) {
40+ Some ( v) => {
41+ if v. meta != bundle. meta || v. source != bundle. source {
42+ diffs += 1 ;
43+
44+ index_map. insert ( bundle. meta . name . clone ( ) , Index {
45+ meta : bundle. meta ,
46+ source : bundle. source ,
47+ } ) ;
48+ }
49+ }
50+ None => {
51+ diffs += 1 ;
52+
53+ index_map. insert ( bundle. meta . name . clone ( ) , Index {
54+ meta : bundle. meta ,
55+ source : bundle. source ,
56+ } ) ;
57+ }
58+ }
3359 }
3460
35- write_to_disk ( fs_out, indices) ?;
61+ if diffs > 0 {
62+ write_to_disk ( fs_out, index_map) ?;
63+ }
3664
3765 Ok ( ( ) )
3866}
0 commit comments