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,35 +4,50 @@
)] )]
//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;
// Used for mocking the API in testing with_backend! {
#[doc(hidden)] // Used for mocking the API in testing
#[proc_macro_attribute] #[doc(hidden)]
pub fn mock_app(args: TokenStream, input: TokenStream) -> TokenStream { #[proc_macro_attribute]
pub fn mock_app(args: TokenStream, input: TokenStream) -> TokenStream {
if let Err(e) = syntax::parse(args, input) { if let Err(e) = syntax::parse(args, input) {
e.to_compile_error().into() e.to_compile_error().into()
} else { } else {
"fn main() {}".parse().unwrap() "fn main() {}".parse().unwrap()
} }
}
} }
/// Attribute used to declare a RTIC application with_backend! {
/// /// 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)
/// # Panics ///
/// /// # Panics
/// Should never panic, cargo feeds a path which is later converted to a string ///
#[proc_macro_attribute] /// Should never panic, cargo feeds a path which is later converted to a string
pub fn app(args: TokenStream, input: TokenStream) -> TokenStream { #[proc_macro_attribute]
let (app, analysis) = match syntax::parse(args, input) { pub fn app(_args: TokenStream, _input: TokenStream) -> TokenStream {
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,
}; };
@ -87,4 +102,12 @@ 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.");