rtic-macros: wrap everything in macro_rules! to improve the error experience and

royally **** up the DX experience (for lib.rs in rtic-macros, at least :P)
This commit is contained in:
datdenkikniet 2023-04-13 22:13:40 +02:00
parent 68ccf0423d
commit 2ba09dd68d
2 changed files with 86 additions and 70 deletions

View file

@ -1,10 +1,3 @@
#[cfg(not(any(
feature = "cortex-m-source-masking",
feature = "cortex-m-basepri",
feature = "test-template"
)))]
compile_error!("No backend selected");
#[cfg(any(feature = "cortex-m-source-masking", feature = "cortex-m-basepri"))] #[cfg(any(feature = "cortex-m-source-masking", feature = "cortex-m-basepri"))]
pub use cortex::*; pub use cortex::*;

View file

@ -4,14 +4,27 @@
)] )]
//deny_warnings_placeholder_for_ci //deny_warnings_placeholder_for_ci
use proc_macro::TokenStream; macro_rules! with_backend {
use std::{env, fs, path::Path}; (mod: [$($mod:tt),*]) => {
$(
with_backend!{ mod $mod; }
)*
};
($($tokens:tt)*) => {
#[cfg(any(
feature = "cortex-m-source-masking",
feature = "cortex-m-basepri",
feature = "test-template"
))]
$($tokens)*
};
}
mod analyze; with_backend! { mod: [analyze, check, codegen, syntax] }
mod check; with_backend! { use std::{fs, env, path::Path}; }
mod codegen; with_backend! { use proc_macro::TokenStream; }
mod syntax;
with_backend! {
// Used for mocking the API in testing // Used for mocking the API in testing
#[doc(hidden)] #[doc(hidden)]
#[proc_macro_attribute] #[proc_macro_attribute]
@ -22,7 +35,9 @@ pub fn mock_app(args: TokenStream, input: TokenStream) -> TokenStream {
"fn main() {}".parse().unwrap() "fn main() {}".parse().unwrap()
} }
} }
}
with_backend! {
/// Attribute used to declare a RTIC application /// Attribute used to declare a RTIC application
/// ///
/// For user documentation see the [RTIC book](https://rtic.rs) /// For user documentation see the [RTIC book](https://rtic.rs)
@ -31,8 +46,8 @@ pub fn mock_app(args: TokenStream, input: TokenStream) -> TokenStream {
/// ///
/// Should never panic, cargo feeds a path which is later converted to a string /// Should never panic, cargo feeds a path which is later converted to a string
#[proc_macro_attribute] #[proc_macro_attribute]
pub fn app(args: TokenStream, input: TokenStream) -> TokenStream { pub fn app(_args: TokenStream, _input: TokenStream) -> TokenStream {
let (app, analysis) = match syntax::parse(args, input) { let (app, analysis) = match syntax::parse(_args, _input) {
Err(e) => return e.to_compile_error().into(), Err(e) => return e.to_compile_error().into(),
Ok(x) => x, Ok(x) => x,
}; };
@ -88,3 +103,11 @@ pub fn app(args: TokenStream, input: TokenStream) -> TokenStream {
ts.into() ts.into()
} }
}
#[cfg(not(any(
feature = "cortex-m-source-masking",
feature = "cortex-m-basepri",
feature = "test-template"
)))]
compile_error!("Cannot compile. No backend feature selected.");