rust-diff-analyzer 1.6.0

Semantic analyzer for Rust PR diffs that distinguishes production code from test code
Documentation
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// SPDX-FileCopyrightText: 2025 RAprogramm <andrey.rozanov.vl@gmail.com>
// SPDX-License-Identifier: MIT

use std::path::PathBuf;

use masterror::AppError;

use super::hunk::{Hunk, HunkLine};
use crate::error::DiffParseError;

/// A file diff containing all hunks for a single file
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FileDiff {
    /// Path to the file (new path if renamed)
    pub path: PathBuf,
    /// Original path (if renamed)
    pub old_path: Option<PathBuf>,
    /// Hunks in this file diff
    pub hunks: Vec<Hunk>,
}

impl FileDiff {
    /// Creates a new file diff
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the file
    ///
    /// # Returns
    ///
    /// A new FileDiff with empty hunks
    ///
    /// # Examples
    ///
    /// ```
    /// use std::path::PathBuf;
    ///
    /// use rust_diff_analyzer::git::FileDiff;
    ///
    /// let diff = FileDiff::new(PathBuf::from("src/lib.rs"));
    /// assert!(diff.hunks.is_empty());
    /// ```
    pub fn new(path: PathBuf) -> Self {
        Self {
            path,
            old_path: None,
            hunks: Vec::new(),
        }
    }

    /// Returns total number of added lines
    ///
    /// # Returns
    ///
    /// Sum of added lines across all hunks
    ///
    /// # Examples
    ///
    /// ```
    /// use std::path::PathBuf;
    ///
    /// use rust_diff_analyzer::git::FileDiff;
    ///
    /// let diff = FileDiff::new(PathBuf::from("src/lib.rs"));
    /// assert_eq!(diff.total_added(), 0);
    /// ```
    pub fn total_added(&self) -> usize {
        self.hunks.iter().map(|h| h.added_count()).sum()
    }

    /// Returns total number of removed lines
    ///
    /// # Returns
    ///
    /// Sum of removed lines across all hunks
    ///
    /// # Examples
    ///
    /// ```
    /// use std::path::PathBuf;
    ///
    /// use rust_diff_analyzer::git::FileDiff;
    ///
    /// let diff = FileDiff::new(PathBuf::from("src/lib.rs"));
    /// assert_eq!(diff.total_removed(), 0);
    /// ```
    pub fn total_removed(&self) -> usize {
        self.hunks.iter().map(|h| h.removed_count()).sum()
    }

    /// Returns all added line numbers
    ///
    /// # Returns
    ///
    /// Vector of all added line numbers
    ///
    /// # Examples
    ///
    /// ```
    /// use std::path::PathBuf;
    ///
    /// use rust_diff_analyzer::git::FileDiff;
    ///
    /// let diff = FileDiff::new(PathBuf::from("src/lib.rs"));
    /// assert!(diff.all_added_lines().is_empty());
    /// ```
    pub fn all_added_lines(&self) -> Vec<usize> {
        self.hunks.iter().flat_map(|h| h.added_lines()).collect()
    }

    /// Returns all removed line numbers
    ///
    /// # Returns
    ///
    /// Vector of all removed line numbers
    ///
    /// # Examples
    ///
    /// ```
    /// use std::path::PathBuf;
    ///
    /// use rust_diff_analyzer::git::FileDiff;
    ///
    /// let diff = FileDiff::new(PathBuf::from("src/lib.rs"));
    /// assert!(diff.all_removed_lines().is_empty());
    /// ```
    pub fn all_removed_lines(&self) -> Vec<usize> {
        self.hunks.iter().flat_map(|h| h.removed_lines()).collect()
    }

    /// Checks if file path ends with .rs extension
    ///
    /// # Returns
    ///
    /// `true` if file is a Rust source file
    ///
    /// # Examples
    ///
    /// ```
    /// use std::path::PathBuf;
    ///
    /// use rust_diff_analyzer::git::FileDiff;
    ///
    /// let diff = FileDiff::new(PathBuf::from("src/lib.rs"));
    /// assert!(diff.is_rust_file());
    ///
    /// let diff = FileDiff::new(PathBuf::from("README.md"));
    /// assert!(!diff.is_rust_file());
    /// ```
    pub fn is_rust_file(&self) -> bool {
        self.path
            .extension()
            .map(|ext| ext == "rs")
            .unwrap_or(false)
    }
}

/// Parses unified diff format into structured file diffs
///
/// # Arguments
///
/// * `input` - Unified diff content as string
///
/// # Returns
///
/// Vector of file diffs or parse error
///
/// # Errors
///
/// Returns error if diff format is invalid
///
/// # Examples
///
/// ```
/// use rust_diff_analyzer::git::parse_diff;
///
/// let diff = r#"diff --git a/src/lib.rs b/src/lib.rs
/// index 1234567..abcdefg 100644
/// --- a/src/lib.rs
/// +++ b/src/lib.rs
/// @@ -1,3 +1,4 @@
///  fn main() {
/// +    println!("Hello");
///  }
/// "#;
///
/// let files = parse_diff(diff).unwrap();
/// assert_eq!(files.len(), 1);
/// ```
pub fn parse_diff(input: &str) -> Result<Vec<FileDiff>, AppError> {
    let mut files = Vec::new();
    let mut current_file: Option<FileDiff> = None;
    let mut current_hunk: Option<Hunk> = None;
    let mut old_line = 0;
    let mut new_line = 0;

    for line in input.lines() {
        if line.starts_with("diff --git") {
            if let Some(mut file) = current_file.take() {
                if let Some(hunk) = current_hunk.take() {
                    file.hunks.push(hunk);
                }
                files.push(file);
            }

            let path = parse_diff_header(line)?;
            current_file = Some(FileDiff::new(path));
            current_hunk = None;
        } else if line.starts_with("@@") {
            if let Some(ref mut file) = current_file {
                if let Some(hunk) = current_hunk.take() {
                    file.hunks.push(hunk);
                }

                let (old_start, old_count, new_start, new_count) = parse_hunk_header(line)?;
                current_hunk = Some(Hunk::new(old_start, old_count, new_start, new_count));
                old_line = old_start;
                new_line = new_start;
            }
        } else if let Some(ref mut hunk) = current_hunk
            && let Some(first_char) = line.chars().next()
        {
            let content = if line.len() > 1 {
                line[1..].to_string()
            } else {
                String::new()
            };

            match first_char {
                '+' => {
                    hunk.lines.push(HunkLine::added(new_line, content));
                    new_line += 1;
                }
                '-' => {
                    hunk.lines.push(HunkLine::removed(old_line, content));
                    old_line += 1;
                }
                ' ' => {
                    hunk.lines
                        .push(HunkLine::context(old_line, new_line, content));
                    old_line += 1;
                    new_line += 1;
                }
                '\\' => {}
                _ => {}
            }
        }
    }

    if let Some(mut file) = current_file {
        if let Some(hunk) = current_hunk {
            file.hunks.push(hunk);
        }
        files.push(file);
    }

    Ok(files)
}

fn parse_diff_header(line: &str) -> Result<PathBuf, AppError> {
    let parts: Vec<&str> = line.split_whitespace().collect();
    if parts.len() < 4 {
        return Err(DiffParseError {
            message: format!("invalid diff header: {}", line),
        }
        .into());
    }

    let b_path = parts[3];
    let path = b_path.strip_prefix("b/").unwrap_or(b_path);
    Ok(PathBuf::from(path))
}

fn parse_hunk_header(line: &str) -> Result<(usize, usize, usize, usize), AppError> {
    let line = line
        .strip_prefix("@@")
        .and_then(|s| s.split("@@").next())
        .ok_or_else(|| {
            AppError::from(DiffParseError {
                message: format!("invalid hunk header: {}", line),
            })
        })?
        .trim();

    let parts: Vec<&str> = line.split_whitespace().collect();
    if parts.len() < 2 {
        return Err(DiffParseError {
            message: format!("invalid hunk header: {}", line),
        }
        .into());
    }

    let old_range = parts[0].strip_prefix('-').ok_or_else(|| {
        AppError::from(DiffParseError {
            message: format!("invalid old range: {}", parts[0]),
        })
    })?;

    let new_range = parts[1].strip_prefix('+').ok_or_else(|| {
        AppError::from(DiffParseError {
            message: format!("invalid new range: {}", parts[1]),
        })
    })?;

    let (old_start, old_count) = parse_range(old_range)?;
    let (new_start, new_count) = parse_range(new_range)?;

    Ok((old_start, old_count, new_start, new_count))
}

fn parse_range(range: &str) -> Result<(usize, usize), AppError> {
    let parts: Vec<&str> = range.split(',').collect();

    let start = parts[0].parse::<usize>().map_err(|_| {
        AppError::from(DiffParseError {
            message: format!("invalid line number: {}", parts[0]),
        })
    })?;

    let count = if parts.len() > 1 {
        parts[1].parse::<usize>().map_err(|_| {
            AppError::from(DiffParseError {
                message: format!("invalid line count: {}", parts[1]),
            })
        })?
    } else {
        1
    };

    Ok((start, count))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_simple_diff() {
        let diff = r#"diff --git a/src/lib.rs b/src/lib.rs
index 1234567..abcdefg 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,3 +1,4 @@
 fn main() {
+    println!("Hello");
 }
"#;

        let files = parse_diff(diff).expect("parse should succeed");
        assert_eq!(files.len(), 1);
        assert_eq!(files[0].path, PathBuf::from("src/lib.rs"));
        assert_eq!(files[0].hunks.len(), 1);
        assert_eq!(files[0].total_added(), 1);
    }

    #[test]
    fn test_parse_multiple_hunks() {
        let diff = r#"diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,3 +1,4 @@
 fn main() {
+    println!("Hello");
 }
@@ -10,2 +11,3 @@
 fn test() {
+    assert!(true);
 }
"#;

        let files = parse_diff(diff).expect("parse should succeed");
        assert_eq!(files[0].hunks.len(), 2);
        assert_eq!(files[0].total_added(), 2);
    }

    #[test]
    fn test_parse_multiple_files() {
        let diff = r#"diff --git a/src/a.rs b/src/a.rs
--- a/src/a.rs
+++ b/src/a.rs
@@ -1,1 +1,2 @@
 fn a() {}
+fn a2() {}
diff --git a/src/b.rs b/src/b.rs
--- a/src/b.rs
+++ b/src/b.rs
@@ -1,1 +1,2 @@
 fn b() {}
+fn b2() {}
"#;

        let files = parse_diff(diff).expect("parse should succeed");
        assert_eq!(files.len(), 2);
        assert_eq!(files[0].path, PathBuf::from("src/a.rs"));
        assert_eq!(files[1].path, PathBuf::from("src/b.rs"));
    }

    #[test]
    fn test_is_rust_file() {
        let rust_diff = FileDiff::new(PathBuf::from("src/lib.rs"));
        assert!(rust_diff.is_rust_file());

        let md_diff = FileDiff::new(PathBuf::from("README.md"));
        assert!(!md_diff.is_rust_file());
    }
}