Skip to content

Commit 3d41f43

Browse files
committed
Always store the line number as indexed from 0, convert only when printing; issue #11
1 parent d87b4a8 commit 3d41f43

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

src/validation.rs

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ impl IssueDefinition {
6464
findings
6565
.map(|(index, _finding)| {
6666
IssueReport {
67-
// Line numbers start from 1, but the enumeration starts from 0. Add 1 to the index
68-
line_number: Some(index + 1),
67+
line_number: Some(index),
6968
description: self.description,
7069
severity: self.severity,
7170
}
@@ -86,7 +85,9 @@ pub struct IssueReport {
8685
impl fmt::Display for IssueReport {
8786
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
8887
let stamp = if let Some(line_number) = self.line_number {
89-
format!("{} at line {}: ", self.severity, line_number)
88+
// Add 1 to the line number because we store the line number as counted from 0,
89+
// but users want to count the first line as line number 1
90+
format!("{} at line {}: ", self.severity, line_number + 1)
9091
} else {
9192
format!("{}: ", self.severity)
9293
};
@@ -180,6 +181,17 @@ fn perform_simple_tests(content: &str, tests: &[IssueDefinition]) -> Vec<IssueRe
180181
.collect()
181182
}
182183

184+
/// This function collects all tests required regardless of the module or assembly type
185+
fn check_common(content: &str) -> Vec<IssueReport> {
186+
let mut reports = Vec::new();
187+
188+
reports.append(title::check(content).as_mut());
189+
reports.append(content::check(content).as_mut());
190+
reports.append(additional_resources::check(content).as_mut());
191+
192+
reports
193+
}
194+
183195
// This section groups all title requirements
184196
mod title {
185197
use crate::validation::find_first_occurrence;
@@ -337,7 +349,7 @@ mod content {
337349
// If the file contains an abstract flag, test for the following paragraph
338350
if let Some((line_no, _line)) = abstract_flag {
339351
let no_paragraph_report = IssueReport {
340-
line_number: Some(line_no + 1),
352+
line_number: Some(line_no),
341353
description: "The _abstract flag is not immediately followed by a paragraph.",
342354
severity: IssueSeverity::Error,
343355
};
@@ -400,17 +412,6 @@ mod content {
400412
}
401413
}
402414

403-
/// This function collects all tests required regardless of the module or assembly type
404-
fn check_common(content: &str) -> Vec<IssueReport> {
405-
let mut reports = Vec::new();
406-
407-
reports.append(title::check(content).as_mut());
408-
reports.append(content::check(content).as_mut());
409-
reports.append(additional_resources::check(content).as_mut());
410-
411-
reports
412-
}
413-
414415
// This section groups all module requirements;
415416
// they depend on title and content, and additional resources requirements
416417
mod module {
@@ -504,14 +505,14 @@ mod module {
504505
if let Some(_snippet) = snip_include_regex.find(include.as_str()) {
505506
// In this case, the detected include is most likely a snippet. Report as Information
506507
let report = IssueReport {
507-
line_number: Some(index + 1),
508+
line_number: Some(index),
508509
description: "This module includes a file that appears to be a snippet. This is supported.",
509510
severity: IssueSeverity::Information,
510511
};
511512
reports.push(report);
512513
} else {
513514
let report = IssueReport {
514-
line_number: Some(index + 1),
515+
line_number: Some(index),
515516
description:
516517
"This module includes a file that does not appear to be a snippet.",
517518
severity: IssueSeverity::Error,
@@ -592,7 +593,7 @@ mod assembly {
592593
// This line is a standard heading. No report.
593594
} else {
594595
// This is a non-standard heading. Record the line number for a report.
595-
lines_with_heading.push(index + 1);
596+
lines_with_heading.push(index);
596597
}
597598
}
598599
});
@@ -637,16 +638,14 @@ mod additional_resources {
637638
// Prepare the lines vector in advance so that the following functions
638639
// don't have to split the files again on their own.
639640
let lines: Vec<&str> = content.lines().collect();
640-
// Translate the human-readable index to a 0-based index.
641-
let index_from_0 = index - 1;
642641

643642
// Collect the issues found by the particular functions.
644-
if let Some(issue) = check_add_res_flag(&lines, index_from_0) {
643+
if let Some(issue) = check_add_res_flag(&lines, index) {
645644
issues.push(issue);
646645
}
647-
issues.append(check_paragraphs_in_add_res(&lines, index_from_0).as_mut());
648-
issues.append(check_link_labels_in_add_res(&lines, index_from_0).as_mut());
649-
issues.append(check_additional_resource_length(&lines, index_from_0).as_mut());
646+
issues.append(check_paragraphs_in_add_res(&lines, index).as_mut());
647+
issues.append(check_link_labels_in_add_res(&lines, index).as_mut());
648+
issues.append(check_additional_resource_length(&lines, index).as_mut());
650649
}
651650

652651
issues
@@ -674,7 +673,7 @@ mod additional_resources {
674673
// If the preceding line is anything else than the flag, report the missing flag.
675674
} else {
676675
Some(IssueReport {
677-
line_number: Some(heading_index + 1),
676+
line_number: Some(heading_index),
678677
description: "The additional resources heading is not immediately preceded by the _additional-resources flag.",
679678
severity: IssueSeverity::Error,
680679
})
@@ -700,14 +699,16 @@ mod additional_resources {
700699
// Report empty lines found before the first list item.
701700
} else if empty_line_regex.is_match(line) {
702701
issues.push(IssueReport {
703-
line_number: Some(heading_index + offset + 2),
702+
// Add 1 because the offset starts counting the first line that follows the heading from 0
703+
line_number: Some(heading_index + offset + 1),
704704
description: "The additional resources section includes an empty line.",
705705
severity: IssueSeverity::Error,
706706
});
707707
// Report unallowed paragraphs before the first list item.
708708
} else if !allowed_paragraph.is_match(line) {
709709
issues.push(IssueReport {
710-
line_number: Some(heading_index + offset + 2),
710+
// Add 1 because the offset starts counting the first line that follows the heading from 0
711+
line_number: Some(heading_index + offset + 1),
711712
description: "The additional resources section includes a plain paragraph.",
712713
severity: IssueSeverity::Error,
713714
});
@@ -716,7 +717,7 @@ mod additional_resources {
716717

717718
// If no list items have appeared until the end of the file, report that as the final issue.
718719
issues.push(IssueReport {
719-
line_number: Some(heading_index + 1),
720+
line_number: Some(heading_index),
720721
description: "The additional resources section includes no list items.",
721722
severity: IssueSeverity::Error,
722723
});
@@ -734,7 +735,7 @@ mod additional_resources {
734735
for (offset, &line) in lines[heading_index + 1..].iter().enumerate() {
735736
if link_regex.is_match(line) {
736737
issues.push(IssueReport {
737-
line_number: Some(heading_index + offset + 2),
738+
line_number: Some(heading_index + offset + 1),
738739
description:
739740
"The additional resources section includes a link without a label.",
740741
severity: IssueSeverity::Error,
@@ -769,7 +770,7 @@ mod additional_resources {
769770

770771
if number_of_words > maximum_words {
771772
issues.push(IssueReport {
772-
line_number: Some(heading_index + offset + 2),
773+
line_number: Some(heading_index + offset + 1),
773774
description:
774775
"The additional resource is long. Try to limit it to a couple of words.",
775776
severity: IssueSeverity::Warning,
@@ -794,7 +795,7 @@ fn find_mod_id(content: &str) -> Option<(usize, &str)> {
794795
fn find_first_occurrence(content: &str, regex: Regex) -> Option<(usize, &str)> {
795796
for (index, line) in content.lines().enumerate() {
796797
if let Some(_occurrence) = regex.find(line) {
797-
return Some((index + 1, line));
798+
return Some((index, line));
798799
}
799800
}
800801
None
@@ -821,8 +822,7 @@ fn line_from_byte_no(content: &str, byte_no: usize) -> Option<usize> {
821822
for _byte in line.bytes() {
822823
total_bytes += 1;
823824
if total_bytes == byte_no {
824-
// Line numbers start from 1, but the enumeration starts from 0. Add 1 to the index
825-
return Some(line_index + 1);
825+
return Some(line_index);
826826
}
827827
}
828828
}

0 commit comments

Comments
 (0)