rust-diff-analyzer 2.0.1

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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
// SPDX-FileCopyrightText: 2025 RAprogramm <andrey.rozanov.vl@gmail.com>
// SPDX-License-Identifier: MIT

use std::path::{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>,
    /// Whether the file was deleted in this diff
    pub is_deleted: bool,
    /// 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,
            is_deleted: false,
            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()
    }

    /// Returns new-file line positions of all removals across hunks
    ///
    /// See [`Hunk::removed_positions_in_new`] for the attribution rule.
    ///
    /// # Returns
    ///
    /// Vector of new-file line numbers, one per removed line
    ///
    /// # Examples
    ///
    /// ```
    /// use std::path::PathBuf;
    ///
    /// use rust_diff_analyzer::git::FileDiff;
    ///
    /// let diff = FileDiff::new(PathBuf::from("src/lib.rs"));
    /// assert!(diff.all_removed_positions_in_new().is_empty());
    /// ```
    pub fn all_removed_positions_in_new(&self) -> Vec<usize> {
        self.hunks
            .iter()
            .flat_map(|h| h.removed_positions_in_new())
            .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 mut chars = line.chars();
            match chars.next() {
                Some('+') => {
                    hunk.lines
                        .push(HunkLine::added(new_line, chars.as_str().to_string()));
                    new_line += 1;
                }
                Some('-') => {
                    hunk.lines
                        .push(HunkLine::removed(old_line, chars.as_str().to_string()));
                    old_line += 1;
                }
                Some(' ') | None => {
                    hunk.lines.push(HunkLine::context(
                        old_line,
                        new_line,
                        chars.as_str().to_string(),
                    ));
                    old_line += 1;
                    new_line += 1;
                }
                Some(_) => {}
            }
        } else if let Some(ref mut file) = current_file {
            apply_file_metadata(line, file);
        }
    }

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

    Ok(files)
}

/// Applies file-level metadata lines (`---`, `+++`, `rename`, `deleted file
/// mode`) that appear between a `diff --git` header and the first hunk.
fn apply_file_metadata(line: &str, file: &mut FileDiff) {
    if let Some(rest) = line.strip_prefix("+++ ") {
        let target = extract_path_token(rest);
        if target == "/dev/null" {
            file.is_deleted = true;
        } else {
            let path = target.strip_prefix("b/").unwrap_or(&target);
            file.path = PathBuf::from(path);
            if file.old_path.as_deref() == Some(file.path.as_path()) {
                file.old_path = None;
            }
        }
    } else if let Some(rest) = line.strip_prefix("--- ") {
        let target = extract_path_token(rest);
        if target != "/dev/null" {
            let path = target.strip_prefix("a/").unwrap_or(&target);
            if Path::new(path) != file.path {
                file.old_path = Some(PathBuf::from(path));
            }
        }
    } else if let Some(rest) = line.strip_prefix("rename from ") {
        file.old_path = Some(PathBuf::from(unquote_git_path(rest.trim_end())));
    } else if let Some(rest) = line.strip_prefix("rename to ") {
        file.path = PathBuf::from(unquote_git_path(rest.trim_end()));
    } else if line.starts_with("deleted file mode") {
        file.is_deleted = true;
    }
}

/// Extracts a path from the remainder of a `---`/`+++` line, unquoting
/// git-quoted paths and dropping a trailing tab-separated timestamp.
fn extract_path_token(rest: &str) -> String {
    if rest.starts_with('"') {
        unquote_git_path(rest.trim_end())
    } else {
        rest.split('\t')
            .next()
            .unwrap_or(rest)
            .trim_end()
            .to_string()
    }
}

/// Decodes a git-quoted path (`"a/\321\204.rs"`) into its unquoted form.
///
/// Handles the C-style escapes git emits with `core.quotepath=true`:
/// `\\`, `\"`, `\t`, `\n`, `\r` and octal byte escapes (`\321`). Input
/// without surrounding double quotes is returned unchanged.
fn unquote_git_path(raw: &str) -> String {
    let inner = match raw.strip_prefix('"').and_then(|s| s.strip_suffix('"')) {
        Some(inner) => inner,
        None => return raw.to_string(),
    };

    let mut bytes = Vec::with_capacity(inner.len());
    let mut iter = inner.bytes().peekable();
    while let Some(byte) = iter.next() {
        if byte != b'\\' {
            bytes.push(byte);
            continue;
        }
        match iter.next() {
            Some(b'n') => bytes.push(b'\n'),
            Some(b't') => bytes.push(b'\t'),
            Some(b'r') => bytes.push(b'\r'),
            Some(digit @ b'0'..=b'7') => {
                let mut value = u32::from(digit - b'0');
                for _ in 0..2 {
                    match iter.peek().copied() {
                        Some(next @ b'0'..=b'7') => {
                            value = value * 8 + u32::from(next - b'0');
                            iter.next();
                        }
                        _ => break,
                    }
                }
                bytes.push(value as u8);
            }
            Some(other) => bytes.push(other),
            None => {}
        }
    }

    String::from_utf8_lossy(&bytes).into_owned()
}

fn parse_diff_header(line: &str) -> Result<PathBuf, AppError> {
    let invalid_header = || {
        AppError::from(DiffParseError {
            message: format!("invalid diff header: {}", line),
        })
    };

    let rest = line
        .strip_prefix("diff --git ")
        .ok_or_else(invalid_header)?;
    let b_part = if rest.ends_with('"') {
        rest.rfind(" \"").map(|pos| &rest[pos + 1..])
    } else {
        rest.rfind(" b/").map(|pos| &rest[pos + 1..])
    };
    let b_part = b_part
        .or_else(|| rest.split_whitespace().nth(1))
        .ok_or_else(invalid_header)?;

    let unquoted = unquote_git_path(b_part);
    let path = unquoted.strip_prefix("b/").unwrap_or(&unquoted);
    Ok(PathBuf::from(path))
}

fn parse_hunk_header(line: &str) -> Result<(usize, usize, usize, usize), AppError> {
    let invalid_header = || {
        AppError::from(DiffParseError {
            message: format!("invalid hunk header: {}", line),
        })
    };

    let ranges = line
        .strip_prefix("@@")
        .ok_or_else(invalid_header)?
        .split("@@")
        .next()
        .unwrap_or("")
        .trim();

    let mut parts = ranges.split_whitespace();
    let old_part = parts.next().ok_or_else(invalid_header)?;
    let new_part = parts.next().ok_or_else(invalid_header)?;

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

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

    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 (start_part, count_part) = match range.split_once(',') {
        Some((start, count)) => (start, Some(count)),
        None => (range, None),
    };

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

    let count = match count_part {
        Some(count) => count.parse::<usize>().map_err(|_| {
            AppError::from(DiffParseError {
                message: format!("invalid line count: {}", count),
            })
        })?,
        None => 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());
    }

    #[test]
    fn test_parse_multibyte_content_does_not_panic() {
        let diff = "diff --git a/src/lib.rs b/src/lib.rs\n--- a/src/lib.rs\n+++ b/src/lib.rs\n@@ \
                    -1,2 +1,2 @@\n контекст\n-старая строка\n+новая строка\nПривет без префикса\n";

        let files = parse_diff(diff).expect("parse should succeed");
        assert_eq!(files[0].total_added(), 1);
        assert_eq!(files[0].total_removed(), 1);
        let hunk = &files[0].hunks[0];
        assert_eq!(hunk.added_lines(), vec![2]);
        assert_eq!(hunk.removed_lines(), vec![2]);
    }

    #[test]
    fn test_parse_deleted_file() {
        let diff = "diff --git a/src/old.rs b/src/old.rs\ndeleted file mode 100644\nindex \
                    1234567..0000000\n--- a/src/old.rs\n+++ /dev/null\n@@ -1,2 +0,0 @@\n-fn \
                    gone() {}\n-fn also_gone() {}\n";

        let files = parse_diff(diff).expect("parse should succeed");
        assert!(files[0].is_deleted);
        assert_eq!(files[0].path, PathBuf::from("src/old.rs"));
        assert_eq!(files[0].total_removed(), 2);
    }

    #[test]
    fn test_parse_new_file_is_not_deleted() {
        let diff = "diff --git a/src/new.rs b/src/new.rs\nnew file mode 100644\n--- \
                    /dev/null\n+++ b/src/new.rs\n@@ -0,0 +1,1 @@\n+fn fresh() {}\n";

        let files = parse_diff(diff).expect("parse should succeed");
        assert!(!files[0].is_deleted);
        assert!(files[0].old_path.is_none());
        assert_eq!(files[0].path, PathBuf::from("src/new.rs"));
    }

    #[test]
    fn test_parse_renamed_file() {
        let diff = "diff --git a/src/before.rs b/src/after.rs\nsimilarity index 90%\nrename from \
                    src/before.rs\nrename to src/after.rs\n--- a/src/before.rs\n+++ \
                    b/src/after.rs\n@@ -1,1 +1,2 @@\n fn kept() {}\n+fn added() {}\n";

        let files = parse_diff(diff).expect("parse should succeed");
        assert_eq!(files[0].path, PathBuf::from("src/after.rs"));
        assert_eq!(files[0].old_path, Some(PathBuf::from("src/before.rs")));
        assert!(!files[0].is_deleted);
    }

    #[test]
    fn test_parse_path_with_spaces() {
        let diff = "diff --git a/src/my file.rs b/src/my file.rs\n--- a/src/my file.rs\n+++ \
                    b/src/my file.rs\n@@ -1,1 +1,2 @@\n fn a() {}\n+fn b() {}\n";

        let files = parse_diff(diff).expect("parse should succeed");
        assert_eq!(files[0].path, PathBuf::from("src/my file.rs"));
        assert!(files[0].old_path.is_none());
    }

    #[test]
    fn test_parse_quoted_path_with_octal_escapes() {
        let diff = "diff --git \"a/src/\\321\\204.rs\" \"b/src/\\321\\204.rs\"\n--- \
                    \"a/src/\\321\\204.rs\"\n+++ \"b/src/\\321\\204.rs\"\n@@ -1,1 +1,2 @@\n fn \
                    a() {}\n+fn b() {}\n";

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

    #[test]
    fn test_unquote_escapes() {
        assert_eq!(unquote_git_path("plain/path.rs"), "plain/path.rs");
        assert_eq!(unquote_git_path("\"a b.rs\""), "a b.rs");
        assert_eq!(unquote_git_path("\"tab\\there\""), "tab\there");
        assert_eq!(unquote_git_path("\"q\\\"uote\""), "q\"uote");
        assert_eq!(unquote_git_path("\"\\321\\204\""), "ф");
    }

    #[test]
    fn test_removed_line_numbers_across_hunks() {
        let diff = "diff --git a/src/lib.rs b/src/lib.rs\n--- a/src/lib.rs\n+++ b/src/lib.rs\n@@ \
                    -1,3 +1,2 @@\n fn a() {}\n-fn removed_one() {}\n fn c() {}\n@@ -10,3 +9,2 \
                    @@\n fn x() {}\n-fn removed_two() {}\n fn z() {}\n";

        let files = parse_diff(diff).expect("parse should succeed");
        assert_eq!(files[0].all_removed_lines(), vec![2, 11]);
        assert!(files[0].all_added_lines().is_empty());
    }

    #[test]
    fn test_empty_line_in_hunk_is_empty_context() {
        let diff = "diff --git a/src/lib.rs b/src/lib.rs\n--- a/src/lib.rs\n+++ b/src/lib.rs\n@@ \
                    -1,3 +1,4 @@\n fn a() {}\n\n+fn b() {}\n fn c() {}\n";

        let files = parse_diff(diff).expect("parse should succeed");
        assert_eq!(files[0].all_added_lines(), vec![3]);
    }
}