1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
use crate::show_commit::delta::Delta;
use crate::show_commit::status::Status;
/// Represents a file change within a Git repository
#[derive(Debug, Clone)]
#[cfg_attr(test, derive(PartialEq))]
pub struct FileStat {
status: Status,
to_name: String,
from_name: String,
largest_old_line_number: u32,
largest_new_line_number: u32,
deltas: Vec<Delta>,
}
impl FileStat {
/// Create a new `FileStat`
pub(super) fn new(from_name: &str, to_name: &str, status: Status) -> Self {
Self {
status,
to_name: String::from(to_name),
from_name: String::from(from_name),
largest_old_line_number: 0,
largest_new_line_number: 0,
deltas: vec![],
}
}
pub(super) fn add_delta(&mut self, delta: Delta) {
let last_old_line_number = delta.old_start() + delta.old_lines();
if self.largest_old_line_number < last_old_line_number {
self.largest_old_line_number = last_old_line_number;
}
let last_new_line_number = delta.new_start() + delta.new_lines();
if self.largest_new_line_number < last_new_line_number {
self.largest_new_line_number = last_new_line_number;
}
self.deltas.push(delta);
}
/// Get the status of this file change
pub(super) const fn get_status(&self) -> &Status {
&self.status
}
/// Get the destination file name for this change.
pub(super) fn get_to_name(&self) -> &str {
self.to_name.as_str()
}
/// Get the source file name for this change.
pub(super) fn get_from_name(&self) -> &str {
self.from_name.as_str()
}
pub(crate) const fn largest_old_line_number(&self) -> u32 {
self.largest_old_line_number
}
pub(crate) const fn deltas(&self) -> &Vec<Delta> {
&self.deltas
}
pub(crate) const fn largest_new_line_number(&self) -> u32 {
self.largest_new_line_number
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn no_deltas() {
let file_stat = FileStat::new("/from/path", "/to/path", Status::Renamed);
assert_eq!(*file_stat.get_status(), Status::Renamed);
assert_eq!(file_stat.get_from_name(), "/from/path");
assert_eq!(file_stat.get_to_name(), "/to/path");
assert_eq!(file_stat.largest_old_line_number(), 0);
assert_eq!(file_stat.largest_new_line_number(), 0);
assert!(file_stat.deltas().is_empty());
}
#[test]
fn add_delta() {
let mut file_stat = FileStat::new("/from/path", "/to/path", Status::Renamed);
file_stat.add_delta(Delta::new("@ src/show_commit/delta.rs:56 @ impl Delta {", 10, 12, 3, 4));
assert_eq!(file_stat.largest_old_line_number(), 13);
assert_eq!(file_stat.largest_new_line_number(), 16);
assert_eq!(file_stat.deltas().len(), 1);
}
#[test]
fn add_delta_with_larger_old_line_number() {
let mut file_stat = FileStat::new("/from/path", "/to/path", Status::Renamed);
file_stat.add_delta(Delta::new("@ src/show_commit/delta.rs:56 @ impl Delta {", 10, 12, 3, 4));
file_stat.add_delta(Delta::new("@ src/show_commit/delta.rs:56 @ impl Delta {", 14, 12, 3, 4));
assert_eq!(file_stat.largest_old_line_number(), 17);
assert_eq!(file_stat.largest_new_line_number(), 16);
assert_eq!(file_stat.deltas().len(), 2);
}
#[test]
fn add_delta_with_larger_new_line_number() {
let mut file_stat = FileStat::new("/from/path", "/to/path", Status::Renamed);
file_stat.add_delta(Delta::new("@ src/show_commit/delta.rs:56 @ impl Delta {", 10, 12, 3, 4));
file_stat.add_delta(Delta::new("@ src/show_commit/delta.rs:56 @ impl Delta {", 10, 17, 3, 4));
assert_eq!(file_stat.largest_old_line_number(), 13);
assert_eq!(file_stat.largest_new_line_number(), 21);
assert_eq!(file_stat.deltas().len(), 2);
}
#[test]
fn add_delta_with_larger_new_and_old_line_number() {
let mut file_stat = FileStat::new("/from/path", "/to/path", Status::Renamed);
file_stat.add_delta(Delta::new("@ src/show_commit/delta.rs:56 @ impl Delta {", 10, 12, 3, 4));
file_stat.add_delta(Delta::new("@ src/show_commit/delta.rs:56 @ impl Delta {", 14, 12, 3, 4));
file_stat.add_delta(Delta::new("@ src/show_commit/delta.rs:56 @ impl Delta {", 10, 17, 3, 4));
assert_eq!(file_stat.largest_old_line_number(), 17);
assert_eq!(file_stat.largest_new_line_number(), 21);
assert_eq!(file_stat.deltas().len(), 3);
}
}
|