aboutsummaryrefslogtreecommitdiff
path: root/macros/src/syntax/parse/init.rs
diff options
context:
space:
mode:
authorGravatar Emil Fresk <emil.fresk@gmail.com> 2022-12-31 14:45:13 +0100
committerGravatar Henrik Tjäder <henrik@tjaders.com> 2023-03-01 00:29:10 +0100
commit7614b96fe45240dafe91ae549e712b560e2d4c10 (patch)
tree30e1666c0aac09480e1a87e5fe7965487f4a99e6 /macros/src/syntax/parse/init.rs
parent1c5db277e4161470136dbd2a11e914ff1d383581 (diff)
downloadrtic-7614b96fe45240dafe91ae549e712b560e2d4c10.tar.gz
rtic-7614b96fe45240dafe91ae549e712b560e2d4c10.tar.zst
rtic-7614b96fe45240dafe91ae549e712b560e2d4c10.zip
RTIC v2: Initial commit
rtic-syntax is now part of RTIC repository
Diffstat (limited to 'macros/src/syntax/parse/init.rs')
-rw-r--r--macros/src/syntax/parse/init.rs52
1 files changed, 52 insertions, 0 deletions
diff --git a/macros/src/syntax/parse/init.rs b/macros/src/syntax/parse/init.rs
new file mode 100644
index 00000000..727ee205
--- /dev/null
+++ b/macros/src/syntax/parse/init.rs
@@ -0,0 +1,52 @@
+use proc_macro2::TokenStream as TokenStream2;
+
+use syn::{parse, ItemFn};
+
+use crate::syntax::{
+ ast::{Init, InitArgs},
+ parse::{self as syntax_parse, util},
+};
+
+impl InitArgs {
+ pub(crate) fn parse(tokens: TokenStream2) -> parse::Result<Self> {
+ syntax_parse::init_args(tokens)
+ }
+}
+
+impl Init {
+ pub(crate) fn parse(args: InitArgs, item: ItemFn) -> parse::Result<Self> {
+ let valid_signature = util::check_fn_signature(&item, false) && item.sig.inputs.len() == 1;
+
+ let span = item.sig.ident.span();
+
+ let name = item.sig.ident.to_string();
+
+ if valid_signature {
+ if let Ok((user_shared_struct, user_local_struct)) =
+ util::type_is_init_return(&item.sig.output, &name)
+ {
+ if let Some((context, Ok(rest))) = util::parse_inputs(item.sig.inputs, &name) {
+ if rest.is_empty() {
+ return Ok(Init {
+ args,
+ attrs: item.attrs,
+ context,
+ name: item.sig.ident,
+ stmts: item.block.stmts,
+ user_shared_struct,
+ user_local_struct,
+ });
+ }
+ }
+ }
+ }
+
+ Err(parse::Error::new(
+ span,
+ &format!(
+ "the `#[init]` function must have signature `fn({}::Context) -> (Shared resources struct, Local resources struct, {0}::Monotonics)`",
+ name
+ ),
+ ))
+ }
+}