transformDecoder<TOldTo, TNewTo> function

Decoder<TNewTo> transformDecoder<TOldTo, TNewTo>(
  1. Decoder<TOldTo> decoder,
  2. TNewTo map(
    1. TOldTo value,
    2. Uint8List bytes,
    3. int offset
    )
)

Transforms a decoder by mapping its output values.

Takes an existing Decoder<TOldTo> and returns a Decoder<TNewTo>, converting decoded values of type TOldTo into TNewTo via the map function.

Implementation

Decoder<TNewTo> transformDecoder<TOldTo, TNewTo>(
  Decoder<TOldTo> decoder,
  TNewTo Function(TOldTo value, Uint8List bytes, int offset) map,
) {
  return switch (decoder) {
    FixedSizeDecoder<TOldTo>() => FixedSizeDecoder<TNewTo>(
      fixedSize: decoder.fixedSize,
      read: (bytes, offset) {
        final (value, newOffset) = decoder.read(bytes, offset);
        return (map(value, bytes, offset), newOffset);
      },
    ),
    VariableSizeDecoder<TOldTo>() => VariableSizeDecoder<TNewTo>(
      read: (bytes, offset) {
        final (value, newOffset) = decoder.read(bytes, offset);
        return (map(value, bytes, offset), newOffset);
      },
      maxSize: decoder.maxSize,
    ),
  };
}