diff options
author | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2022-12-10 08:20:31 +0000 |
---|---|---|
committer | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2022-12-10 08:20:31 +0000 |
commit | 7df1e3b0348e9f42960f7a51904230398a2f93b8 (patch) | |
tree | 94666274885da2961c5d6e2a12d6b35992a388df | |
parent | 9fd19b05d23b1e4d617a76c472b64e1d30f4076b (diff) | |
parent | 19be86c88c8fc2cbf9f8c122c4b8068d6b6a877d (diff) | |
download | der_derive-7df1e3b0348e9f42960f7a51904230398a2f93b8.tar.gz |
Snap for 9391130 from 19be86c88c8fc2cbf9f8c122c4b8068d6b6a877d to udc-release
Change-Id: I16f1ac5bec648d07e37f7ac0c8d6465166fa9b03
-rw-r--r-- | .cargo_vcs_info.json | 2 | ||||
-rw-r--r-- | Android.bp | 4 | ||||
-rw-r--r-- | CHANGELOG.md | 6 | ||||
-rw-r--r-- | Cargo.toml | 3 | ||||
-rw-r--r-- | Cargo.toml.orig | 2 | ||||
-rw-r--r-- | LICENSE-MIT | 25 | ||||
-rw-r--r-- | METADATA | 13 | ||||
-rw-r--r-- | src/asn1_type.rs | 18 | ||||
-rw-r--r-- | src/value_ord.rs | 117 |
9 files changed, 147 insertions, 43 deletions
diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json index 24adda8..b80855a 100644 --- a/.cargo_vcs_info.json +++ b/.cargo_vcs_info.json @@ -1,6 +1,6 @@ { "git": { - "sha1": "4abe0399865eb66112723f8e15e2d17bad00dba8" + "sha1": "1dccbc1893d5bae33959e0ccb496b9ce5a7de122" }, "path_in_vcs": "der/derive" }
\ No newline at end of file @@ -1,8 +1,6 @@ // This file is generated by cargo2android.py --config cargo2android.json. // Do not modify this file as changes will be overridden on upgrade. - - package { default_applicable_licenses: ["external_rust_crates_der_derive_license"], } @@ -37,7 +35,7 @@ rust_proc_macro { name: "libder_derive", crate_name: "der_derive", cargo_env_compat: true, - cargo_pkg_version: "0.6.0", + cargo_pkg_version: "0.6.1", srcs: ["src/lib.rs"], edition: "2021", rustlibs: [ diff --git a/CHANGELOG.md b/CHANGELOG.md index 92111f1..b4a9502 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## 0.6.1 (2022-12-05) +### Added +- Support for deriving `ValueOrd` on `Choice` enums ([#723]) + +[#723]: https://github.com/RustCrypto/formats/pull/723 + ## 0.6.0 (2022-05-08) ### Added - Support for Context-Specific fields with default values ([#246]) @@ -13,7 +13,7 @@ edition = "2021" rust-version = "1.56" name = "der_derive" -version = "0.6.0" +version = "0.6.1" authors = ["RustCrypto Developers"] description = "Custom derive support for the `der` crate's `Choice` and `Sequence` traits" documentation = "https://docs.rs/der" @@ -34,7 +34,6 @@ categories = [ ] license = "Apache-2.0 OR MIT" repository = "https://github.com/RustCrypto/formats/tree/master/der/derive" -resolver = "2" [lib] proc-macro = true diff --git a/Cargo.toml.orig b/Cargo.toml.orig index 058401f..9360572 100644 --- a/Cargo.toml.orig +++ b/Cargo.toml.orig @@ -1,6 +1,6 @@ [package] name = "der_derive" -version = "0.6.0" +version = "0.6.1" description = "Custom derive support for the `der` crate's `Choice` and `Sequence` traits" authors = ["RustCrypto Developers"] license = "Apache-2.0 OR MIT" diff --git a/LICENSE-MIT b/LICENSE-MIT new file mode 100644 index 0000000..1b78809 --- /dev/null +++ b/LICENSE-MIT @@ -0,0 +1,25 @@ +Copyright (c) 2020-2022 The RustCrypto Project Developers + +Permission is hereby granted, free of charge, to any +person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without +limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice +shall be included in all copies or substantial portions +of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. @@ -1,3 +1,7 @@ +# This project was upgraded with external_updater. +# Usage: tools/external_updater/updater.sh update rust/crates/der_derive +# For more info, check https://cs.android.com/android/platform/superproject/+/master:tools/external_updater/README.md + name: "der_derive" description: "Custom derive support for the `der` crate\'s `Choice` and `Sequence` traits" third_party { @@ -7,14 +11,13 @@ third_party { } url { type: ARCHIVE - value: "https://static.crates.io/crates/der_derive/der_derive-0.6.0.crate" + value: "https://static.crates.io/crates/der_derive/der_derive-0.6.1.crate" } - version: "0.6.0" - # Dual-licensed, using the least restrictive per go/thirdpartylicenses#same. + version: "0.6.1" license_type: NOTICE last_upgrade_date { year: 2022 - month: 9 - day: 6 + month: 12 + day: 9 } } diff --git a/src/asn1_type.rs b/src/asn1_type.rs index 787e054..cd21614 100644 --- a/src/asn1_type.rs +++ b/src/asn1_type.rs @@ -23,6 +23,12 @@ pub(crate) enum Asn1Type { /// ASN.1 `PrintableString`. PrintableString, + /// ASN.1 `TeletexString`. + TeletexString, + + /// ASN.1 `VideotexString`. + VideotexString, + /// ASN.1 `UTCTime`. UtcTime, @@ -39,6 +45,8 @@ impl Asn1Type { Asn1Type::GeneralizedTime => quote!(::der::Tag::GeneralizedTime), Asn1Type::OctetString => quote!(::der::Tag::OctetString), Asn1Type::PrintableString => quote!(::der::Tag::PrintableString), + Asn1Type::TeletexString => quote!(::der::Tag::TeletexString), + Asn1Type::VideotexString => quote!(::der::Tag::VideotexString), Asn1Type::UtcTime => quote!(::der::Tag::UtcTime), Asn1Type::Utf8String => quote!(::der::Tag::Utf8String), } @@ -52,6 +60,8 @@ impl Asn1Type { Asn1Type::GeneralizedTime => quote!(::der::asn1::GeneralizedTime::decode(reader)?), Asn1Type::OctetString => quote!(::der::asn1::OctetStringRef::decode(reader)?), Asn1Type::PrintableString => quote!(::der::asn1::PrintableStringRef::decode(reader)?), + Asn1Type::TeletexString => quote!(::der::asn1::TeletexStringRef::decode(reader)?), + Asn1Type::VideotexString => quote!(::der::asn1::VideotexStringRef::decode(reader)?), Asn1Type::UtcTime => quote!(::der::asn1::UtcTime::decode(reader)?), Asn1Type::Utf8String => quote!(::der::asn1::Utf8StringRef::decode(reader)?), } @@ -65,6 +75,8 @@ impl Asn1Type { Asn1Type::Ia5String | Asn1Type::OctetString | Asn1Type::PrintableString + | Asn1Type::TeletexString + | Asn1Type::VideotexString | Asn1Type::Utf8String => quote!(#type_path::new(#binding)?), _ => quote!(#type_path::try_from(#binding)?), } @@ -79,6 +91,8 @@ impl Asn1Type { Asn1Type::GeneralizedTime => quote!(::der::asn1::GeneralizedTime), Asn1Type::OctetString => quote!(::der::asn1::OctetStringRef), Asn1Type::PrintableString => quote!(::der::asn1::PrintableStringRef), + Asn1Type::TeletexString => quote!(::der::asn1::TeletexStringRef), + Asn1Type::VideotexString => quote!(::der::asn1::VideotexStringRef), Asn1Type::UtcTime => quote!(::der::asn1::UtcTime), Asn1Type::Utf8String => quote!(::der::asn1::Utf8StringRef), } @@ -95,6 +109,8 @@ impl FromStr for Asn1Type { "GeneralizedTime" => Ok(Self::GeneralizedTime), "OCTET STRING" => Ok(Self::OctetString), "PrintableString" => Ok(Self::PrintableString), + "TeletexString" => Ok(Self::TeletexString), + "VideotexString" => Ok(Self::VideotexString), "UTCTime" => Ok(Self::UtcTime), "UTF8String" => Ok(Self::Utf8String), _ => Err(ParseError), @@ -110,6 +126,8 @@ impl fmt::Display for Asn1Type { Asn1Type::GeneralizedTime => "GeneralizedTime", Asn1Type::OctetString => "OCTET STRING", Asn1Type::PrintableString => "PrintableString", + Asn1Type::TeletexString => "TeletexString", + Asn1Type::VideotexString => "VideotexString", Asn1Type::UtcTime => "UTCTime", Asn1Type::Utf8String => "UTF8String", }) diff --git a/src/value_ord.rs b/src/value_ord.rs index 6b08f7f..97e37ca 100644 --- a/src/value_ord.rs +++ b/src/value_ord.rs @@ -21,6 +21,9 @@ pub(crate) struct DeriveValueOrd { /// Fields of structs or enum variants. fields: Vec<ValueField>, + + /// Type of input provided (`enum` or `struct`). + input_type: InputType, } impl DeriveValueOrd { @@ -36,17 +39,21 @@ impl DeriveValueOrd { .next() .map(|lt| lt.lifetime.clone()); - let fields = match input.data { - syn::Data::Enum(data) => data - .variants - .into_iter() - .map(|variant| ValueField::new_enum(variant, &type_attrs)) - .collect(), - syn::Data::Struct(data) => data - .fields - .into_iter() - .map(|field| ValueField::new_struct(field, &type_attrs)) - .collect(), + let (fields, input_type) = match input.data { + syn::Data::Enum(data) => ( + data.variants + .into_iter() + .map(|variant| ValueField::new_enum(variant, &type_attrs)) + .collect(), + InputType::Enum, + ), + syn::Data::Struct(data) => ( + data.fields + .into_iter() + .map(|field| ValueField::new_struct(field, &type_attrs)) + .collect(), + InputType::Struct, + ), _ => abort!( ident, "can't derive `ValueOrd` on this type: \ @@ -58,6 +65,7 @@ impl DeriveValueOrd { ident, lifetime, fields, + input_type, } } @@ -79,36 +87,70 @@ impl DeriveValueOrd { body.push(field.to_tokens()); } - quote! { - impl<#(#lt_params)*> ::der::ValueOrd for #ident<#(#lt_params)*> { - fn value_cmp(&self, other: &Self) -> ::der::Result<::core::cmp::Ordering> { + let body = match self.input_type { + InputType::Enum => { + quote! { + #[allow(unused_imports)] + use ::der::ValueOrd; + match (self, other) { + #(#body)* + _ => unreachable!(), + } + } + } + InputType::Struct => { + quote! { #[allow(unused_imports)] - use ::der::DerOrd; + use ::der::{DerOrd, ValueOrd}; #(#body)* Ok(::core::cmp::Ordering::Equal) } } + }; + + quote! { + impl<#(#lt_params)*> ::der::ValueOrd for #ident<#(#lt_params)*> { + fn value_cmp(&self, other: &Self) -> ::der::Result<::core::cmp::Ordering> { + #body + } + } } } } +/// What kind of input was provided (i.e. `enum` or `struct`). +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +enum InputType { + /// Input is an `enum`. + Enum, + + /// Input is a `struct`. + Struct, +} + struct ValueField { /// Name of the field ident: Ident, /// Field-level attributes. attrs: FieldAttrs, + + is_enum: bool, } impl ValueField { /// Create from an `enum` variant. - fn new_enum(variant: Variant, _: &TypeAttrs) -> Self { - abort!( - variant, - "deriving `ValueOrd` only presently supported for structs" - ); + fn new_enum(variant: Variant, type_attrs: &TypeAttrs) -> Self { + let ident = variant.ident; + + let attrs = FieldAttrs::parse(&variant.attrs, type_attrs); + Self { + ident, + attrs, + is_enum: true, + } } /// Create from a `struct` field. @@ -120,24 +162,37 @@ impl ValueField { .unwrap_or_else(|| abort!(&field, "tuple structs are not supported")); let attrs = FieldAttrs::parse(&field.attrs, type_attrs); - Self { ident, attrs } + Self { + ident, + attrs, + is_enum: false, + } } /// Lower to [`TokenStream`]. fn to_tokens(&self) -> TokenStream { let ident = &self.ident; - let mut binding1 = quote!(self.#ident); - let mut binding2 = quote!(other.#ident); - if let Some(ty) = &self.attrs.asn1_type { - binding1 = ty.encoder(&binding1); - binding2 = ty.encoder(&binding2); - } + if self.is_enum { + let binding1 = quote!(Self::#ident(this)); + let binding2 = quote!(Self::#ident(other)); + quote! { + (#binding1, #binding2) => this.value_cmp(other), + } + } else { + let mut binding1 = quote!(self.#ident); + let mut binding2 = quote!(other.#ident); - quote! { - match #binding1.der_cmp(&#binding2)? { - ::core::cmp::Ordering::Equal => (), - other => return Ok(other), + if let Some(ty) = &self.attrs.asn1_type { + binding1 = ty.encoder(&binding1); + binding2 = ty.encoder(&binding2); + } + + quote! { + match #binding1.der_cmp(&#binding2)? { + ::core::cmp::Ordering::Equal => (), + other => return Ok(other), + } } } } |