commit: 2d9e78f676d24490740605cb65184f8ecf5d5f28
parent 4a0166e23b319388c7356c88a3ca2851b7438395
Author: Drew DeVault <sir@cmpwn.com>
Date: Fri, 18 Feb 2022 10:29:29 +0100
Improve error handling types
The "ok" type should not be an error type (!void) because it is not an
error. In this situation, it is preferred to simply use "void" to
represent the success case. I also replaced "invalid" with the
off-the-shelf errors::invalid type.
Diffstat:
M | main.ha | 30 | +++++++++++++----------------- |
1 file changed, 13 insertions(+), 17 deletions(-)
diff --git a/main.ha b/main.ha
@@ -1,6 +1,7 @@
// Copyright © 2019-2022 Haelwenn (lanodan) Monnier <contact+deblob-notice@hacktivis.me>
// SPDX-License-Identifier: BSD-3-Clause
use bytes;
+use errors;
use fmt;
use fnmatch;
use fs;
@@ -10,9 +11,6 @@ use os;
use path;
use strings;
-type invalid = !void;
-type ok = !void;
-
let excludes: []str = [];
let noop: bool = false;
@@ -22,7 +20,7 @@ const magic: [_]str = [
"\x55\xAA",
];
-fn is_blob(filename: str) (bool | invalid) = {
+fn is_blob(filename: str) (bool | errors::invalid) = {
static let buffer: [512]u8 = [0...];
const file = match (os::open(filename)) {
@@ -30,7 +28,7 @@ fn is_blob(filename: str) (bool | invalid) = {
yield f;
case let e: fs::error =>
fmt::errorf("os::open({}): {}\n", filename, fs::strerror(e))!;
- return invalid;
+ return errors::invalid;
};
defer io::close(file);
@@ -40,10 +38,10 @@ fn is_blob(filename: str) (bool | invalid) = {
case io::EOF =>
// empty file
//fmt::errorf("EOF reading: {}\n", filename)!;
- return invalid;
+ return errors::invalid;
case let e: io::error =>
fmt::errorf("Error reading `{}`: {}\n", filename, io::strerror(e))!;
- return invalid;
+ return errors::invalid;
};
for (let i = 0z; i < len(magic); i += 1) {
@@ -72,12 +70,12 @@ fn is_blob(filename: str) (bool | invalid) = {
if (got != tests[i].1) {
fmt::fatal("is_blob({}) was incorrect, got {}, expected {}", tests[i].0, got, tests[i].1);
};
- case invalid =>
+ case errors::invalid =>
fmt::fatal("is_blob({}) was incorrect, got invalid, expected {}", tests[i].0, tests[i].1);
};
};
- assert(is_blob("test/fixtures/empty") is invalid);
+ assert(is_blob("test/fixtures/empty") is errors::invalid);
};
fn is_excluded(filename: str) bool = {
@@ -89,13 +87,13 @@ fn is_excluded(filename: str) bool = {
return false;
};
-fn check_dir(dirname: str, ignoring: bool) (ok | invalid) = {
+fn check_dir(dirname: str, ignoring: bool) (void | errors::invalid) = {
const iter = match (os::iter(dirname)) {
case let iter: *fs::iterator =>
yield iter;
case let err: fs::error =>
fmt::errorf("os::iter({}): {}\n", dirname, fs::strerror(err))!;
- return invalid;
+ return errors::invalid;
};
for (true) {
@@ -122,7 +120,7 @@ fn check_dir(dirname: str, ignoring: bool) (ok | invalid) = {
const is_blob = match (is_blob(filename)) {
case let b: bool =>
yield b;
- case invalid =>
+ case errors::invalid =>
continue;
};
@@ -158,8 +156,6 @@ fn check_dir(dirname: str, ignoring: bool) (ok | invalid) = {
continue;
};
};
-
- return ok;
};
@test fn check_dir() void = {
@@ -173,7 +169,7 @@ fn check_dir(dirname: str, ignoring: bool) (ok | invalid) = {
};
const ret = check_dir(dirname, false);
- assert(ret is ok);
+ assert(ret is void);
const files_after = match (os::readdir(dirname)) {
case let d: []fs::dirent =>
@@ -227,9 +223,9 @@ export fn main() void = {
fmt::println(":: Done checking for blobs")!;
match (ret) {
- case ok =>
+ case void =>
os::exit(0);
- case invalid =>
+ case errors::invalid =>
os::exit(1);
};
};