Skip to content

Commit

Permalink
Merge pull request #294 from Sh3Rm4n/use
Browse files Browse the repository at this point in the history
Allow use statements in defmt-test
  • Loading branch information
Jonas Schievink authored Dec 2, 2020
2 parents a6edbd1 + f25e2f0 commit ccc05b0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
8 changes: 7 additions & 1 deletion firmware/defmt-test/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ fn tests_impl(args: TokenStream, input: TokenStream) -> parse::Result<TokenStrea

let mut init = None;
let mut tests = vec![];
let mut imports = vec![];
for item in items {
let item_span = item.span();
match item {
Expand Down Expand Up @@ -130,10 +131,14 @@ fn tests_impl(args: TokenStream, input: TokenStream) -> parse::Result<TokenStrea
}
}

Item::Use(u) => {
imports.push(u);
}

_ => {
return Err(parse::Error::new(
item.span(),
"only function items are allowed",
"only `#[test]` functions and imports (`use`) are allowed in this scope",
));
}
}
Expand Down Expand Up @@ -194,6 +199,7 @@ fn tests_impl(args: TokenStream, input: TokenStream) -> parse::Result<TokenStrea
.map(|test| format!(".. {} ok", test.ident))
.collect::<Vec<_>>();
Ok(quote!(mod #ident {
#(#imports)*
// TODO use `cortex-m-rt::entry` here to get the `static mut` transform
#[export_name = "main"]
unsafe extern "C" fn __defmt_test_entry() -> ! {
Expand Down
7 changes: 2 additions & 5 deletions parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,7 @@ pub enum Type {
}

fn is_digit(c: Option<char>) -> bool {
match c.unwrap_or('\0') {
'0'..='9' => true,
_ => false,
}
matches!(c.unwrap_or('\0'), '0'..='9')
}

fn parse_range(mut s: &str) -> Option<(Range<u8>, usize /* consumed */)> {
Expand Down Expand Up @@ -283,7 +280,7 @@ pub fn parse<'f>(format_string: &'f str) -> Result<Vec<Fragment<'f>>, Cow<'stati
}

// Peek at the next char.
if chars.as_str().chars().next() == Some('{') {
if chars.as_str().starts_with('{') {
// Escaped `{{`, also part of a literal fragment.
chars.next(); // Move after both `{`s.
continue;
Expand Down

0 comments on commit ccc05b0

Please sign in to comment.