Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
Signed-off-by: xxchan <[email protected]>
  • Loading branch information
xxchan committed May 16, 2024
1 parent 0416fe2 commit 7178666
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,34 +346,42 @@ impl Expect {

let literal_start = literal_start + (lit_to_eof.len() - lit_to_eof_trimmed.len());

let literal_len =
locate_end(lit_to_eof_trimmed).expect("Couldn't find closing delimiter for `expect!`.");
let literal_len = locate_end(lit_to_eof_trimmed);
let literal_range = literal_start..literal_start + literal_len;
Location { line_indent, literal_range }
}
}

/// Returns the byte index of the closing delimiter.
///
/// `arg_start_to_eof` is the part after `expect![`, with leading whitespaces trimmed.
fn locate_end(arg_start_to_eof: &str) -> Option<usize> {
match arg_start_to_eof.chars().next()? {
/// `arg_start_to_eof` is the part after `expect![` until the closing delimiter
/// with leading whitespaces trimmed.
/// Note that we can actually assume the syntax is valid according to `macro_rules`.
fn locate_end(arg_start_to_eof: &str) -> usize {
let mut chars = arg_start_to_eof.chars();
match chars.next().expect("after `expect![` there should be a char") {
c if c.is_whitespace() => panic!("skip whitespace before calling `locate_end`"),

// expect![[]]
'[' => {
let str_start_to_eof = arg_start_to_eof[1..].trim_start();
let str_len = find_str_lit_len(str_start_to_eof).unwrap_or(0);
let str_len = if ']' == chars.next().expect("after `expect![[` there should be a char")
{
0
} else {
find_str_lit_len(str_start_to_eof).expect("invalid string literal in `expect![[`")
};
let str_end_to_eof = &str_start_to_eof[str_len..];
let closing_brace_offset = str_end_to_eof.find(']')?;
Some((arg_start_to_eof.len() - str_end_to_eof.len()) + closing_brace_offset + 1)
let closing_brace_offset =
str_end_to_eof.find(']').expect("closing `]` not found after `expect![[`");
(arg_start_to_eof.len() - str_end_to_eof.len()) + closing_brace_offset + 1
}

// expect![] | expect!{} | expect!()
']' | '}' | ')' => Some(0),
']' | '}' | ')' => 0,

// expect!["..."] | expect![r#"..."#]
_ => find_str_lit_len(arg_start_to_eof),
_ => find_str_lit_len(arg_start_to_eof).expect("invalid string literal after `expect![`"),
}
}

Expand Down Expand Up @@ -843,7 +851,7 @@ line1
let lit = stringify!($s);
let with_trailer = format!("[{} \t]]\n", lit);
// ^ ^^ ^^ 5 additional chars
assert_eq!(locate_end(&with_trailer), Some(4+lit.len()));
assert_eq!(locate_end(&with_trailer), 4+lit.len());
})*};
}

Expand All @@ -856,9 +864,14 @@ line1
);

// Check `expect![[ ]]`
assert_eq!(locate_end("[]]"), Some(2));
assert_eq!(locate_end("[]]"), 2);
// Check `expect![ ]`
assert_eq!(locate_end("]"), Some(0));
assert_eq!(locate_end("]"), 0);

// `locate_end` returns after the closing delimiter.
assert_eq!(locate_end("]abc"), 0);
// This is actually invalid syntax.
assert_eq!(locate_end("]]abc"), 0);
}

#[test]
Expand Down

0 comments on commit 7178666

Please sign in to comment.