Skip to content
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

der: support for handling DEFAULT values in SEQUENCEs #202

Merged
merged 9 commits into from
Nov 11, 2021
Merged

der: support for handling DEFAULT values in SEQUENCEs #202

merged 9 commits into from
Nov 11, 2021

Conversation

carl-wallace
Copy link
Contributor

No description provided.

abort!(attr.value, "duplicate ASN.1 `tag_mode` attribute");
}

default = Some(Ident::new(&default_attr_str, attrs[counter].span()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works for the Ident case, although I think to do this "properly" it should be a Path.

I think you can use the Parse trait to parse a Path from the LitStr (retaining span info automatically), and perhaps the value portion of AttrNameValue should retain the original LitStr.

Comment on lines 111 to 114
} else if let Some(_default_attr_str) = attr.parse_value::<String>("default") {
if default.is_some() {
abort!(attr.value, "duplicate ASN.1 `tag_mode` attribute");
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} else if let Some(_default_attr_str) = attr.parse_value::<String>("default") {
if default.is_some() {
abort!(attr.value, "duplicate ASN.1 `tag_mode` attribute");
}
} else if attr.parse_value::<String>("default").is_some() {
if default.is_some() {
abort!(attr.value, "duplicate ASN.1 `default` attribute");
}

Comment on lines +82 to +85

let mut tag_mode = None;

let mut default = None;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the added whitespace here is unnecessary... these should be grouped together as the fields-under-construction

#[asn1(type = "BIT STRING")]
pub subject_public_key: &'a [u8],
pub subject_public_key: BitString<'a>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you change this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it made the default stuff messy for not particular value to my eye. We could add it back with cost of some additional verbosity in the default bits. If we do restore, I'd prefer to wait until context specific proc macro support lands (or address in tandem with that).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd really prefer to retain the functionality, although I'd be happy with fixing it in a follow-up PR after merging this one

@tarcieri tarcieri changed the title draft support for handling DEFAULT values in SEQUENCEs der: support for handling DEFAULT values in SEQUENCEs Nov 11, 2021
@tarcieri tarcieri merged commit ce68844 into RustCrypto:master Nov 11, 2021
quote! { let #ident = #field_decoder.try_into()?; }
let ty = self.field_type.clone();
if let Some(default) = &self.attrs.default {
quote!(let mut #ident = Some(decoder.decode::<#ty>()?.unwrap_or_else(#default));)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought I'd merge this and try to clean up my remaining nits, but this seems a bit problematic. I'm not sure this is what you actually intended?

It seems like when default is present, it should attempt to decode the given type as Option<#ty>, and then call unwrap_or_default on the result, if the Option decodes successfully.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I think the Option<> on the default'ed fields will likely go away as Context Specific is worked. The goal here was to always return a value where there was a default, perhaps stretching Option a bit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see now. The Option<> became unnecessary as default support was added. Originally, the absence of the field signified default and the caller had to supply the default value. Removing Option was natural next step.

tarcieri added a commit that referenced this pull request Nov 11, 2021
PR #202 removed support for the `#[asn1(type = "...")]` attribute when
deriving `Sequence`.

This PR restores it, and also slightly changes the way the `default`
attribute is handled, with added tests.
tarcieri added a commit that referenced this pull request Nov 11, 2021
PR #202 removed support for the `#[asn1(type = "...")]` attribute when
deriving `Sequence`.

This PR restores it, and also slightly changes the way the `default`
attribute is handled, with added tests.

Additionally, this commit introduces a reference type for `OPTIONAL`
similar to the one for `CONTEXT-SPECIFIC` called `OptionalRef` that can
be used as an `Encodable` trait object for `Option<&T>`. This is used by
the custom derive when encoding fields with defaults.
tarcieri added a commit that referenced this pull request Nov 11, 2021
PR #202 removed support for the `#[asn1(type = "...")]` attribute when
deriving `Sequence`.

This PR restores it, and also slightly changes the way the `default`
attribute is handled, with added tests.

Additionally, this commit introduces a reference type for `OPTIONAL`
similar to the one for `CONTEXT-SPECIFIC` called `OptionalRef` that can
be used as an `Encodable` trait object for `Option<&T>`. This is used by
the custom derive when encoding fields with defaults.
@carl-wallace carl-wallace deleted the sequence_defaults branch November 12, 2021 11:36
@tarcieri tarcieri mentioned this pull request Nov 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants