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

Adding Parser.peek_explicit/implicit_tag() #248

Closed
wants to merge 1 commit into from
Closed

Adding Parser.peek_explicit/implicit_tag() #248

wants to merge 1 commit into from

Conversation

orthecreedence
Copy link

The idea behind this change is such that when encoding an enum (via
CHOICE) using tagging, you can determine which variant to read by
peeking at the tag value without advancing the parser.

peek_explicit_tag() and peek_implicit_tag() return a tuple like

(<raw-tag>, <decoded-tag>)

such that raw-tag is the tag as it is read directly from the input
bytes, and decoded-tag is the tag converted into the same format that
would be passed to Writer.write_explicit_element() or
Writer.write_implicit_element().

peek_implicit_tag() requires knowing the type you wish to decode
up-front in order to retrieve an accurate decoded-tag value, as
implicit tagging embeds the type within the tag.

The idea behind this change is such that when encoding an enum (via
CHOICE) using tagging, you can determine which variant to read by
peeking at the tag value without advancing the parser.

`peek_explicit_tag()` and `peek_implicit_tag()` return a tuple like

```
(<raw-tag>, <decoded-tag>)
```

such that `raw-tag` is the tag as it is read directly from the input
bytes, and `decoded-tag` is the tag converted into the same format that
would be passed to `Writer.write_explicit_element()` or
`Writer.write_implicit_element()`.

`peek_implicit_tag()` requires knowing the type you wish to decode
up-front in order to retrieve an accurate `decoded-tag` value, as
implicit tagging embeds the type within the tag.
@alex
Copy link
Owner

alex commented Mar 22, 2022

Can you help me understand the use case for this vs. using read_implicit_optional_element?

@orthecreedence
Copy link
Author

Yes the case is I didn't really understand how read_optional_implicit/explicit_element worked before I went stampeding into a PR. I don't think they are terribly different!

The only real difference is the peek_*_tag methods let you match { } on the tag, but I personally don't think that's worth the cost of duplication here. Thanks for pointing me in the right direction.

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