Skip to main content

aether_evals/evals/
diff.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Serialize, Deserialize)]
4pub struct GitDiff {
5    pub diff: String,
6    pub stats: DiffStats,
7}
8
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct DiffStats {
11    pub files_changed: usize,
12    pub lines_added: usize,
13    pub lines_removed: usize,
14}
15
16impl DiffStats {
17    pub fn from_diff(diff: &str) -> Self {
18        let mut lines_added = 0;
19        let mut lines_removed = 0;
20        let mut files_changed = 0;
21
22        for line in diff.lines() {
23            if line.starts_with("diff --git") {
24                files_changed += 1;
25            } else if line.starts_with('+') && !line.starts_with("+++") {
26                lines_added += 1;
27            } else if line.starts_with('-') && !line.starts_with("---") {
28                lines_removed += 1;
29            }
30        }
31
32        Self { files_changed, lines_added, lines_removed }
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39
40    #[test]
41    fn diff_stats_from_diff_counts_files_and_changed_lines() {
42        let diff = "diff --git a/a.txt b/a.txt\n--- a/a.txt\n+++ b/a.txt\n@@\n-old\n+new\ndiff --git a/b.txt b/b.txt\n+++ b/b.txt\n+added\n";
43
44        let stats = DiffStats::from_diff(diff);
45
46        assert_eq!(stats.files_changed, 2);
47        assert_eq!(stats.lines_added, 2);
48        assert_eq!(stats.lines_removed, 1);
49    }
50}