Skip to content

der/derive: support for skipped field #1008

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions der/derive/src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ pub(crate) struct FieldAttrs {

/// Is the inner type constructed?
pub constructed: bool,

/// Is the type skipped?
pub skipped: Option<Path>,
}

impl FieldAttrs {
Expand All @@ -98,6 +101,7 @@ impl FieldAttrs {
let mut optional = None;
let mut tag_mode = None;
let mut constructed = None;
let mut skipped = None;

let mut parsed_attrs = Vec::new();
AttrNameValue::from_attributes(attrs, &mut parsed_attrs);
Expand Down Expand Up @@ -154,6 +158,15 @@ impl FieldAttrs {
}

constructed = Some(ty);
// `skipped` attribute
} else if attr.parse_value::<String>("skipped").is_some() {
if skipped.is_some() {
abort!(attr.name, "duplicate ASN.1 `skipped` attribute");
}

skipped = Some(attr.value.parse().unwrap_or_else(|e| {
abort!(attr.value, "error parsing ASN.1 `skipped` attribute: {}", e)
}));
} else {
abort!(
attr.name,
Expand All @@ -171,6 +184,7 @@ impl FieldAttrs {
optional: optional.unwrap_or_default(),
tag_mode: tag_mode.unwrap_or(type_attrs.tag_mode),
constructed: constructed.unwrap_or_default(),
skipped,
}
}

Expand Down
28 changes: 21 additions & 7 deletions der/derive/src/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use field::SequenceField;
use proc_macro2::TokenStream;
use proc_macro_error::abort;
use quote::quote;
use syn::{DeriveInput, Ident, Lifetime};
use syn::{DeriveInput, Ident, Lifetime, TypeParam};

/// Derive the `Sequence` trait for a struct
pub(crate) struct DeriveSequence {
Expand All @@ -18,6 +18,9 @@ pub(crate) struct DeriveSequence {
/// Lifetime of the struct.
lifetime: Option<Lifetime>,

/// Type param of the struct.
type_param: Option<TypeParam>,

/// Fields of the struct.
fields: Vec<SequenceField>,
}
Expand All @@ -40,6 +43,8 @@ impl DeriveSequence {
.next()
.map(|lt| lt.lifetime.clone());

let type_param = input.generics.type_params().next().cloned();

let type_attrs = TypeAttrs::parse(&input.attrs);

let fields = data
Expand All @@ -51,6 +56,7 @@ impl DeriveSequence {
Self {
ident: input.ident,
lifetime,
type_param,
fields,
}
}
Expand All @@ -72,6 +78,13 @@ impl DeriveSequence {
.map(|_| lifetime.clone())
.unwrap_or_default();

let type_params = self.type_param.as_ref().map(|t| {
let mut t = t.clone();
t.default = None;
t
});
let type_params_names = self.type_param.as_ref().map(|t| t.ident.clone());

let mut decode_body = Vec::new();
let mut decode_result = Vec::new();
let mut encoded_lengths = Vec::new();
Expand All @@ -81,13 +94,14 @@ impl DeriveSequence {
decode_body.push(field.to_decode_tokens());
decode_result.push(&field.ident);

let field = field.to_encode_tokens();
encoded_lengths.push(quote!(#field.encoded_len()?));
encode_fields.push(quote!(#field.encode(writer)?;));
if let Some(field) = field.to_encode_tokens() {
encoded_lengths.push(quote!(#field.encoded_len()?));
encode_fields.push(quote!(#field.encode(writer)?;));
}
}

quote! {
impl<#lifetime> ::der::DecodeValue<#lifetime> for #ident<#lt_params> {
impl<#lifetime, #type_params> ::der::DecodeValue<#lifetime> for #ident<#lt_params #type_params_names> {
fn decode_value<R: ::der::Reader<#lifetime>>(
reader: &mut R,
header: ::der::Header,
Expand All @@ -104,7 +118,7 @@ impl DeriveSequence {
}
}

impl<#lifetime> ::der::EncodeValue for #ident<#lt_params> {
impl<#lifetime, #type_params> ::der::EncodeValue for #ident<#lt_params #type_params_names> {
fn value_len(&self) -> ::der::Result<::der::Length> {
use ::der::Encode as _;

Expand All @@ -122,7 +136,7 @@ impl DeriveSequence {
}
}

impl<#lifetime> ::der::Sequence<#lifetime> for #ident<#lt_params> {}
impl<#lifetime, #type_params> ::der::Sequence<#lifetime> for #ident<#lt_params #type_params_names> {}
}
}
}
Expand Down
71 changes: 66 additions & 5 deletions der/derive/src/sequence/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,22 @@ impl SequenceField {
}
}

if let Some(default) = &self.attrs.skipped {
let ident = &self.ident;
return quote! {
let #ident = #default();
};
}

lowerer.into_tokens(&self.ident)
}

/// Derive code for encoding a field of a sequence.
pub(super) fn to_encode_tokens(&self) -> TokenStream {
pub(super) fn to_encode_tokens(&self) -> Option<TokenStream> {
if self.attrs.skipped.is_some() {
return None;
}

let mut lowerer = LowerFieldEncoder::new(&self.ident);
let attrs = &self.attrs;

Expand All @@ -101,7 +112,7 @@ impl SequenceField {
lowerer.apply_default(&self.ident, default);
}

lowerer.into_tokens()
Some(lowerer.into_tokens())
}
}

Expand Down Expand Up @@ -241,7 +252,7 @@ mod tests {
use crate::{FieldAttrs, TagMode, TagNumber};
use proc_macro2::Span;
use quote::quote;
use syn::{punctuated::Punctuated, Ident, Path, PathSegment, Type, TypePath};
use syn::{punctuated::Punctuated, Ident, Path, PathArguments, PathSegment, Type, TypePath};

/// Create a [`Type::Path`].
pub fn type_path(ident: Ident) -> Type {
Expand Down Expand Up @@ -273,6 +284,7 @@ mod tests {
optional: false,
tag_mode: TagMode::Explicit,
constructed: false,
skipped: None,
};

let field_type = Ident::new("String", span);
Expand All @@ -292,7 +304,7 @@ mod tests {
);

assert_eq!(
field.to_encode_tokens().to_string(),
field.to_encode_tokens().unwrap().to_string(),
quote! {
self.example_field
}
Expand All @@ -313,6 +325,7 @@ mod tests {
optional: false,
tag_mode: TagMode::Implicit,
constructed: false,
skipped: None,
};

let field_type = Ident::new("String", span);
Expand Down Expand Up @@ -343,7 +356,7 @@ mod tests {
);

assert_eq!(
field.to_encode_tokens().to_string(),
field.to_encode_tokens().unwrap().to_string(),
quote! {
::der::asn1::ContextSpecificRef {
tag_number: ::der::TagNumber::N0,
Expand All @@ -354,4 +367,52 @@ mod tests {
.to_string()
);
}

#[test]
fn skipped() {
let span = Span::call_site();
let ident = Ident::new("skipped", span);

let mut segments = Punctuated::new();
segments.push(PathSegment {
ident: Ident::new("Default", span),
arguments: PathArguments::None,
});
segments.push(PathSegment {
ident: Ident::new("default", span),
arguments: PathArguments::None,
});

let attrs = FieldAttrs {
asn1_type: None,
context_specific: Some(TagNumber(0)),
default: None,
extensible: false,
optional: false,
tag_mode: TagMode::Implicit,
constructed: false,
skipped: Some(Path {
leading_colon: None,
segments,
}),
};

let field_type = Ident::new("String", span);

let field = SequenceField {
ident,
attrs,
field_type: type_path(field_type),
};

assert_eq!(
field.to_decode_tokens().to_string(),
quote! {
let skipped = Default::default();
}
.to_string()
);

assert!(field.to_encode_tokens().is_none());
}
}
4 changes: 4 additions & 0 deletions der/derive/src/value_ord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ impl ValueField {

/// Lower to [`TokenStream`].
fn to_tokens(&self) -> TokenStream {
if self.attrs.skipped.is_some() {
return TokenStream::default();
}

let ident = &self.ident;

if self.is_enum {
Expand Down