parse static method

SenderTable parse(
  1. Uint8List bytes, {
  2. UserId? uidOf(
    1. String user
    ) = _uidOfUser,
})

bytes as a table. Throws a FormatException naming the line when a line has no kinds, a kind is not one Kind.parse takes, an attaching kind is one whose body the service reads (such as Kind.missionEnded), or a user is not one uidOf finds. uidOf defaults to a number as itself and a name as id -u prints it, and a user UserId.parse refuses (65534, nobody) is not one it finds.

Implementation

static SenderTable parse(
  Uint8List bytes, {
  UserId? Function(String user) uidOf = _uidOfUser,
}) {
  final kinds = <UserId, Set<Kind>>{};
  final attaching = <Kind>{};
  final lines = utf8.decode(bytes).split('\n');
  for (var n = 0; n < lines.length; n++) {
    final words = lines[n].trim().split(RegExp(r'\s+'));
    if (words.first.isEmpty || words.first.startsWith('#')) continue;
    if (words.length < 2) {
      throw FormatException('line ${n + 1}: ${words.first} has no kinds');
    }
    if (words.first == _attachingLabel) {
      for (final word in words.skip(1)) {
        final kind = Kind.parse(word);
        if (kind == null || OwnershipKind.of(kind) != null) {
          throw FormatException('line ${n + 1}: $word cannot attach');
        }
        attaching.add(kind);
      }
      continue;
    }
    final user = uidOf(words.first);
    if (user == null) {
      throw FormatException('line ${n + 1}: no user ${words.first}');
    }
    for (final word in words.skip(1)) {
      final kind = Kind.parse(word);
      if (kind == null) {
        throw FormatException('line ${n + 1}: $word is not a kind');
      }
      if (OwnershipKind.of(kind) == OwnershipKind.usersAssignedUids) {
        throw FormatException('line ${n + 1}: only the service writes $word');
      }
      (kinds[user] ??= {}).add(kind);
    }
  }
  return SenderTable._(bytes, kinds, attaching);
}