From a7653cc05092aa5b009172c531e4f729c678a858 Mon Sep 17 00:00:00 2001 From: Vixu Date: Thu, 8 Jun 2023 13:44:49 +0200 Subject: allow init and idle to be externed --- rtic-macros/src/syntax/parse/app.rs | 39 ++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) (limited to 'rtic-macros/src/syntax/parse/app.rs') diff --git a/rtic-macros/src/syntax/parse/app.rs b/rtic-macros/src/syntax/parse/app.rs index 2f2d816..d75c8c6 100644 --- a/rtic-macros/src/syntax/parse/app.rs +++ b/rtic-macros/src/syntax/parse/app.rs @@ -365,6 +365,42 @@ impl App { if let ForeignItem::Fn(mut item) = item { let span = item.sig.ident.span(); if let Some(pos) = item + .attrs + .iter() + .position(|attr| util::attr_eq(attr, "init")) + { + let args = InitArgs::parse(item.attrs.remove(pos).tokens)?; + + // If an init function already exists, error + if init.is_some() { + return Err(parse::Error::new( + span, + "`#[init]` function must appear at most once", + )); + } + + check_ident(&item.sig.ident)?; + + init = Some(Init::parse_foreign(args, item)?); + } else if let Some(pos) = item + .attrs + .iter() + .position(|attr| util::attr_eq(attr, "idle")) + { + let args = IdleArgs::parse(item.attrs.remove(pos).tokens)?; + + // If an idle function already exists, error + if idle.is_some() { + return Err(parse::Error::new( + span, + "`#[idle]` function must appear at most once", + )); + } + + check_ident(&item.sig.ident)?; + + idle = Some(Idle::parse_foreign(args, item)?); + } else if let Some(pos) = item .attrs .iter() .position(|attr| util::attr_eq(attr, "task")) @@ -408,7 +444,8 @@ impl App { } else { return Err(parse::Error::new( span, - "`extern` task required `#[task(..)]` attribute", + "`extern` task, init or idle must have either `#[task(..)]`, + `#[init(..)]` or `#[idle(..)]` attribute", )); } } else { -- cgit v1.2.3