resizeDecoder<T> function

Decoder<T> resizeDecoder<T>(
  1. Decoder<T> decoder,
  2. int resize(
    1. int size
    )
)

Updates the size of a given decoder using a resize function.

For fixed-size decoders, the fixedSize is updated. Variable-size decoders are returned unchanged since their size is determined dynamically.

Throws a SolanaError if the new size is negative.

Implementation

Decoder<T> resizeDecoder<T>(Decoder<T> decoder, int Function(int size) resize) {
  return switch (decoder) {
    FixedSizeDecoder<T>() => () {
      final newFixedSize = resize(decoder.fixedSize);
      if (newFixedSize < 0) {
        throw SolanaError(SolanaErrorCode.codecsExpectedPositiveByteLength, {
          'bytesLength': newFixedSize,
          'codecDescription': 'resizeDecoder',
        });
      }
      return FixedSizeDecoder<T>(fixedSize: newFixedSize, read: decoder.read);
    }(),
    VariableSizeDecoder<T>() => decoder,
  };
}