Skip to content
Merged
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
29 changes: 19 additions & 10 deletions der/derive/src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,19 +247,28 @@ impl FieldAttrs {
}
}

/// Get a `der::Encoder` object which respects these field attributes.
pub fn encoder(&self, binding: &TokenStream) -> TokenStream {
if let Some(tag_number) = self.context_specific {
let tag_number = tag_number.to_tokens();
let tag_mode = self.tag_mode.to_tokens();
quote!(encoder.context_specific(#tag_number, #tag_mode, #binding))
} else {
self.asn1_type
/// Get tokens to encode the binding using `::der::EncodeValue`.
pub fn value_encode(&self, binding: &TokenStream) -> TokenStream {
match self.context_specific {
Some(tag_number) => {
let tag_number = tag_number.to_tokens();
let tag_mode = self.tag_mode.to_tokens();
quote! {
::der::asn1::ContextSpecificRef {
tag_number: #tag_number,
tag_mode: #tag_mode,
value: #binding,
}.encode_value(encoder)
}
}

None => self
.asn1_type
.map(|ty| {
let encoder_obj = ty.encoder(binding);
quote!(#encoder_obj.encode(encoder))
quote!(#encoder_obj.encode_value(encoder))
})
.unwrap_or_else(|| quote!(encoder.encode(#binding)?))
.unwrap_or_else(|| quote!(encoder.encode_value(#binding)?)),
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions der/derive/src/choice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@ impl DeriveChoice {
let mut can_decode_body = Vec::new();
let mut decode_body = Vec::new();
let mut encode_body = Vec::new();
let mut encoded_len_body = Vec::new();
let mut value_len_body = Vec::new();
let mut tagged_body = Vec::new();

for variant in &self.variants {
can_decode_body.push(variant.tag.to_tokens());
decode_body.push(variant.to_decode_tokens());
encode_body.push(variant.to_encode_tokens());
encoded_len_body.push(variant.to_encoded_len_tokens());
encode_body.push(variant.to_encode_value_tokens());
value_len_body.push(variant.to_value_len_tokens());
tagged_body.push(variant.to_tagged_tokens());
}

Expand All @@ -107,16 +107,16 @@ impl DeriveChoice {
}
}

impl<#lt_params> ::der::Encodable for #ident<#lt_params> {
fn encode(&self, encoder: &mut ::der::Encoder<'_>) -> ::der::Result<()> {
impl<#lt_params> ::der::EncodeValue for #ident<#lt_params> {
fn encode_value(&self, encoder: &mut ::der::Encoder<'_>) -> ::der::Result<()> {
match self {
#(#encode_body)*
}
}

fn encoded_len(&self) -> ::der::Result<::der::Length> {
fn value_len(&self) -> ::der::Result<::der::Length> {
match self {
#(#encoded_len_body)*
#(#value_len_body)*
}
}
}
Expand Down
28 changes: 14 additions & 14 deletions der/derive/src/choice/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,21 @@ impl ChoiceVariant {
}
}

/// Derive a match arm for the impl body for `der::Encodable::encode`.
pub(super) fn to_encode_tokens(&self) -> TokenStream {
/// Derive a match arm for the impl body for `der::EncodeValue::encode_value`.
pub(super) fn to_encode_value_tokens(&self) -> TokenStream {
let ident = &self.ident;
let binding = quote!(variant);
let encoder = self.attrs.encoder(&binding);
let encoder = self.attrs.value_encode(&binding);
quote! {
Self::#ident(#binding) => #encoder,
}
}

/// Derive a match arm for the impl body for `der::Encodable::encode`.
pub(super) fn to_encoded_len_tokens(&self) -> TokenStream {
/// Derive a match arm for the impl body for `der::EncodeValue::value_len`.
pub(super) fn to_value_len_tokens(&self) -> TokenStream {
let ident = &self.ident;
quote! {
Self::#ident(variant) => variant.encoded_len(),
Self::#ident(variant) => variant.value_len(),
}
}

Expand Down Expand Up @@ -110,17 +110,17 @@ mod tests {
);

assert_eq!(
variant.to_encode_tokens().to_string(),
variant.to_encode_value_tokens().to_string(),
quote! {
Self::ExampleVariant(variant) => encoder.encode(variant)?,
Self::ExampleVariant(variant) => encoder.encode_value(variant)?,
}
.to_string()
);

assert_eq!(
variant.to_encoded_len_tokens().to_string(),
variant.to_value_len_tokens().to_string(),
quote! {
Self::ExampleVariant(variant) => variant.encoded_len(),
Self::ExampleVariant(variant) => variant.value_len(),
}
.to_string()
);
Expand Down Expand Up @@ -159,17 +159,17 @@ mod tests {
);

assert_eq!(
variant.to_encode_tokens().to_string(),
variant.to_encode_value_tokens().to_string(),
quote! {
Self::ImplicitVariant(variant) => encoder.encode(variant)?,
Self::ImplicitVariant(variant) => encoder.encode_value(variant)?,
}
.to_string()
);

assert_eq!(
variant.to_encoded_len_tokens().to_string(),
variant.to_value_len_tokens().to_string(),
quote! {
Self::ImplicitVariant(variant) => variant.encoded_len(),
Self::ImplicitVariant(variant) => variant.value_len(),
}
.to_string()
);
Expand Down