move parse methods to result type

This commit is contained in:
Daniel Ledda
2025-01-23 16:44:51 +01:00
parent 068c41a15d
commit a337c0d9ac
3 changed files with 33 additions and 25 deletions

27
app.cpp
View File

@@ -143,19 +143,19 @@ int gymTrackerStatus(Arena *arena, list<string> args) {
GymLogDbParsed *db = parseDb(arena, os_readEntireFile(arena, DB_FILE_LOCATION));
Timestamp startTs = {0};
int numDays = 1;
ParsePositiveIntResult numDays = {1, false};
bool showAll = args.length == 1 && strEql(args.data[0], "--all"_s);
if (!showAll) {
if (args.length == 2 && (strEql(args.data[0], "--days"_s) || strEql(args.data[0], "-d"_s))) {
size_t l;
numDays = parsePositiveInt(args.data[1], &l);
}
if (numDays == -1) {
if (!numDays.valid) {
puts("Bad argument for --days (-d) parameter.");
statusCode = 1;
} else {
uint64 todayUnix = getSystemUnixTime();
UnixTimestamp startUnix = todayUnix - numDays * 24 * 60 * 60;
UnixTimestamp startUnix = todayUnix - numDays.result * 24 * 60 * 60;
startTs = timestampFromUnixTime(&startUnix);
}
}
@@ -267,14 +267,14 @@ int gymTrackerDeleteEntries(Arena *arena, list<string> args) {
statusCode = 1;
} else {
size_t position = 0;
int numToDelete = parsePositiveInt(args.data[0], &position);
if (numToDelete != -1) {
ParsePositiveIntResult numToDeleteParsed = parsePositiveInt(args.data[0], &position);
if (numToDeleteParsed.valid) {
list<GymLogEntry> logEntries = loadEntryLog(arena, LOG_FILE_LOCATION);
if (numToDelete > logEntries.length) {
log("%i is more than the current number of log entries (%i). Aborting.", numToDelete, logEntries.length);
if (numToDeleteParsed.result > logEntries.length) {
log("%i is more than the current number of log entries (%i). Aborting.", numToDeleteParsed, logEntries.length);
statusCode = 1;
} else {
os_writeEntireFile(arena, LOG_FILE_LOCATION, (byte *)logEntries.data, (logEntries.length - numToDelete) * sizeof(GymLogEntry));
os_writeEntireFile(arena, LOG_FILE_LOCATION, (byte *)logEntries.data, (logEntries.length - numToDeleteParsed.result) * sizeof(GymLogEntry));
}
} else {
log("Invalid number to delete.\n");
@@ -321,17 +321,18 @@ int gymTrackerDo(Arena *arena, list<string> args) {
if (statusCode == 0) {
exercise.id = existingEntry->id;
size_t parsedCount = 0;
real32 kg = parsePositiveReal32(args.data[1], &parsedCount);
uint8 reps = parsePositiveInt(args.data[2], &parsedCount);
if (parsedCount == 0 || kg == NAN || reps == 0 || kg == 0) {
ParsePositiveReal32Result kg = parsePositiveReal32(args.data[1], &parsedCount);
ParsePositiveIntResult reps = parsePositiveInt(args.data[2], &parsedCount);
if (!kg.valid || !reps.valid) {
log("%zu, %f, %\n", parsedCount, kg, reps);
log("Invalid reps or weight input.\n");
statusCode = 1;
} else {
GymLogEntry entry = {
getSystemUnixTime(),
exercise.id,
reps,
kg,
reps.result,
kg.result,
};
os_fileAppend(arena, LOG_FILE_LOCATION, (byte *)&entry, sizeof(entry));