Skip to content

Commit 45a3e66

Browse files
committed
imp(cache): load cache
1 parent 37a2f78 commit 45a3e66

File tree

2 files changed

+53
-11
lines changed

2 files changed

+53
-11
lines changed

core/src/cache.rs

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::io::Write;
1+
use std::io::{Read, Write};
22
use std::path::{Path, PathBuf};
33

44
use 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
99
pub 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+
}

core/src/commands.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::cache::{build_cache, store_cache};
1+
use crate::cache::{build_cache, load_cache, store_cache};
22
use crate::common::find_files;
33
use crate::types::{CacheEncoding, CodeownersEntry, OutputFormat};
44

@@ -39,7 +39,11 @@ pub fn codeowners_parse(
3939
//dbg!(&files);
4040
let cache = build_cache(parsed_codeowners, files)?;
4141

42-
store_cache(&cache, cache_file.unwrap(), encoding)?;
42+
//store_cache(&cache, cache_file.unwrap(), encoding)?;
43+
44+
let cache = load_cache(cache_file.unwrap())?;
45+
46+
dbg!(cache);
4347

4448
println!("CODEOWNERS parsing completed successfully");
4549
Ok(())

0 commit comments

Comments
 (0)