-
Notifications
You must be signed in to change notification settings - Fork 28
Handle more SCALE attributes: skip, index #44
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
Merged
Merged
Changes from all commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
37c0a57
Parameterize CompactForm String for optional SCALE impl
ascjones 643f09d
Merge remote-tracking branch 'origin/master' into aj-compact-string
dvdplm 9a7ccbf
Fix no-std compilation
dvdplm 254fee1
Obey the fmt
dvdplm e74e4f9
Introduce String trait for Form
ascjones 7860c79
Rename "Compact" to "Frozen" (and associated fallout)
dvdplm 579f958
Docs cleanup and more renames
dvdplm 8333e5a
Cleanup
dvdplm 2818f7b
More cleanup
dvdplm 7706a38
Merge branch 'aj-compact-string' into dp-rename-compact-to-frozen
dvdplm e03a2cd
obey the fmt
dvdplm 3a95663
Add a `compact` flag to `Field` to indicate that this type is to be e…
dvdplm 004e107
Clippy warnings
dvdplm 93a9aeb
Acommodate older clippy
dvdplm 6569e50
Derive (scale) compact fields
dvdplm f098101
Merge branch 'master' into dp-flag-types-as-compact
dvdplm b43cdfc
Use utils from parity-scale-codec-derive
dvdplm eda2769
Merge remote-tracking branch 'origin/master' into dp-flag-types-as-co…
dvdplm 6321da9
Merge branch 'dp-flag-types-as-compact' into dp-handle-scale-skip
dvdplm 1d32a38
Merge remote-tracking branch 'origin/master' into dp-handle-scale-skip
dvdplm d1700b2
fmt
dvdplm 3387355
Merge branch 'master' into dp-handle-scale-skip
dvdplm d29c9be
Attempt to fix CI
dvdplm 0426479
FIx CI take 2
dvdplm 0a97eb1
Merge branch 'master' into dp-handle-scale-skip
dvdplm 81e3519
Merge branch 'master' into dp-handle-scale-skip
dvdplm e8d2a74
Use is_compact from utils
dvdplm d9239c5
Fn is enough
dvdplm 98292c5
Doc tweaks
dvdplm 71d7e11
Add tests for enums
dvdplm 00aee2f
Add test for indexed enum
dvdplm a31acc5
Oops
dvdplm e70425f
Update derive/src/utils.rs
dvdplm abdb081
Review feedback
dvdplm c53eb38
fmt
dvdplm 1c1802e
Better error message, clearer bad syntax trubuild-test
dvdplm 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| // Copyright 2019-2021 Parity Technologies (UK) Ltd. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| //! Utility methods to work with `SCALE` attributes relevant for the `TypeInfo` derive.. | ||
| //! | ||
| //! NOTE: The code here is copied verbatim from `parity-scale-codec-derive`. | ||
|
|
||
| use proc_macro2::TokenStream; | ||
| use quote::quote; | ||
| use syn::{ | ||
| spanned::Spanned, | ||
| AttrStyle, | ||
| Attribute, | ||
| Lit, | ||
| Meta, | ||
| NestedMeta, | ||
| Variant, | ||
| }; | ||
|
|
||
| /// Look for a `#[codec(index = $int)]` attribute on a variant. If no attribute | ||
| /// is found, fall back to the discriminant or just the variant index. | ||
| pub fn variant_index(v: &Variant, i: usize) -> TokenStream { | ||
| // first look for an attribute | ||
| let index = find_meta_item(v.attrs.iter(), |meta| { | ||
| if let NestedMeta::Meta(Meta::NameValue(ref nv)) = meta { | ||
| if nv.path.is_ident("index") { | ||
| if let Lit::Int(ref v) = nv.lit { | ||
| let byte = v | ||
| .base10_parse::<u8>() | ||
| .expect("Internal error. `#[codec(index = …)]` attribute syntax must be checked in `parity-scale-codec`. This is a bug."); | ||
| return Some(byte) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| None | ||
| }); | ||
|
|
||
| // then fallback to discriminant or just index | ||
| index.map(|i| quote! { #i }).unwrap_or_else(|| { | ||
| v.discriminant | ||
| .as_ref() | ||
| .map(|&(_, ref expr)| quote! { #expr }) | ||
| .unwrap_or_else(|| quote! { #i }) | ||
| }) | ||
| } | ||
|
|
||
| /// Look for a `#[codec(compact)]` outer attribute on the given `Field`. | ||
| pub fn is_compact(field: &syn::Field) -> bool { | ||
| let outer_attrs = field | ||
| .attrs | ||
| .iter() | ||
| .filter(|attr| attr.style == AttrStyle::Outer); | ||
| find_meta_item(outer_attrs, |meta| { | ||
| if let NestedMeta::Meta(Meta::Path(ref path)) = meta { | ||
| if path.is_ident("compact") { | ||
| return Some(()) | ||
| } | ||
| } | ||
|
|
||
| None | ||
| }) | ||
| .is_some() | ||
| } | ||
|
|
||
| /// Look for a `#[codec(skip)]` in the given attributes. | ||
| pub fn should_skip(attrs: &[Attribute]) -> bool { | ||
| find_meta_item(attrs.iter(), |meta| { | ||
| if let NestedMeta::Meta(Meta::Path(ref path)) = meta { | ||
| if path.is_ident("skip") { | ||
| return Some(path.span()) | ||
| } | ||
| } | ||
|
|
||
| None | ||
| }) | ||
| .is_some() | ||
| } | ||
|
|
||
| fn find_meta_item<'a, F, R, I>(itr: I, pred: F) -> Option<R> | ||
| where | ||
| F: Fn(&NestedMeta) -> Option<R> + Clone, | ||
| I: Iterator<Item = &'a Attribute>, | ||
| { | ||
| itr.filter_map(|attr| { | ||
ascjones marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if attr.path.is_ident("codec") { | ||
| if let Meta::List(ref meta_list) = attr | ||
| .parse_meta() | ||
| .expect("scale-info: Bad index in `#[codec(index = …)]`, see `parity-scale-codec` error") | ||
| { | ||
| return meta_list.nested.iter().filter_map(pred.clone()).next() | ||
| } | ||
| } | ||
|
|
||
| None | ||
| }) | ||
| .next() | ||
| } | ||
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.