-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1025 from neon-bindings/kv/export
feat(neon-macros): Export Macro
- Loading branch information
Showing
25 changed files
with
1,606 additions
and
209 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#[derive(Default)] | ||
pub(crate) struct Meta { | ||
pub(super) kind: Kind, | ||
pub(super) name: Option<syn::LitStr>, | ||
pub(super) json: bool, | ||
pub(super) context: bool, | ||
pub(super) result: bool, | ||
} | ||
|
||
#[derive(Default)] | ||
pub(super) enum Kind { | ||
#[default] | ||
Normal, | ||
Task, | ||
} | ||
|
||
impl Meta { | ||
fn set_name(&mut self, meta: syn::meta::ParseNestedMeta) -> syn::Result<()> { | ||
self.name = Some(meta.value()?.parse::<syn::LitStr>()?); | ||
|
||
Ok(()) | ||
} | ||
|
||
fn force_json(&mut self, _meta: syn::meta::ParseNestedMeta) -> syn::Result<()> { | ||
self.json = true; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn force_context(&mut self, meta: syn::meta::ParseNestedMeta) -> syn::Result<()> { | ||
match self.kind { | ||
Kind::Normal => {} | ||
Kind::Task => return Err(meta.error(super::TASK_CX_ERROR)), | ||
} | ||
|
||
self.context = true; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn force_result(&mut self, _meta: syn::meta::ParseNestedMeta) -> syn::Result<()> { | ||
self.result = true; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn make_task(&mut self, meta: syn::meta::ParseNestedMeta) -> syn::Result<()> { | ||
if self.context { | ||
return Err(meta.error(super::TASK_CX_ERROR)); | ||
} | ||
|
||
self.kind = Kind::Task; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
pub(crate) struct Parser; | ||
|
||
impl syn::parse::Parser for Parser { | ||
type Output = Meta; | ||
|
||
fn parse2(self, tokens: proc_macro2::TokenStream) -> syn::Result<Self::Output> { | ||
let mut attr = Meta::default(); | ||
let parser = syn::meta::parser(|meta| { | ||
if meta.path.is_ident("name") { | ||
return attr.set_name(meta); | ||
} | ||
|
||
if meta.path.is_ident("json") { | ||
return attr.force_json(meta); | ||
} | ||
|
||
if meta.path.is_ident("context") { | ||
return attr.force_context(meta); | ||
} | ||
|
||
if meta.path.is_ident("result") { | ||
return attr.force_result(meta); | ||
} | ||
|
||
if meta.path.is_ident("task") { | ||
return attr.make_task(meta); | ||
} | ||
|
||
Err(meta.error("unsupported property")) | ||
}); | ||
|
||
parser.parse2(tokens)?; | ||
|
||
Ok(attr) | ||
} | ||
} |
Oops, something went wrong.