Fix File.readBytes

This commit is contained in:
Richard Feldman 2022-09-12 10:03:51 -04:00
parent 81d11e4edd
commit 4ccd726141
No known key found for this signature in database
GPG key ID: F1F21AA5B1D9E43B
2 changed files with 23 additions and 8 deletions

View file

@ -85,10 +85,7 @@ delete = \path ->
## To read and decode data from a file, you can use `File.read` instead.
readBytes : Path -> Task (List U8) [FileReadErr Path ReadErr]* [Read [File]*]*
readBytes = \path ->
InternalPath.toBytes path
|> Effect.fileReadBytes
|> InternalTask.fromEffect
|> Task.mapFail \err -> FileReadErr path err
toReadTask path \bytes -> Effect.fileReadBytes bytes
## Read a [Str] from a file containing [UTF-8](https://en.wikipedia.org/wiki/UTF-8)-encoded text.
##
@ -140,3 +137,10 @@ toWriteTask = \path, toEffect ->
|> toEffect
|> InternalTask.fromEffect
|> Task.mapFail \err -> FileWriteErr path err
toReadTask : Path, (List U8 -> Effect (Result ok err)) -> Task ok [FileReadErr Path err]* [Read [File]*]*
toReadTask = \path, toEffect ->
InternalPath.toBytes path
|> toEffect
|> InternalTask.fromEffect
|> Task.mapFail \err -> FileReadErr path err

View file

@ -210,11 +210,22 @@ pub fn os_str_from_list(bytes: &RocList<u8>) -> &OsStr {
}
#[no_mangle]
pub extern "C" fn roc_fx_fileReadBytes(path: &RocList<u8>) -> RocResult<RocList<u8>, ReadErr> {
let path = path_from_roc_path(path);
println!("TODO read bytes from {:?}", path);
pub extern "C" fn roc_fx_fileReadBytes(roc_path: &RocList<u8>) -> RocResult<RocList<u8>, ReadErr> {
use std::io::Read;
RocResult::ok(RocList::empty())
let mut bytes = Vec::new();
match File::open(path_from_roc_path(roc_path)) {
Ok(mut file) => match file.read_to_end(&mut bytes) {
Ok(_bytes_read) => RocResult::ok(RocList::from(bytes.as_slice())),
Err(_) => {
todo!("Report a file write error");
}
},
Err(_) => {
todo!("Report a file open error");
}
}
}
#[no_mangle]