Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions esp-hal-procmacros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ documentation = "https://docs.espressif.com/projects/rust/esp-hal-procmacros/lat
repository = "https://github.com/esp-rs/esp-hal"
license = "MIT OR Apache-2.0"

exclude = ["testdata"]

[package.metadata.espressif]
doc-config = { features = [] }
check-configs = [
Expand Down
79 changes: 68 additions & 11 deletions esp-hal-procmacros/src/blocking.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
#![deny(warnings)]

use proc_macro::TokenStream;
use proc_macro2::Span;
use proc_macro2::{Span, TokenStream};
use syn::{
ItemFn,
parse::{self},
parse_macro_input,
};

/// Marks the firmware entry point.
pub fn main(args: TokenStream, input: TokenStream) -> TokenStream {
let f = parse_macro_input!(input as ItemFn);

pub fn main(args: TokenStream, f: ItemFn) -> TokenStream {
if f.sig.asyncness.is_some() {
return parse::Error::new(
Span::call_site(),
"If you want to use `async` please use `esp-rtos`'s `#[esp_rtos::main]` macro instead.",
)
.to_compile_error()
.into();
.to_compile_error();
}

if !args.is_empty() {
return parse::Error::new(Span::call_site(), "This attribute accepts no arguments")
.to_compile_error()
.into();
.to_compile_error();
}

let root = match proc_macro_crate::crate_name("esp-hal") {
Expand All @@ -37,5 +31,68 @@ pub fn main(args: TokenStream, input: TokenStream) -> TokenStream {
#[#root::__macro_implementation::__entry]
#f
)
.into()
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_basic() {
let result = main(
quote::quote! {}.into(),
syn::parse2(quote::quote! {
fn main() {}
})
.unwrap(),
);

assert_eq!(
result.to_string(),
quote::quote! {
#[doc = "The main entry point of the firmware, generated by the `#[main]` macro."]
#[esp_hal::__macro_implementation::__entry]
fn main () { }
}
.to_string()
);
}

#[test]
fn test_try_async() {
let result = main(
quote::quote! {}.into(),
syn::parse2(quote::quote! {
async fn main() {}
})
.unwrap(),
);

assert_eq!(
result.to_string(),
quote::quote! {
::core::compile_error!{ "If you want to use `async` please use `esp-rtos`'s `#[esp_rtos::main]` macro instead." }
}
.to_string()
);
}

#[test]
fn test_try_non_emptyargs() {
let result = main(
quote::quote! {non_empty}.into(),
syn::parse2(quote::quote! {
fn main() {}
})
.unwrap(),
);

assert_eq!(
result.to_string(),
quote::quote! {
::core::compile_error!{ "This attribute accepts no arguments" }
}
.to_string()
);
}
}
Loading
Loading