parse static method
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 Kind.usersAssignedToJourneyPath or Kind.journeyEnded, or a
user is not one uidOf finds. uidOf defaults to a number as itself
and a name as id -u prints it.
Implementation
static SenderTable parse(
Uint8List bytes, {
int? Function(String user) uidOf = _uidOf,
}) {
final kinds = <int, 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 ||
kind == Kind.usersAssignedToJourneyPath ||
kind == Kind.journeyEnded) {
throw FormatException('line ${n + 1}: $word cannot attach');
}
attaching.add(kind);
}
continue;
}
final uid = uidOf(words.first);
if (uid == 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');
}
(kinds[uid] ??= {}).add(kind);
}
}
return SenderTable._(bytes, kinds, attaching);
}