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"))]
pub use cortex::*;

View file

@ -4,35 +4,50 @@
)]
//deny_warnings_placeholder_for_ci
use proc_macro::TokenStream;
use std::{env, fs, path::Path};
macro_rules! with_backend {
(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;
mod check;
mod codegen;
mod syntax;
with_backend! { mod: [analyze, check, codegen, syntax] }
with_backend! { use std::{fs, env, path::Path}; }
with_backend! { use proc_macro::TokenStream; }
// Used for mocking the API in testing
#[doc(hidden)]
#[proc_macro_attribute]
pub fn mock_app(args: TokenStream, input: TokenStream) -> TokenStream {
with_backend! {
// Used for mocking the API in testing
#[doc(hidden)]
#[proc_macro_attribute]
pub fn mock_app(args: TokenStream, input: TokenStream) -> TokenStream {
if let Err(e) = syntax::parse(args, input) {
e.to_compile_error().into()
} else {
"fn main() {}".parse().unwrap()
}
}
}
/// Attribute used to declare a RTIC application
///
/// For user documentation see the [RTIC book](https://rtic.rs)
///
/// # Panics
///
/// Should never panic, cargo feeds a path which is later converted to a string
#[proc_macro_attribute]
pub fn app(args: TokenStream, input: TokenStream) -> TokenStream {
let (app, analysis) = match syntax::parse(args, input) {
with_backend! {
/// Attribute used to declare a RTIC application
///
/// For user documentation see the [RTIC book](https://rtic.rs)
///
/// # Panics
///
/// Should never panic, cargo feeds a path which is later converted to a string
#[proc_macro_attribute]
pub fn app(_args: TokenStream, _input: TokenStream) -> TokenStream {
let (app, analysis) = match syntax::parse(_args, _input) {
Err(e) => return e.to_compile_error().into(),
Ok(x) => x,
};
@ -87,4 +102,12 @@ pub fn app(args: TokenStream, input: TokenStream) -> TokenStream {
}
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.");