Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 185
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| 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()) | ||
| } | ||
| } | ||
| } | ||
| 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; | ||
| } |
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.