The Decoder class is the base type to implement custom decoders. It has three type parameters:
In for input. The input to a decoder can be any raw value that your decoder
can extract information from. In a simple case it could be a
or an array of bytes. If decoders are chained, the input needs to be
transferred from one decoder to the following one. That is why
DecodeResult has an
Input as well. It is important to notice that
what is passed to the next decoder needs to be the relative input and not the
original one. For efficiency reasons, most decoders will wrap their own input
into some more sophisticated type to allow tracking the current position in
Err for error. It's the type of the error message. In some
implementations it might be as simple as
string. For the embedded decoders
(text and value), the error is fixed to the
DecodeError unione type.
The constructors for DecodeError have more semantic value than
strings and can be properly used for localization.
These placeholder (
_E) types are not expected to bring any
value. They exist to allow inspecting the main types of a Decoder at
Expect the current decoder to be repeated at least n
The result is an array of
Repeat the current decoder at most n
Notice that this allows for zero successes. Which makes this decoder optional.
Repeat the current decoder between
Repeat the current decoder zero or more times.
Convert and/or transform the result of a decoding into a different value.
The applied function argument is executed exclusively on a succesful result of decoding.
Repeat the current decoder exactly n
Allow to convert the result of decoder into the input for another one. It can be used to use different kind of decoders together.
If the current decoder is successful it passes its value to
and succeed the decoding if the function returns
failure is provided to have an explicit error if
If the current decoder fails, replace its error with the value of
If the current decoder executes succesfully, ignore its result and
replace it with