shithub: mc

Download patch

ref: c49aefbd03db6f3253160b34ff2ed0812615a84f
parent: 5af6cfa9fed7947928ee8eae76cf9f9b62f7b580
author: Ori Bernstein <[email protected]>
date: Tue Oct 8 17:46:54 EDT 2013

Make std.slurp return a std.error() type

--- a/libstd/slurp.myr
+++ b/libstd/slurp.myr
@@ -1,12 +1,13 @@
-use "types.use"
 use "alloc.use"
 use "die.use"
-use "sys.use"
-use "fmt.use"
+use "error.use"
 use "extremum.use"
+use "fmt.use"
+use "sys.use"
+use "types.use"
 
 pkg std =
-	const slurp : (path : byte[:] -> byte[:])
+	const slurp : (path : byte[:] -> error(byte[:], byte[:]))
 ;;
 
 const Bufinc = 4096
@@ -19,7 +20,7 @@
 
 	fd = open(path, Ordonly, 0o777)
 	if fd < 0
-		die("Could not open file")
+		-> `Failure "Could not open file"
 	;;
 
 	len = 0
@@ -33,5 +34,5 @@
 		buf = slgrow(buf, len + Bufinc)
 	;;
 :done
-	-> buf[:len]
+	-> `Success buf[:len]
 }
--- a/test/catfile.myr
+++ b/test/catfile.myr
@@ -2,10 +2,13 @@
 use std
 
 const main = {args : byte[:][:]
-	var dat
+	var r
 
-	dat = std.slurp("data/catfile")
-	std.write(1, dat)
+	r = std.slurp("data/catfile")
+	match r
+	`std.Success dat: 	std.write(1, dat);;
+	`std.Failure msg:	std.put("Failed to read file: %s\n", msg);;
+	;;
 	-> 0
 }