mirror of
https://github.com/roc-lang/roc.git
synced 2024-11-13 09:49:11 +03:00
Merge branch 'trunk' into parse-expr-underscore
This commit is contained in:
commit
2115994f4b
@ -3,13 +3,21 @@ app "task-example"
|
||||
imports [ base.Task.{ Task }, base.File, base.Path ]
|
||||
provides [ main ] to base
|
||||
|
||||
main : Task.Task {} (File.FileReadErr [BadUtf8])
|
||||
main : Task.Task {} []
|
||||
main =
|
||||
when Path.fromStr "Cargo.toml" is
|
||||
when Path.fromStr "vendor" is
|
||||
Ok path ->
|
||||
{} <- Task.await (Task.putLine "Our Cargo.toml:")
|
||||
|
||||
line <- Task.await (File.readUtf8 path)
|
||||
result <- Task.attempt (File.readUtf8 path)
|
||||
|
||||
# pathStr = Path.toStr path
|
||||
|
||||
when result is
|
||||
Ok contents -> Task.putLine contents
|
||||
Err (FileNotFound _) -> Task.putLine "file not found"
|
||||
Err (BadUtf8 _ _) -> Task.putLine "bad utf8"
|
||||
Err (FileWasDir _) -> Task.putLine "file was dir"
|
||||
Err _ -> Task.putLine "Error retrieving file - error"
|
||||
|
||||
Task.putLine line
|
||||
_ -> Task.putLine "invalid path"
|
||||
|
@ -60,13 +60,16 @@ FileWriteErr a :
|
||||
# Effect.readBytes (Path.toStr path)
|
||||
|
||||
## Read a file's bytes and interpret them as UTF-8 encoded text.
|
||||
readUtf8 : Path -> Task.Task Str (FileReadErr [ BadUtf8 ]*)
|
||||
readUtf8 : Path -> Task.Task Str (FileReadErr [ BadUtf8 Str.Utf8ByteProblem Nat ]*)
|
||||
readUtf8 = \path ->
|
||||
Effect.map (Effect.readAllUtf8 (Path.toStr path)) \answer ->
|
||||
# errno values - see
|
||||
# https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/errno.h.html
|
||||
when answer.errno is
|
||||
0 -> Ok answer.bytes # TODO use Str.fromUtf8 to validate a byte list as UTF-8 and return (Err BadUtf8) if validation fails
|
||||
0 ->
|
||||
when Str.fromUtf8 answer.bytes is
|
||||
Ok str -> Ok str
|
||||
Err (BadUtf8 problem index) -> Err (BadUtf8 problem index)
|
||||
1 -> Err (PermissionDenied path)
|
||||
2 -> Err (FileNotFound path)
|
||||
19 -> Err (FileWasDir path)
|
||||
|
@ -1,15 +1,15 @@
|
||||
platform folkertdev/foo
|
||||
requires { main : Task {} * }
|
||||
requires { main : Task {} [] }
|
||||
exposes []
|
||||
packages {}
|
||||
imports [ Task, File ]
|
||||
imports [ Task ]
|
||||
provides [ mainForHost ]
|
||||
effects fx.Effect
|
||||
{
|
||||
# TODO change sig to Effect { errno : I32, bytes : List U8 }
|
||||
readAllUtf8 : Str -> Effect { errno : I64, bytes : Str },
|
||||
# TODO change errno to I32
|
||||
readAllUtf8 : Str -> Effect { errno : I64, bytes : List U8 },
|
||||
putLine : Str -> Effect {}
|
||||
}
|
||||
|
||||
mainForHost : Task.Task {} (File.FileReadErr [BadUtf8]) as Fx
|
||||
mainForHost : Task.Task {} [] as Fx
|
||||
mainForHost = main
|
||||
|
@ -1,5 +1,5 @@
|
||||
interface Task
|
||||
exposes [ Task, succeed, fail, await, map, putLine ]
|
||||
exposes [ Task, succeed, fail, await, map, putLine, attempt ]
|
||||
imports [ fx.Effect ]
|
||||
|
||||
|
||||
@ -23,6 +23,13 @@ await = \effect, transform ->
|
||||
Ok a -> transform a
|
||||
Err err -> Task.fail err
|
||||
|
||||
attempt : Task a b, (Result a b -> Task c d) -> Task c d
|
||||
attempt = \effect, transform ->
|
||||
Effect.after effect \result ->
|
||||
when result is
|
||||
Ok ok -> transform (Ok ok)
|
||||
Err err -> transform (Err err)
|
||||
|
||||
map : Task a err, (a -> b) -> Task b err
|
||||
map = \effect, transform ->
|
||||
Effect.after effect \result ->
|
||||
|
@ -108,12 +108,17 @@ pub const ReadResult = extern struct {
|
||||
|
||||
pub export fn roc_fx_readAllUtf8(rocPath: RocStr) callconv(.C) ReadResult {
|
||||
var dir = std.fs.cwd();
|
||||
var content = dir.readFileAlloc(testing.allocator, rocPath.asSlice(), 1024) catch unreachable;
|
||||
|
||||
var content = dir.readFileAlloc(testing.allocator, rocPath.asSlice(), 1024) catch |e| switch (e) {
|
||||
error.FileNotFound => return .{ .bytes = RocStr.empty(), .errno = 2 },
|
||||
error.IsDir => return .{ .bytes = RocStr.empty(), .errno = 19 },
|
||||
else => return .{ .bytes = RocStr.empty(), .errno = 9999 },
|
||||
};
|
||||
|
||||
var str_ptr = @ptrCast([*]u8, content);
|
||||
var roc_str3 = RocStr.init(testing.allocator, str_ptr, content.len);
|
||||
|
||||
return ReadResult{ .bytes = roc_str3, .errno = 0 };
|
||||
return .{ .bytes = roc_str3, .errno = 0 };
|
||||
}
|
||||
|
||||
pub fn roc_fx_readAllUtf8_that_does_not_work(rocPath: *RocStr) ReadResult {
|
||||
|
Loading…
Reference in New Issue
Block a user