Skip to content

Commit

Permalink
Flatten the parse logic in line_directive
Browse files Browse the repository at this point in the history
  • Loading branch information
Zalathar committed Feb 21, 2024
1 parent 99fb653 commit 544d091
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions src/tools/compiletest/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,27 +650,22 @@ impl TestProps {
/// See [`HeaderLine`] for a diagram.
pub fn line_directive<'line>(
comment: &str,
ln: &'line str,
original_line: &'line str,
) -> Option<(Option<&'line str>, &'line str)> {
let ln = ln.trim_start();
if ln.starts_with(comment) {
let ln = ln[comment.len()..].trim_start();
if ln.starts_with('[') {
// A comment like `//[foo]` is specific to revision `foo`
let Some(close_brace) = ln.find(']') else {
panic!(
"malformed condition directive: expected `{}[foo]`, found `{}`",
comment, ln
);
};
// Ignore lines that don't start with the comment prefix.
let after_comment = original_line.trim_start().strip_prefix(comment)?.trim_start();

if let Some(after_open_bracket) = after_comment.strip_prefix('[') {
// A comment like `//@[foo]` only applies to revision `foo`.
let Some((line_revision, directive)) = after_open_bracket.split_once(']') else {
panic!(
"malformed condition directive: expected `{comment}[foo]`, found `{original_line}`"
)
};

let line_revision = &ln[1..close_brace];
Some((Some(line_revision), ln[(close_brace + 1)..].trim_start()))
} else {
Some((None, ln))
}
Some((Some(line_revision), directive.trim_start()))
} else {
None
Some((None, after_comment))
}
}

Expand Down

0 comments on commit 544d091

Please sign in to comment.