#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DiffEntry {
pub line: usize,
pub analyzer: String,
pub original: String,
pub modified: String,
pub description: String,
pub import: Option<String>
}
#[derive(Debug, Clone)]
pub struct FileDiff {
pub path: String,
pub entries: Vec<DiffEntry>
}
impl FileDiff {
#[inline]
pub fn new(path: String) -> Self {
Self {
path,
entries: Vec::new()
}
}
#[inline]
pub fn add_entry(&mut self, entry: DiffEntry) {
self.entries.push(entry);
}
#[inline]
pub fn total_changes(&self) -> usize {
self.entries.len()
}
}
#[derive(Debug, Clone)]
pub struct DiffResult {
pub files: Vec<FileDiff>
}
impl DiffResult {
#[inline]
pub fn new() -> Self {
Self {
files: Vec::new()
}
}
#[inline]
pub fn add_file(&mut self, file_diff: FileDiff) {
if file_diff.total_changes() > 0 {
self.files.push(file_diff);
}
}
#[inline]
pub fn total_changes(&self) -> usize {
self.files.iter().map(|f| f.total_changes()).sum()
}
#[inline]
pub fn total_files(&self) -> usize {
self.files.len()
}
}
impl Default for DiffResult {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_diff_entry_creation() {
let entry = DiffEntry {
line: 10,
analyzer: "test".to_string(),
original: "old".to_string(),
modified: "new".to_string(),
description: "desc".to_string(),
import: None
};
assert_eq!(entry.line, 10);
assert_eq!(entry.analyzer, "test");
}
#[test]
fn test_file_diff_new() {
let diff = FileDiff::new("test.rs".to_string());
assert_eq!(diff.path, "test.rs");
assert_eq!(diff.total_changes(), 0);
}
#[test]
fn test_file_diff_add_entry() {
let mut diff = FileDiff::new("test.rs".to_string());
let entry = DiffEntry {
line: 1,
analyzer: "test".to_string(),
original: "old".to_string(),
modified: "new".to_string(),
description: "desc".to_string(),
import: None
};
diff.add_entry(entry);
assert_eq!(diff.total_changes(), 1);
}
#[test]
fn test_diff_result_new() {
let result = DiffResult::new();
assert_eq!(result.total_changes(), 0);
assert_eq!(result.total_files(), 0);
}
#[test]
fn test_diff_result_add_file() {
let mut result = DiffResult::new();
let mut file_diff = FileDiff::new("test.rs".to_string());
let entry = DiffEntry {
line: 1,
analyzer: "test".to_string(),
original: "old".to_string(),
modified: "new".to_string(),
description: "desc".to_string(),
import: None
};
file_diff.add_entry(entry);
result.add_file(file_diff);
assert_eq!(result.total_files(), 1);
assert_eq!(result.total_changes(), 1);
}
#[test]
fn test_diff_result_skip_empty_files() {
let mut result = DiffResult::new();
let file_diff = FileDiff::new("test.rs".to_string());
result.add_file(file_diff);
assert_eq!(result.total_files(), 0);
}
#[test]
fn test_diff_result_multiple_files() {
let mut result = DiffResult::new();
let mut file1 = FileDiff::new("file1.rs".to_string());
file1.add_entry(DiffEntry {
line: 1,
analyzer: "test".to_string(),
original: "old".to_string(),
modified: "new".to_string(),
description: "desc".to_string(),
import: None
});
let mut file2 = FileDiff::new("file2.rs".to_string());
file2.add_entry(DiffEntry {
line: 1,
analyzer: "test".to_string(),
original: "old".to_string(),
modified: "new".to_string(),
description: "desc".to_string(),
import: None
});
result.add_file(file1);
result.add_file(file2);
assert_eq!(result.total_files(), 2);
assert_eq!(result.total_changes(), 2);
}
}