Skip to content

Commit e5fb0ac

Browse files
authored
Add effect to read file (#27)
1 parent 8ab26e2 commit e5fb0ac

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

host/roc/roc.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"io"
2424
"iter"
2525
"net/http"
26+
"os"
2627
"sync"
2728
"time"
2829
"unsafe"
@@ -242,6 +243,16 @@ func roc_fx_get(url *RocStr) C.struct_ResultBytesString {
242243
return getResultSuccess(body)
243244
}
244245

246+
//export roc_fx_file_read_bytes
247+
func roc_fx_file_read_bytes(path *RocStr) C.struct_ResultBytesString {
248+
content, err := os.ReadFile(path.String())
249+
if err != nil {
250+
return getResultError(err)
251+
}
252+
253+
return getResultSuccess(content)
254+
}
255+
245256
func getResultError(err error) C.struct_ResultBytesString {
246257
errStr := NewRocStr(err.Error())
247258
payload := *(*[24]byte)(unsafe.Pointer(&errStr))

platform/Host.roc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ hosted Host
44
stdout_line!,
55
posix_time!,
66
get!,
7+
file_read_bytes!,
78
]
89
imports []
910

1011
save_event! : List U8 => {}
1112
stdout_line! : Str => {}
1213
posix_time! : {} => U128
1314
get! : Str => Result (List U8) Str # TODO: Improve this
15+
file_read_bytes! : Str => Result (List U8) Str

0 commit comments

Comments
 (0)