-
Notifications
You must be signed in to change notification settings - Fork 243
Support NifMap decoding when elixir map has missing fields #746
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DanielSidhion
wants to merge
3
commits into
rusterlium:master
Choose a base branch
from
DanielSidhion:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,120 @@ | ||
| use std::fmt::Display; | ||
|
|
||
| use syn::{Ident, Lit, Meta}; | ||
|
|
||
| pub(crate) trait TryFromRustlerNestedAttr: Sized { | ||
| fn collect_attrs_for_ident(ident: &Ident, meta: &Meta) -> Option<Vec<Self>>; | ||
| fn try_from_rustler_nested_attr(ident: &Ident) -> Option<Self>; | ||
| fn parse_failure_message() -> impl Display; | ||
|
|
||
| fn parse_rustler<T: TryFromRustlerNestedAttr>(meta: &Meta) -> Vec<T> { | ||
| if let Meta::List(ref list) = meta { | ||
| let mut attrs: Vec<T> = vec![]; | ||
| let _ = list.parse_nested_meta(|nested_meta| { | ||
| let parsed_attr = nested_meta | ||
| .path | ||
| .get_ident() | ||
| .and_then(T::try_from_rustler_nested_attr); | ||
|
|
||
| match parsed_attr { | ||
| None => Err(nested_meta.error(T::parse_failure_message())), | ||
| Some(attr) => { | ||
| attrs.push(attr); | ||
| Ok(()) | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| return attrs; | ||
| } | ||
|
|
||
| panic!("Expected nested attributes inside the rustler attribute"); | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| pub(crate) enum RustlerAttr { | ||
| Encode, | ||
| Decode, | ||
| OptionalDecode, | ||
| Module(String), | ||
| Tag(String), | ||
| } | ||
|
|
||
| impl RustlerAttr { | ||
| fn try_parse_tag(meta: &Meta) -> Option<Vec<RustlerAttr>> { | ||
| if let Meta::NameValue(ref name_value) = meta { | ||
| let expr = &name_value.value; | ||
|
|
||
| if let syn::Expr::Lit(lit_expr) = expr { | ||
| if let Lit::Str(ref tag) = lit_expr.lit { | ||
| return Some(vec![RustlerAttr::Tag(tag.value())]); | ||
| } | ||
| } | ||
| } | ||
| panic!("Cannot parse tag") | ||
| } | ||
|
|
||
| fn try_parse_module(meta: &Meta) -> Option<Vec<RustlerAttr>> { | ||
| if let Meta::NameValue(name_value) = meta { | ||
| let expr = &name_value.value; | ||
|
|
||
| if let syn::Expr::Lit(lit_expr) = expr { | ||
| if let Lit::Str(ref module) = lit_expr.lit { | ||
| let ident = format!("Elixir.{}", module.value()); | ||
| return Some(vec![RustlerAttr::Module(ident)]); | ||
| } | ||
| } | ||
| } | ||
| panic!("Cannot parse module") | ||
| } | ||
| } | ||
|
|
||
| impl TryFromRustlerNestedAttr for RustlerAttr { | ||
| fn parse_failure_message() -> impl Display { | ||
| "Expected encode, decode and/or optional_decode in rustler attribute" | ||
| } | ||
|
|
||
| fn collect_attrs_for_ident(ident: &Ident, meta: &Meta) -> Option<Vec<Self>> { | ||
| match ident.to_string().as_ref() { | ||
| "rustler" => Some(Self::parse_rustler(meta)), | ||
| "tag" => Self::try_parse_tag(meta), | ||
| "module" => Self::try_parse_module(meta), | ||
| _ => None, | ||
| } | ||
| } | ||
|
|
||
| fn try_from_rustler_nested_attr(ident: &Ident) -> Option<Self> { | ||
| match ident.to_string().as_ref() { | ||
| "encode" => Some(Self::Encode), | ||
| "decode" => Some(Self::Decode), | ||
| "optional_decode" => Some(Self::OptionalDecode), | ||
| _ => None, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| pub(crate) enum RustlerFieldAttr { | ||
| OptionalDecode, | ||
| } | ||
|
|
||
| impl TryFromRustlerNestedAttr for RustlerFieldAttr { | ||
| fn parse_failure_message() -> impl Display { | ||
| "Expected optional_decode in rustler field attribute" | ||
| } | ||
|
|
||
| fn collect_attrs_for_ident(ident: &Ident, meta: &Meta) -> Option<Vec<Self>> { | ||
| match ident.to_string().as_ref() { | ||
| "rustler" => Some(Self::parse_rustler(meta)), | ||
| _ => None, | ||
| } | ||
| } | ||
|
|
||
| fn try_from_rustler_nested_attr(ident: &Ident) -> Option<Self> { | ||
| match ident.to_string().as_ref() { | ||
| "optional_decode" => Some(Self::OptionalDecode), | ||
| _ => None, | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The name for this trait should change to be a bit more global than just "rustler nested attributes", but I couldn't think of anything interesting. Appreciate any suggestions.