aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian Hurst <ark@mansus.space>2024-09-27 01:03:06 +0200
committerJulian Hurst <ark@mansus.space>2024-09-27 01:03:06 +0200
commit5b48e9c9e172ff322cc7e4dde118aa49829b79d9 (patch)
tree01829c6cb7119fb1a215b6a7f64b8699ff73ac8b
parentd7b7e927d4201a8bf4b924927657290656c919a1 (diff)
downloaddistamp-5b48e9c9e172ff322cc7e4dde118aa49829b79d9.tar.gz
Better error handling
-rw-r--r--distamp.ha32
1 files 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("<t:{}:R>", 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("<t:{}:R>", time::unix(li))!;
};