From 5b48e9c9e172ff322cc7e4dde118aa49829b79d9 Mon Sep 17 00:00:00 2001 From: Julian Hurst Date: Fri, 27 Sep 2024 01:03:06 +0200 Subject: Better error handling --- distamp.ha | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/distamp.ha b/distamp.ha index 9cb65bb..f6aa286 100644 --- a/distamp.ha +++ b/distamp.ha @@ -4,6 +4,8 @@ use os; use strconv; use strings; +type parseerror = strconv::error; + export fn main() void = { if (len(os::args) != 2) { os::exit(os::status::FAILURE); @@ -16,16 +18,32 @@ export fn main() void = { let li = if (len(spl) > 2) { fmt::fatal("Duration format: m or m:s"); - } else if (len(spl) == 2) { - let m = strconv::stoi64(spl[0])!; - let s = strconv::stoi64(spl[1])!; + } else { + yield match (parsetime(i, spl)) { + case let inst: time::instant => + yield inst; + case let e: parseerror => + fmt::fatal(strerror(e)); + }; + }; + + fmt::printfln("", time::unix(li))!; +}; + +fn strerror(e: parseerror) str = { + return strconv::strerror(e); +}; + +fn parsetime(clk: time::instant, spl: []str) (time::instant | parseerror) = { + return if (len(spl) == 2) { + let m = strconv::stoi64(spl[0])?; + let s = strconv::stoi64(spl[1])?; let d = (m * time::MINUTE) + (s * time::SECOND); - yield time::add(i, d); + yield time::add(clk, d); } else { - let m = strconv::stoi64(spl[0])!; + let m = strconv::stoi64(spl[0])?; let d = m * time::MINUTE; - yield time::add(i, d); + yield time::add(clk, d); }; - fmt::printfln("", time::unix(li))!; }; -- cgit v1.2.3