readTrajectoryStart<R> function
The logbook entry entry (its bytes as the logbook holds them, the
<kind>\n<previous>\n header included, which its ids are hashes of),
read for the trajectory it starts. rules reads the rules from their
JSON, null when they aren't rules.
Implementation
AdmittedTrajectoryEntry<R> readTrajectoryStart<R>(
Uint8List entry, {
required R? Function(Object? json) rules,
}) {
UnreadableTrajectoryEntry<R> unreadable(String why) =>
UnreadableTrajectoryEntry(why);
final first = entry.indexOf(0x0a);
final second = first < 0 ? -1 : entry.indexOf(0x0a, first + 1);
if (second < 0) return unreadable('no `<kind>\\n<previous>\\n` header');
final kind = ascii.decode(
Uint8List.sublistView(entry, 0, first),
allowInvalid: true,
);
// The entry's own header. Kind's package stays out of the airlock's imports.
// wire-ok: the logbook entry header, read before the body
if (kind != 'event-admitted-to-mission') {
return unreadable('kind `$kind`, not `event-admitted-to-mission`');
}
final JsonObject written;
try {
written = JsonObject.decode(
utf8.decode(Uint8List.sublistView(entry, second + 1)),
);
} on FormatException catch (e) {
return unreadable('not a JSON object: ${e.message}');
}
final json = withoutKnownDecisionFormat(written);
if (json == null) {
return unreadable(
'`$decisionFormatField` is a format this does not read: '
'${written.raw(decisionFormatField)}',
);
}
final fields = {'event', 'by', 'translatedEvent'};
final event = switch (json.raw('event')) {
final String hex => ObjectId.parse(hex),
_ => null,
};
if (event == null) return unreadable('`event` is not an entry hash');
final by = switch (json.raw('by')) {
final String name => DecidedBy.values.asNameMap()[name],
_ => null,
};
if (by == null) return unreadable('`by` is not who decided');
final eventCase = switch (JsonObject.parse(json.raw('translatedEvent'))
?.raw('case')) {
final String eventCase => eventCase,
_ => null,
};
if (eventCase == null) {
return unreadable('`translatedEvent` names no case');
}
// Where it goes: under a trajectory, or into a mission, starting a trajectory
// or not.
TrajectoryId? onTrajectory;
MissionId? inMission;
var startsMission = false;
var startsTrajectory = false;
if (json.has('trajectory')) {
onTrajectory = switch (json.raw('trajectory')) {
final String hex => TrajectoryId.parse(hex),
_ => null,
};
if (onTrajectory == null) return unreadable('`trajectory` is not an id');
fields.add('trajectory');
} else {
if (json.has('mission') == json.has('startsMission')) {
return unreadable('not exactly one of `mission` and `startsMission`');
}
if (json.has('mission')) {
inMission = switch (json.raw('mission')) {
final String hex => MissionId.parse(hex),
_ => null,
};
if (inMission == null) return unreadable('`mission` is not an id');
fields.add('mission');
} else {
if (json.raw('startsMission') != true) {
return unreadable('`startsMission` is not true');
}
startsMission = true;
fields.add('startsMission');
}
if (json.has('startsTrajectory')) {
if (json.raw('startsTrajectory') != true) {
return unreadable('`startsTrajectory` is not true');
}
startsTrajectory = true;
fields.add('startsTrajectory');
}
}
// Rules exactly when it starts a mission or a trajectory, as
// EventAdmittedToMission.
final R? read;
if (json.has('rules')) {
read = rules(json.raw('rules'));
if (read == null) return unreadable('`rules` are not rules');
fields.add('rules');
} else {
read = null;
}
if ((read != null) != (startsTrajectory || startsMission)) {
return unreadable('`rules` not given exactly when something starts');
}
// Where replies go: only beside rules, and read without decoding the
// translated event it is copied from.
ReplyEndpoint? replyEndpoint;
if (json.has('replyEndpoint')) {
if (read == null) {
return unreadable('`replyEndpoint` on an entry that starts nothing');
}
try {
replyEndpoint = decodeReplyEndpoint(
json.raw('replyEndpoint'),
'replyEndpoint',
);
} on FormatException catch (e) {
return unreadable(e.message);
}
fields.add('replyEndpoint');
}
// Whether it starts a turn, exactly for something said, and never
// false on a trajectory's start.
// A case this version doesn't know is still no reason to stop: only its
// being something said changes what the entry must hold.
final said =
TranslatedEventCase.parse(eventCase) == TranslatedEventCase.messageSaid;
if (json.has('startsTurn') != said) {
return unreadable('`startsTurn` not given exactly for something said');
}
if (said) {
final startsTurn = json.raw('startsTurn');
if (startsTurn is! bool) return unreadable('`startsTurn` is not a bool');
if (startsTrajectory && !startsTurn) {
return unreadable('a path started by something that starts no turn');
}
fields.add('startsTurn');
}
if (json.keys.length != fields.length) {
return unreadable(
'fields beyond ${fields.join(', ')}: '
'${json.keys.where((k) => !fields.contains(k)).join(', ')}',
);
}
if (!startsTrajectory) return EntryStartsNoTrajectory();
// Never null here: rules are given exactly when something starts.
if (read == null) return unreadable('a trajectory start with no `rules`');
return EntryStartsTrajectory(
event: event,
by: by,
trajectory: TrajectoryId.of(entry),
mission: inMission ?? MissionId.of(entry),
rules: read,
replyEndpoint: replyEndpoint,
);
}