testing result with os.file open
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 26 Feb 2023 04:13:36 +0000 (22:13 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 26 Feb 2023 04:13:36 +0000 (22:13 -0600)
core/container/optional.onyx
core/container/result.onyx
core/os/file.onyx
core/std.onyx

index 33aafefb2f0a0ca64aeae2a7509d7ba28281d3e6..86f54d103a0978790947ae5112c27550e2292518 100644 (file)
@@ -83,6 +83,16 @@ Optional :: struct (Value_Type: type_expr) {
         return generate();
     }
 
+    #doc """
+        Returns the value inside the optional, if there is one.
+        If not, an assertion is thrown and the context's assert
+        handler must take care of it.
+    """
+    unwrap :: (o: Optional) -> o.Value_Type {
+        if o.has_value do return o.value;
+        assert(false, "Unwrapping empty Optional.");
+    }
+
     hash :: (o: Optional($T/core.hash.Hashable)) -> u32 {
         if !o.has_value do return 0;
         return core.hash.to_u32(o.value);
index 2a14f62e9340f28b4c4b6f50aed52396b9f08fe1..147e4bfe10548cc1e2f9164fbe0393597c2f28fa 100644 (file)
@@ -54,6 +54,15 @@ Result_Data :: struct (T: type_expr, E: type_expr) {
         return r.__data.value;
     }
 
+    expect :: (r: #Self, msg: str) -> r.Ok_Type {
+        if r.status == .Err {
+            assert(false, msg);
+            return .{};
+        }
+
+        return r.__data.value;
+    }
+
     transform :: (r: #Self, f: (r.Ok_Type) -> $R) => {
         if r.status == .Err do return r;
         return .{ .Ok, .{ value = f(r.__data.value) } };
@@ -76,6 +85,13 @@ Result_Data :: struct (T: type_expr, E: type_expr) {
         return #from_enclosing .{ .Err, .{ error = res.__data.error } };
     }
 
+    or_return :: macro (r: Result($T, $E), v: $V) -> T {
+        res := r;
+        if res.status == .Ok do return res.__data.value;
+        
+        return #from_enclosing v;
+    }
+
     format :: (o: ^conv.Format_Output, format: ^conv.Format, res: ^Result($T, $E)) {
         if res.status == .Ok {
             conv.format(o, "{}({\"})", res.status, res.__data.value);
index 53e742156bc1b88e5a5868403b159e02a8874365..c67642702a1fc7509cfa63a0bc3e6c792c199a58 100644 (file)
@@ -64,18 +64,18 @@ get_contents_from_file :: (file: ^File) -> str {
     return data[0 .. size];
 }
 
-open :: (path: str, mode := OpenMode.Read) -> (os.FileError, File) {
+open :: (path: str, mode := OpenMode.Read) -> Result(File, os.FileError) {
     file := File.{
         stream = .{ vtable = null },
         data   = .{},
     };
 
     file_data, error := fs.__file_open(path, mode);
-    if error != .None do return error, file;
+    if error != .None do return .{ .Err, .{error=error} };
 
     file.data = file_data;
     file.vtable = ^fs.__file_stream_vtable;
-    return .None, file;
+    return .{ .Ok, .{value=file} };
 }
 
 close :: (file: ^File) {
@@ -87,8 +87,7 @@ get_contents :: #match {
     get_contents_from_file,
 
     (path: str) -> str {
-        error, file := open(path, .Read);
-        if error != .None do return .{ null, 0 };
+        file := open(path, .Read)->or_return(null_str);
         defer close(^file);
 
         return get_contents(^file);
@@ -103,8 +102,8 @@ with_file :: (path: str, mode := OpenMode.Read) -> Iterator(^File) {
     }
 
     c := new(Context);
-    if err, file_stream := open(path, mode); err == .None {
-        c.file_stream = file_stream;
+    if file_stream := open(path, mode); file_stream {
+        c.file_stream = file_stream->unwrap();
         c.valid_file_stream = true;
     }
 
@@ -156,9 +155,7 @@ file_logger_open :: (filename: str, allocator := context.allocator) -> ^File_Log
 
     file_logger.file = new(File, allocator);
 
-    error := os.FileError.None;
-    error, *file_logger.file = open(filename, mode=.Append);
-    assert(error == .None, "Unable to open file for logging.");
+    *file_logger.file = open(filename, mode=.Append)->expect("Unable to open file for logging.");
 
     return file_logger;
 }
index fd0eb0c830ef92c365031cdf0d9242674f505664..2df668c5243fb63db16d2581ed8358ec50356d7c 100644 (file)
@@ -14,6 +14,7 @@ package core
 #load "./container/heap"
 #load "./container/pair"
 #load "./container/optional"
+#load "./container/result"
 
 #load "./conv/conv"
 #load "./conv/format"