Redesign error handling in the decode module #65
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR changes the entire way that errors are handled while decoding.
Sources now provide errors related to them failing the get more data when there should be more via the associated
Source::Error
type. This has been renamed fromSource::Err
for consistency with other such associated types.A new type
ContentError
is introduced for errors where incorrectly encoded data is encountered, be actual encoding errors or data that isn't following the ASN.1 definition or informal profiles. This type wraps an error message that currently can be either a staticstr
or a boxedDisplay
trait object but can be extended if necessary later.Since both source and content errors can happen during decoding, the compound type
DecodeError
wraps both these types. For content errors it also stores where in source the error happened to facilitate debugging. Currently, this type only allows displaying the error. It can be extended if additional access to the internally stored error is necessary.The various content decoding methods now return a
DecodeError
.In order to implement all these changes, the
Source
trait had to be adjusted. First, it needs to be able to provide the current position. This meant that it couldn’t be implemented onBytes
and&[u8]
directly anymore. Therefore, the new traitIntoSource
allows to convert a type into itsSource
implementation.Finally, the trait's methods got cleaned up a bit. Specifically,
Source::advance
now only allows advancing as far as the length most recently returned bySource::request
which also means it cannot fail anymore but needs to panic if the length is too large. This consistent to howSource::bytes
already behaves.