@@ -2,56 +2,49 @@ use std::{ fs, io::{ self, Write }, path::Path, sync::Arc };
22
33use flate2:: read:: GzDecoder ;
44use once_cell:: sync:: Lazy ;
5- use serde_json:: Value ;
65use tar:: Archive ;
76
8- use crate :: bot:: include:: types:: GlobalMap ;
7+ use crate :: bot:: include:: types:: { GlobalMap , OpeningBook } ;
98
109const COMPRESSED_OPENING_DB : & [ u8 ] = include_bytes ! ( "../../data/openings.tar.gz" ) ;
1110
12- fn read_opening_db ( ) -> Result < Value , io:: Error > {
11+ pub fn read_opening_db ( ) -> Result < OpeningBook , io:: Error > {
1312 let output_dir = Path :: new ( "./db" ) ;
1413 let compressed_path = output_dir. join ( "openings.tar.gz" ) ;
1514 let file_path = output_dir. join ( "openingDB.json" ) ;
1615
17- // If file doesn't exist, extract from embedded archive
1816 if !file_path. exists ( ) {
1917 println ! ( "OpeningDB not found, extracting..." ) ;
2018
2119 fs:: create_dir_all ( output_dir) ?;
2220
23- // Write embedded tar.gz to disk
2421 {
2522 let mut file = fs:: File :: create ( & compressed_path) ?;
2623 file. write_all ( COMPRESSED_OPENING_DB ) ?;
2724 }
2825
29- // Extract tar.gz
3026 let tar_file = fs:: File :: open ( & compressed_path) ?;
3127 let tar = GzDecoder :: new ( tar_file) ;
3228 let mut archive = Archive :: new ( tar) ;
3329 archive. unpack ( output_dir) ?;
3430
35- // Clean up
3631 fs:: remove_file ( & compressed_path) ?;
3732 println ! ( "OpeningDB extracted to {:?}" , file_path) ;
3833 }
3934
40- // Read and parse JSON
4135 let file_content = fs:: read_to_string ( & file_path) ?;
42- let json_data : Value = serde_json
36+ let db : OpeningBook = serde_json
4337 :: from_str ( & file_content)
4438 . map_err ( |e| io:: Error :: new ( io:: ErrorKind :: InvalidData , e) ) ?;
4539
46- Ok ( json_data )
40+ Ok ( db )
4741}
48-
49- pub const OPENING_DB : Lazy < Arc < Value > > = Lazy :: new ( || {
42+ pub static OPENING_DB : Lazy < Arc < OpeningBook > > = Lazy :: new ( || {
5043 Arc :: new ( read_opening_db ( ) . expect ( "Failed to load opening DB" ) )
5144} ) ;
5245
5346impl GlobalMap {
54- pub fn opening_db ( ) -> Arc < Value > {
47+ pub fn opening_db ( ) -> Arc < OpeningBook > {
5548 Arc :: clone ( & OPENING_DB )
5649 }
5750
0 commit comments