Skip to content

Commit 3bf471e

Browse files
committed
wip(core): parse cache
1 parent 9b39cbc commit 3bf471e

File tree

5 files changed

+88
-15
lines changed

5 files changed

+88
-15
lines changed

core/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ utils = { path = "../utils" }
1111
rand = "0.9.1"
1212
log = "0.4.27"
1313
ignore = "0.4.23"
14+
serde = { version = "1.0.219", features = ["derive"] }
15+
bincode = {version= "2.0.1", features = ["serde"] }
1416

1517
[dev-dependencies]
1618
tempfile = "3.8"

core/src/cache.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use std::path::{Path, PathBuf};
2+
3+
use crate::common::{find_owners_for_file, find_tags_for_file};
4+
use crate::types::{CodeownersCache, CodeownersEntry, FileEntry};
5+
use utils::error::{Error, Result};
6+
7+
/// Create a cache from parsed CODEOWNERS entries and files
8+
pub fn build_cache(entries: Vec<CodeownersEntry>, files: Vec<PathBuf>) -> Result<CodeownersCache> {
9+
let mut file_entries = Vec::new();
10+
let mut owners_map = std::collections::HashMap::new();
11+
let mut tags_map = std::collections::HashMap::new();
12+
13+
// Process each file to find owners and tags
14+
for file_path in files {
15+
let owners = find_owners_for_file(&file_path, &entries)?;
16+
let tags = find_tags_for_file(&file_path, &entries)?;
17+
18+
// Build file entry
19+
let file_entry = FileEntry {
20+
path: file_path.clone(),
21+
owners: owners.clone(),
22+
tags: tags.clone(),
23+
};
24+
file_entries.push(file_entry);
25+
}
26+
27+
Ok(CodeownersCache {
28+
entries,
29+
files: file_entries,
30+
owners_map,
31+
tags_map,
32+
})
33+
}
34+
35+
/// Store Cache
36+
pub fn store_cache(cache: &CodeownersCache, path: &Path) -> Result<()> {
37+
let parent = path
38+
.parent()
39+
.ok_or_else(|| Error::new("Invalid cache path"))?;
40+
std::fs::create_dir_all(parent)?;
41+
42+
let file = std::fs::File::create(path)?;
43+
44+
bincode::serialize_into(file, cache)
45+
.map_err(|e| Error::new(&format!("Failed to serialize cache: {}", e)))?;
46+
}
47+
48+
/// Load cache from a file
49+
pub fn load_cache(path: &Path) -> Result<CodeownersCache> {
50+
let file = std::fs::File::open(path)?;
51+
bincode::deserialize_from(file)
52+
.map_err(|e| Error::new(&format!("Failed to deserialize cache: {}", e)))
53+
}
54+

core/src/commands.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::cache::build_cache;
12
use crate::common::find_files;
23
use crate::types::{CodeownersEntry, OutputFormat};
34

@@ -20,7 +21,7 @@ pub fn codeowners_parse(
2021

2122
let codeowners_files = crate::common::find_codeowners_files(path)?;
2223

23-
dbg!(&codeowners_files);
24+
//dbg!(&codeowners_files);
2425

2526
let parsed_codeowners: Vec<CodeownersEntry> = codeowners_files
2627
.iter()
@@ -31,18 +32,12 @@ pub fn codeowners_parse(
3132
.flatten()
3233
.collect();
3334

34-
dbg!(&parsed_codeowners);
35+
//dbg!(&parsed_codeowners);
3536

3637
let files = find_files(path)?;
3738

38-
dbg!(&files);
39-
40-
for file in files {
41-
let owners = crate::common::find_owners_for_file(&file, parsed_codeowners.as_slice());
42-
let tags = crate::common::find_tags_for_file(&file, parsed_codeowners.as_slice());
43-
println!("File: {} - Owners: {:?}", file.display(), owners);
44-
println!("File: {} - Tags: {:?}", file.display(), tags);
45-
}
39+
//dbg!(&files);
40+
build_cache(parsed_codeowners, files)?;
4641

4742
println!("CODEOWNERS parsing completed successfully");
4843
Ok(())

core/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#[macro_use]
22
extern crate log;
33

4+
pub mod cache;
45
pub mod commands;
56
pub mod common;
67
pub mod types;

core/src/types.rs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use std::path::PathBuf;
22

3+
use serde::{Deserialize, Serialize};
4+
35
/// CODEOWNERS entry with source tracking
4-
#[derive(Debug)]
6+
#[derive(Debug, Serialize, Deserialize)]
57
pub struct CodeownersEntry {
68
pub source_file: PathBuf,
79
pub line_number: usize,
@@ -11,14 +13,14 @@ pub struct CodeownersEntry {
1113
}
1214

1315
/// Detailed owner representation
14-
#[derive(Debug, Clone)]
16+
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, Hash)]
1517
pub struct Owner {
1618
pub identifier: String,
1719
pub owner_type: OwnerType,
1820
}
1921

2022
/// Owner type classification
21-
#[derive(Debug, Clone)]
23+
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, Hash)]
2224
pub enum OwnerType {
2325
User,
2426
Team,
@@ -28,10 +30,10 @@ pub enum OwnerType {
2830
}
2931

3032
/// Tag representation
31-
#[derive(Debug, Clone)]
33+
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, Hash)]
3234
pub struct Tag(pub String);
3335

34-
#[derive(Clone, Debug, PartialEq)]
36+
#[derive(Clone, Debug, Eq, PartialEq)]
3537
pub enum OutputFormat {
3638
Text,
3739
Json,
@@ -47,3 +49,22 @@ impl std::fmt::Display for OutputFormat {
4749
}
4850
}
4951
}
52+
53+
// Cache related types
54+
/// File entry in the ownership cache
55+
#[derive(Debug, Clone, Serialize, Deserialize)]
56+
pub struct FileEntry {
57+
pub path: PathBuf,
58+
pub owners: Vec<Owner>,
59+
pub tags: Vec<Tag>,
60+
}
61+
62+
/// Cache for storing parsed CODEOWNERS information
63+
#[derive(Debug, Serialize, Deserialize)]
64+
pub struct CodeownersCache {
65+
pub entries: Vec<CodeownersEntry>,
66+
pub files: Vec<FileEntry>,
67+
// Derived data for lookups
68+
pub owners_map: std::collections::HashMap<Owner, Vec<PathBuf>>,
69+
pub tags_map: std::collections::HashMap<Tag, Vec<PathBuf>>,
70+
}

0 commit comments

Comments
 (0)