-
Notifications
You must be signed in to change notification settings - Fork 172
Derived Version Structs #410
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
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 |
|---|---|---|
|
|
@@ -6,7 +6,7 @@ use crate::ATTR_NAME; | |
| use proc_macro2::TokenStream; | ||
| use proc_macro_error::abort; | ||
| use quote::quote; | ||
| use syn::{DeriveInput, Expr, ExprLit, Ident, Lit, LitInt, Variant}; | ||
| use syn::{DeriveInput, Expr, ExprLit, Ident, Lit, LitInt, Meta, MetaList, NestedMeta, Variant}; | ||
|
|
||
| /// Valid options for the `#[repr]` attribute on `Enumerated` types. | ||
| const REPR_TYPES: &[&str] = &["u8", "u16", "u32"]; | ||
|
|
@@ -19,6 +19,9 @@ pub(crate) struct DeriveEnumerated { | |
| /// Value of the `repr` attribute. | ||
| repr: Ident, | ||
|
|
||
| /// Whether or not to tag the enum as an integer | ||
| integer: bool, | ||
|
|
||
| /// Variants of this enum. | ||
| variants: Vec<EnumeratedVariant>, | ||
| } | ||
|
|
@@ -36,13 +39,25 @@ impl DeriveEnumerated { | |
|
|
||
| // Reject `asn1` attributes, parse the `repr` attribute | ||
| let mut repr: Option<Ident> = None; | ||
| let mut integer = false; | ||
|
|
||
| for attr in &input.attrs { | ||
| if attr.path.is_ident(ATTR_NAME) { | ||
| abort!( | ||
| attr.path, | ||
| "`asn1` attribute is not allowed on `Enumerated` types" | ||
| ); | ||
| if let Ok(Meta::List(MetaList { nested, .. })) = attr.parse_meta() { | ||
| for meta in nested { | ||
| if let NestedMeta::Meta(Meta::NameValue(nv)) = meta { | ||
| if nv.path.is_ident("type") { | ||
| if let Lit::Str(lit) = nv.lit { | ||
| match lit.value().as_str() { | ||
| "ENUMERATED" => integer = false, | ||
| "INTEGER" => integer = true, | ||
| s => abort!(lit, "`type = \"{}\"` is unsupported", s), | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
Comment on lines
+46
to
+60
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is... less than ideal, but I have some ideas about how to refactor this along with attribute parsing in general which I can do in a followup PR. |
||
| } else if attr.path.is_ident("repr") { | ||
| if repr.is_some() { | ||
| abort!( | ||
|
|
@@ -81,13 +96,18 @@ impl DeriveEnumerated { | |
| ) | ||
| }), | ||
| variants, | ||
| integer, | ||
| } | ||
| } | ||
|
|
||
| /// Lower the derived output into a [`TokenStream`]. | ||
| pub fn to_tokens(&self) -> TokenStream { | ||
| let ident = &self.ident; | ||
| let repr = &self.repr; | ||
| let tag = match self.integer { | ||
| false => quote! { ::der::Tag::Enumerated }, | ||
| true => quote! { ::der::Tag::Integer }, | ||
| }; | ||
|
|
||
| let mut try_from_body = Vec::new(); | ||
| for variant in &self.variants { | ||
|
|
@@ -115,7 +135,7 @@ impl DeriveEnumerated { | |
| } | ||
|
|
||
| impl ::der::FixedTag for #ident { | ||
| const TAG: ::der::Tag = ::der::Tag::Enumerated; | ||
| const TAG: ::der::Tag = #tag; | ||
| } | ||
|
|
||
| impl TryFrom<#repr> for #ident { | ||
|
|
@@ -124,7 +144,7 @@ impl DeriveEnumerated { | |
| fn try_from(n: #repr) -> ::der::Result<Self> { | ||
| match n { | ||
| #(#try_from_body)* | ||
| _ => Err(der::Tag::Enumerated.value_error()) | ||
| _ => Err(#tag.value_error()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
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 |
|---|---|---|
| @@ -1,49 +1,14 @@ | ||
| //! Certification request information version identifier. | ||
|
|
||
| use der::{Decodable, Decoder, Encodable, Encoder, FixedTag, Tag}; | ||
| use der::Enumerated; | ||
|
|
||
| /// Version identifier for certification request information. | ||
| /// | ||
| /// (RFC 2986 designates `0` as the only valid version) | ||
| #[derive(Clone, Debug, Copy, PartialEq, Eq)] | ||
| #[derive(Clone, Debug, Copy, PartialEq, Eq, Enumerated)] | ||
| #[asn1(type = "INTEGER")] | ||
| #[repr(u8)] | ||
| pub enum Version { | ||
| /// Denotes PKCS#8 v1 | ||
| V1 = 0, | ||
| } | ||
|
|
||
| impl Decodable<'_> for Version { | ||
| fn decode(decoder: &mut Decoder<'_>) -> der::Result<Self> { | ||
| Version::try_from(u8::decode(decoder)?).map_err(|_| Self::TAG.value_error()) | ||
| } | ||
| } | ||
|
|
||
| impl Encodable for Version { | ||
| fn encoded_len(&self) -> der::Result<der::Length> { | ||
| der::Length::from(1u8).for_tlv() | ||
| } | ||
|
|
||
| fn encode(&self, encoder: &mut Encoder<'_>) -> der::Result<()> { | ||
| u8::from(*self).encode(encoder) | ||
| } | ||
| } | ||
|
|
||
| impl From<Version> for u8 { | ||
| fn from(version: Version) -> Self { | ||
| version as u8 | ||
| } | ||
| } | ||
|
|
||
| impl TryFrom<u8> for Version { | ||
| type Error = der::Error; | ||
|
|
||
| fn try_from(byte: u8) -> Result<Version, der::Error> { | ||
| match byte { | ||
| 0 => Ok(Version::V1), | ||
| _ => Err(Self::TAG.value_error()), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl FixedTag for Version { | ||
| const TAG: Tag = Tag::Integer; | ||
| } |
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
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.
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.
This seems like an inextensible way to override the tag of an
Enumerated, so why not just add a#[asn1(tag = "...")]argument instead that lets you override theENUMERATEDtag however you want? Then it could potentially work for more than justINTEGER.