1- use std:: io:: Write ;
1+ use std:: io:: { Read , Write } ;
22use std:: path:: { Path , PathBuf } ;
33
44use crate :: common:: { find_owners_for_file, find_tags_for_file} ;
@@ -8,8 +8,8 @@ use utils::error::{Error, Result};
88/// Create a cache from parsed CODEOWNERS entries and files
99pub fn build_cache ( entries : Vec < CodeownersEntry > , files : Vec < PathBuf > ) -> Result < CodeownersCache > {
1010 let mut file_entries = Vec :: new ( ) ;
11- let mut owners_map = std:: collections:: HashMap :: new ( ) ;
12- let mut tags_map = std:: collections:: HashMap :: new ( ) ;
11+ let owners_map = std:: collections:: HashMap :: new ( ) ;
12+ let tags_map = std:: collections:: HashMap :: new ( ) ;
1313
1414 // Process each file to find owners and tags
1515 for file_path in files {
@@ -59,9 +59,47 @@ pub fn store_cache(cache: &CodeownersCache, path: &Path, encoding: CacheEncoding
5959 Ok ( ( ) )
6060}
6161
62- // Load cache from a file
63- // pub fn load_cache(path: &Path) -> Result<CodeownersCache> {
64- // let file = std::fs::File::open(path)?;
65- // bincode::deserialize_from(file)
66- // .map_err(|e| Error::new(&format!("Failed to deserialize cache: {}", e)))
67- // }
62+ /// Load Cache from file, automatically detecting whether it's JSON or Bincode format
63+ pub fn load_cache ( path : & Path ) -> Result < CodeownersCache > {
64+ // Read the first byte to make an educated guess about the format
65+ let mut file = std:: fs:: File :: open ( path)
66+ . map_err ( |e| Error :: new ( & format ! ( "Failed to open cache file: {}" , e) ) ) ?;
67+
68+ let mut first_byte = [ 0u8 ; 1 ] ;
69+ let read_result = file. read_exact ( & mut first_byte) ;
70+
71+ // Close the file handle and reopen for full reading
72+ drop ( file) ;
73+
74+ if read_result. is_ok ( ) && first_byte[ 0 ] == b'{' {
75+ // First byte is '{', likely JSON
76+ let file = std:: fs:: File :: open ( path)
77+ . map_err ( |e| Error :: new ( & format ! ( "Failed to open cache file: {}" , e) ) ) ?;
78+ let reader = std:: io:: BufReader :: new ( file) ;
79+
80+ return serde_json:: from_reader ( reader)
81+ . map_err ( |e| Error :: new ( & format ! ( "Failed to deserialize JSON cache: {}" , e) ) ) ;
82+ }
83+
84+ // Try bincode first since it's not JSON
85+ let file = std:: fs:: File :: open ( path)
86+ . map_err ( |e| Error :: new ( & format ! ( "Failed to open cache file: {}" , e) ) ) ?;
87+ let mut reader = std:: io:: BufReader :: new ( file) ;
88+
89+ match bincode:: serde:: decode_from_std_read ( & mut reader, bincode:: config:: standard ( ) ) {
90+ Ok ( cache) => Ok ( cache) ,
91+ Err ( _) => {
92+ // If bincode fails and it's not obviously JSON, still try JSON as a fallback
93+ let file = std:: fs:: File :: open ( path)
94+ . map_err ( |e| Error :: new ( & format ! ( "Failed to open cache file: {}" , e) ) ) ?;
95+ let reader = std:: io:: BufReader :: new ( file) ;
96+
97+ serde_json:: from_reader ( reader) . map_err ( |e| {
98+ Error :: new ( & format ! (
99+ "Failed to deserialize cache in any supported format: {}" ,
100+ e
101+ ) )
102+ } )
103+ }
104+ }
105+ }
0 commit comments