diff --git a/der/derive/src/attributes.rs b/der/derive/src/attributes.rs index aa6f2d3f0..73e6c4f36 100644 --- a/der/derive/src/attributes.rs +++ b/der/derive/src/attributes.rs @@ -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)?)), } } } diff --git a/der/derive/src/choice.rs b/der/derive/src/choice.rs index 5bdcd9b37..3b384e894 100644 --- a/der/derive/src/choice.rs +++ b/der/derive/src/choice.rs @@ -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()); } @@ -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)* } } } diff --git a/der/derive/src/choice/variant.rs b/der/derive/src/choice/variant.rs index cb2639b0a..eeb5d2921 100644 --- a/der/derive/src/choice/variant.rs +++ b/der/derive/src/choice/variant.rs @@ -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(), } } @@ -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() ); @@ -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() );