From d6b7ce98c18c29e62ea7d137db24f06574d447c3 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Sat, 14 Jan 2023 19:08:35 +0100 Subject: [PATCH] feat(cron): proper parse error handling --- src/cron/expression.c.v | 20 +++++++++++++++++++- src/cron/expression.v | 2 +- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/cron/expression.c.v b/src/cron/expression.c.v index 7551e6f..0698432 100644 --- a/src/cron/expression.c.v +++ b/src/cron/expression.c.v @@ -28,6 +28,24 @@ struct C.cron_simple_time { type SimpleTime = C.cron_simple_time +enum ParseError as u8 { + ok = 0 + invalid_expression = 1 + invalid_number = 2 + out_of_range = 3 + too_many_parts = 4 +} + +fn (e ParseError) str() string { + return match e { + .ok { '' } + .invalid_expression { 'Invalid expression' } + .invalid_number { 'Invalid number' } + .out_of_range { 'Out of range' } + .too_many_parts { 'Too many parts' } + } +} + fn C.ce_init() &C.cron_expression fn C.ce_free(ce &C.cron_expression) @@ -36,4 +54,4 @@ fn C.ce_next(out &C.cron_simple_time, ce &C.cron_expression, ref &C.cron_simple_ fn C.ce_next_from_now(out &C.cron_simple_time, ce &C.cron_expression) -fn C.ce_parse_expression(out &C.cron_expression, s &char) int +fn C.ce_parse_expression(out &C.cron_expression, s &char) ParseError diff --git a/src/cron/expression.v b/src/cron/expression.v index c0cab8d..4a0d04c 100644 --- a/src/cron/expression.v +++ b/src/cron/expression.v @@ -11,7 +11,7 @@ pub fn parse_expression(exp string) !&Expression { out := C.ce_init() res := C.ce_parse_expression(out, exp.str) - if res != 0 { + if res != .ok { return error(res.str()) }