Skip to content

Commit

Permalink
feat(neon-macros): Add Channel context for async functions and `thi…
Browse files Browse the repository at this point in the history
…s` support

Additionally, improves error messaging when incorrect types are provided. UI tests, via trybuild, are added to prevent regressions on errors.
  • Loading branch information
kjvalencik committed Oct 1, 2024
1 parent 3367d20 commit abf2084
Show file tree
Hide file tree
Showing 27 changed files with 944 additions and 81 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
target
wip
**/*~
**/node_modules
**/.DS_Store
Expand Down
114 changes: 114 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 14 additions & 13 deletions crates/neon-macros/src/export/function/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub(crate) struct Meta {
pub(super) name: Option<syn::LitStr>,
pub(super) json: bool,
pub(super) context: bool,
pub(super) this: bool,
}

#[derive(Default)]
Expand All @@ -28,33 +29,29 @@ impl Meta {
Ok(())
}

fn force_context(&mut self, meta: syn::meta::ParseNestedMeta) -> syn::Result<()> {
match self.kind {
Kind::Normal | Kind::AsyncFn => {}
Kind::Async => return Err(meta.error(super::ASYNC_CX_ERROR)),
Kind::Task => return Err(meta.error(super::TASK_CX_ERROR)),
}

fn force_context(&mut self, _meta: syn::meta::ParseNestedMeta) -> syn::Result<()> {
self.context = true;

Ok(())
}

fn force_this(&mut self, _meta: syn::meta::ParseNestedMeta) -> syn::Result<()> {
self.this = true;

Ok(())
}

fn make_async(&mut self, meta: syn::meta::ParseNestedMeta) -> syn::Result<()> {
if matches!(self.kind, Kind::AsyncFn) {
return Err(meta.error(super::ASYNC_FN_ERROR));
return Err(meta.error("`async` attribute should not be used with an `async fn`"));
}

self.kind = Kind::Async;

Ok(())
}

fn make_task(&mut self, meta: syn::meta::ParseNestedMeta) -> syn::Result<()> {
if self.context {
return Err(meta.error(super::TASK_CX_ERROR));
}

fn make_task(&mut self, _meta: syn::meta::ParseNestedMeta) -> syn::Result<()> {
self.kind = Kind::Task;

Ok(())
Expand Down Expand Up @@ -93,6 +90,10 @@ impl syn::parse::Parser for Parser {
return attr.force_context(meta);
}

if meta.path.is_ident("this") {
return attr.force_this(meta);
}

if meta.path.is_ident("async") {
return attr.make_async(meta);
}
Expand Down
Loading

0 comments on commit abf2084

Please sign in to comment.