diff options
author | 2023-02-04 16:47:17 +0100 | |
---|---|---|
committer | 2023-03-01 00:35:13 +0100 | |
commit | 9e445b3583c15c7701f3167eaa8dfe4afd541691 (patch) | |
tree | 167565d51598f42c0454d60b34e1170589ae1056 /rtic-macros/src/syntax/parse/hardware_task.rs | |
parent | 4124fbdd61ff823c6217a2a16ebb4d813146116c (diff) | |
download | rtic-9e445b3583c15c7701f3167eaa8dfe4afd541691.tar.gz rtic-9e445b3583c15c7701f3167eaa8dfe4afd541691.tar.zst rtic-9e445b3583c15c7701f3167eaa8dfe4afd541691.zip |
Move rtic macros to repo root, tune xtask
Diffstat (limited to 'rtic-macros/src/syntax/parse/hardware_task.rs')
-rw-r--r-- | rtic-macros/src/syntax/parse/hardware_task.rs | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/rtic-macros/src/syntax/parse/hardware_task.rs b/rtic-macros/src/syntax/parse/hardware_task.rs new file mode 100644 index 00000000..7f6dfbe4 --- /dev/null +++ b/rtic-macros/src/syntax/parse/hardware_task.rs @@ -0,0 +1,76 @@ +use syn::{parse, ForeignItemFn, ItemFn, Stmt}; + +use crate::syntax::parse::util::FilterAttrs; +use crate::syntax::{ + ast::{HardwareTask, HardwareTaskArgs}, + parse::util, +}; + +impl HardwareTask { + pub(crate) fn parse(args: HardwareTaskArgs, item: ItemFn) -> parse::Result<Self> { + let span = item.sig.ident.span(); + let valid_signature = util::check_fn_signature(&item, false) + && item.sig.inputs.len() == 1 + && util::type_is_unit(&item.sig.output); + + let name = item.sig.ident.to_string(); + + if valid_signature { + if let Some((context, Ok(rest))) = util::parse_inputs(item.sig.inputs, &name) { + if rest.is_empty() { + let FilterAttrs { cfgs, attrs, .. } = util::filter_attributes(item.attrs); + + return Ok(HardwareTask { + args, + cfgs, + attrs, + context, + stmts: item.block.stmts, + is_extern: false, + }); + } + } + } + + Err(parse::Error::new( + span, + format!("this task handler must have type signature `fn({name}::Context)`"), + )) + } +} + +impl HardwareTask { + pub(crate) fn parse_foreign( + args: HardwareTaskArgs, + item: ForeignItemFn, + ) -> parse::Result<Self> { + let span = item.sig.ident.span(); + let valid_signature = util::check_foreign_fn_signature(&item, false) + && item.sig.inputs.len() == 1 + && util::type_is_unit(&item.sig.output); + + let name = item.sig.ident.to_string(); + + if valid_signature { + if let Some((context, Ok(rest))) = util::parse_inputs(item.sig.inputs, &name) { + if rest.is_empty() { + let FilterAttrs { cfgs, attrs, .. } = util::filter_attributes(item.attrs); + + return Ok(HardwareTask { + args, + cfgs, + attrs, + context, + stmts: Vec::<Stmt>::new(), + is_extern: true, + }); + } + } + } + + Err(parse::Error::new( + span, + format!("this task handler must have type signature `fn({name}::Context)`"), + )) + } +} |