transformDecoder<TOldTo, TNewTo> function
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,
),
};
}