decode method

Implementation

RitoAnnotationResponse decode(Uint8List bytes) {
  if (bytes.length > ritoMaxWireBytes) {
    throw const FormatException('RITOANR1 exceeds the byte limit.');
  }
  final reader = RitoBinaryReader(bytes);
  reader.expectMagic(_magic, 'annotation magic');
  final version = reader.uint32('annotation wire version');
  if (version != 1) {
    reader.fail('unsupported annotation wire version: $version');
  }
  if (reader.uint64('annotation total length') != bytes.length) {
    reader.fail('annotation total length does not match input');
  }
  final tag = reader.uint8('annotation level');
  if (tag >= RitoAnnotationLevel.values.length) {
    reader.fail('unknown annotation level: $tag');
  }
  final level = RitoAnnotationLevel.values[tag];
  final target = reader.option('annotation target', () => _target(reader));
  reader.finish('annotation wire message');
  final response = RitoAnnotationResponse(level: level, target: target);
  if (response.isOrphaned != (response.target == null)) {
    throw const FormatException(
      'an annotation carries a target exactly when it is not orphaned',
    );
  }
  return response;
}