Skip to main content

clankerdiff_core/models/
hunk.rs

1use super::{PatchLine, PatchLineKind};
2use serde::{Deserialize, Serialize};
3
4/// A unified-diff hunk.
5#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
6pub struct Hunk {
7    pub header: String,
8    pub function_context: Option<String>,
9    pub old_start: usize,
10    pub old_count: usize,
11    pub new_start: usize,
12    pub new_count: usize,
13    pub lines: Vec<PatchLine>,
14}
15
16impl Hunk {
17    /// Number of additions.
18    #[must_use]
19    pub fn additions(&self) -> usize {
20        self.count(PatchLineKind::Added)
21    }
22
23    /// Number of deletions.
24    #[must_use]
25    pub fn deletions(&self) -> usize {
26        self.count(PatchLineKind::Removed)
27    }
28
29    fn count(&self, kind: PatchLineKind) -> usize {
30        self.lines.iter().filter(|line| line.kind == kind).count()
31    }
32}