Skip to content

Commit

Permalink
riot-rs-macros: make thread support both riot-rs and riot-rs-deps
Browse files Browse the repository at this point in the history
  • Loading branch information
kaspar030 committed May 24, 2024
1 parent b2bd763 commit 413c971
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
12 changes: 10 additions & 2 deletions src/riot-rs-macros/src/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ pub fn thread(args: TokenStream, item: TokenStream) -> TokenStream {

use quote::quote;

use crate::utils::find_crate;

let mut attrs = Attributes::default();
let thread_parser = syn::meta::parser(|meta| attrs.parse(&meta));
syn::parse_macro_input!(args with thread_parser);
Expand All @@ -62,13 +64,19 @@ pub fn thread(args: TokenStream, item: TokenStream) -> TokenStream {
priority,
} = Parameters::from(attrs);

let riot_rs_crate = utils::riot_rs_crate();
let thread_crate = {
match (find_crate("riot-rs"), find_crate("riot-rs-threads")) {
(Some(riot_rs), _) => quote! { #riot_rs::thread },
(None, Some(riot_rs_threads)) => quote! { #riot_rs_threads },
_ => panic!(r#"neither "riot-rs" nor "riot-rs-threads" found in dependencies!"#),
}
};

let expanded = quote! {
#no_mangle_attr
#thread_function

#riot_rs_crate::thread::autostart_thread!(#fn_name, stacksize = #stack_size, priority = #priority);
#thread_crate::autostart_thread!(#fn_name, stacksize = #stack_size, priority = #priority);
};

TokenStream::from(expanded)
Expand Down
7 changes: 2 additions & 5 deletions src/riot-rs-macros/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn riot_rs_crate() -> syn::Ident {
.unwrap_or_else(|| panic!("{RIOT_RS_CRATE_NAME} should be present in `Cargo.toml`"))
}

/// Returns a [`struct@syn::Ident`] identifying the `name` dependency (or None).
/// Returns a [`struct@syn::Ident`] identifying the `name` dependency (or `None`).
///
/// # Panics
///
Expand All @@ -23,10 +23,7 @@ pub fn find_crate(name: &str) -> Option<syn::Ident> {
if let Ok(crate_) = proc_macro_crate::crate_name(name) {
match crate_ {
proc_macro_crate::FoundCrate::Itself => {
panic!(
"{} cannot be used as a dependency of itself",
env!("CARGO_CRATE_NAME"),
);
panic!("{name} cannot be used as a dependency of itself");
}
proc_macro_crate::FoundCrate::Name(crate_) => Some(format_ident!("{crate_}")),
}
Expand Down

0 comments on commit 413c971

Please sign in to comment.