1- use build_warren:: index_manager:: { LOWEST_INDEX , get_st_highest_index} ;
2- use build_warren:: { build_order:: BuildOrderError , build_parser:: fetch_build_order} ;
1+ use build_warren:: build_parser:: fetch_build_order;
2+ use build_warren:: handlers:: { fetch_latest, fetch_segment} ;
3+ use build_warren:: index_manager:: get_st_highest_index;
34use clap:: { Parser , Subcommand } ;
45use serde_json;
56use std:: fs;
@@ -31,6 +32,14 @@ enum Commands {
3132 #[ arg( default_value_t = 1 ) ]
3233 count : u32 ,
3334 } ,
35+
36+ /// Fetch a segment of build orders
37+ FetchSegment {
38+ /// The starting index of the segment
39+ start : u32 ,
40+ /// The ending index of the segment
41+ end : u32 ,
42+ } ,
3443}
3544
3645fn main ( ) {
@@ -60,38 +69,7 @@ fn main() {
6069 Err ( e) => eprintln ! ( "Error fetching build order: {}" , e) ,
6170 } ,
6271 Some ( Commands :: FetchLatest { count } ) => {
63- let highest_index = get_st_highest_index ( ) ;
64- let mut end_index = if * count > highest_index - LOWEST_INDEX {
65- LOWEST_INDEX
66- } else {
67- highest_index - * count + 1
68- } ;
69- let mut increment = 0 ;
70- let mut current_id = highest_index;
71- let mut build_orders = Vec :: new ( ) ;
72- while current_id >= end_index && build_orders. len ( ) < * count as usize {
73- current_id = highest_index - increment;
74- match fetch_build_order ( current_id) {
75- Ok ( build_order) => {
76- build_orders. push ( build_order) ;
77- increment += 1 ;
78- }
79- Err ( e) => {
80- if e. eq ( & BuildOrderError :: Cloaked ) {
81- eprintln ! ( "Build order {} is cloaked, skipping." , current_id) ;
82- end_index = if end_index > LOWEST_INDEX {
83- end_index - 1
84- } else {
85- LOWEST_INDEX
86- } // Decrease end_index to compensate for the skipped build
87- } else {
88- eprintln ! ( "Error fetching build order {}: {}" , current_id, e) ;
89- }
90- increment += 1 ; // Increment to avoid infinite loop
91- continue ; // Some builds might not be available, continue fetching
92- }
93- }
94- }
72+ let build_orders = fetch_latest ( * count) ;
9573 let json_output = serde_json:: to_string_pretty ( & build_orders)
9674 . expect ( "Failed to serialize build orders to JSON" ) ;
9775 if let Some ( output_file) = & cli. output {
@@ -104,5 +82,16 @@ fn main() {
10482 None => {
10583 eprintln ! ( "No command provided. Use --help to see available commands." ) ;
10684 }
85+ Some ( Commands :: FetchSegment { start, end } ) => {
86+ let build_orders = fetch_segment ( * start, * end) ;
87+ let json_output = serde_json:: to_string_pretty ( & build_orders)
88+ . expect ( "Failed to serialize build orders to JSON" ) ;
89+ if let Some ( output_file) = & cli. output {
90+ fs:: write ( output_file, json_output)
91+ . expect ( "Failed to write build orders to output file" ) ;
92+ } else {
93+ println ! ( "{}" , json_output) ;
94+ }
95+ }
10796 }
10897}
0 commit comments