Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

requirements.authoring.line_numbers.detection

Requirements index · View definition

evidence Valid

Requirement

A rationale MUST be treated as citing line numbers when it contains a file path followed by : and a digit, the word line or lines in any case followed by a number, or a parenthesized number range such as (12–30) or (12-30).

Rationale

The requirement has one MUST with three detection forms. Each is implemented by the LINE_NUMBERS regex in src/authoring/line_numbers.rs#static[LINE_NUMBERS]. src/authoring/line_numbers.rs#fn[cited] applies that regex to a rationale and returns the first match. Both items carry the {impl} reference.

  1. File path followed by : and a digit. The first alternative, (?:[\w.-]+/)*[\w-]+\.[A-Za-z][A-Za-z0-9]*:\d, matches src/lib.rs:12 and Cargo.toml:4. Requiring the extension to start with a letter rejects version-like text such as 0.1:9. It also rejects 12.30:15.
  2. The word line or lines in any case, followed by a number. The second alternative, (?i:\blines?)\s+\d, is case-insensitive and uses a word boundary. It matches on line 7 and Lines 10 to 12. It does not match deadline 7 or inline 3, and two lines of output has no number after it.
  3. A parenthesized number range. The third alternative, \(\s*\d+\s*[-–]\s*\d+\s*\), accepts both the en dash and the hyphen. It matches (12–30), (12-30) and ( 3 - 4 ), but not (1).

Testing. mod[tests]/fn[line_numbers_are_detected_in_each_form] carries the {test} reference. It asserts that cited detects every form, including the hyphen and en dash ranges and the mixed-case Lines. It also asserts that cited returns None for near-miss inputs:

  • bare paths such as src/lib.rs#fn[check]
  • deadline 7 days
  • 3:4
  • 0.1:9
  • (1)

It also checks that the first match is the one returned.

Other points.

  • I found no unspecified observable behavior. Returning the first matched text is a natural way to expose the detection.
  • Every obligation is covered by an {impl} item and a test, so I found no gaps.

Inputs

Implementation

Input 2: outline src/authoring/line_numbers.rs static[LINE_NUMBERS]
/// The forms of a line-number citation: a file path followed by `:` and a
/// digit, `line` or `lines` followed by a number, or a parenthesized number
/// range.
// {impl}`requirements.authoring.line_numbers.detection`
static LINE_NUMBERS: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(concat!(
        // A file path's extension starts with a letter, so a version
        // such as `0.1:9` is not one.
        r"(?:[\w.-]+/)*[\w-]+\.[A-Za-z][A-Za-z0-9]*:\d",
        r"|(?i:\blines?)\s+\d",
        r"|\(\s*\d+\s*[-–]\s*\d+\s*\)",
    ))
    .expect("line-number pattern")
});
Input 3: outline src/authoring/line_numbers.rs fn[cited]
/// The first text in `rationale` that cites line numbers, if any.
// {impl}`requirements.authoring.line_numbers.detection`
pub(crate) fn cited(rationale: &str) -> Option<&str> {
    LINE_NUMBERS.find(rationale).map(|found| found.as_str())
}

Tests

Input 4: outline src/authoring/line_numbers.rs mod[tests]/fn[line_numbers_are_detected_in_each_form]
    // {test}`requirements.authoring.line_numbers.detection`
    #[test]
    fn line_numbers_are_detected_in_each_form() {
        for text in [
            "see src/lib.rs:12",
            "`tests/cli.rs:3-9` covers it",
            "Cargo.toml:4",
            "on line 7",
            "Lines 10 to 12",
            "`check` (12–30) handles it",
            "`check` (12-30)",
            "( 3 - 4 )",
        ] {
            assert!(cited(text).is_some(), "{text}");
        }
        for text in [
            "`src/lib.rs#fn[check]` covers it",
            "`src/lib.rs` and `requirements.ci.errors`",
            "the deadline 7 days",
            "inline 3 items",
            "two lines of output",
            "(see the example)",
            "a ratio of 3:4",
            "version 0.1:9",
            "at 12.30:15",
            "(1)",
        ] {
            assert_eq!(cited(text), None, "{text}");
        }
        assert_eq!(cited("at src/a.rs:5 and line 3"), Some("src/a.rs:5"));
    }

Other sources

Input 1: requirements selection requirements.authoring.line_numbers.detection
:::{req} line_numbers.detection
A rationale MUST be treated as citing line numbers when it contains a file path
followed by `:` and a digit, the word `line` or `lines` in any case followed by
a number, or a parenthesized number range such as `(12–30)` or `(12-30)`.
:::