mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-30 23:31:12 +00:00
Fix File.readBytes
This commit is contained in:
parent
81d11e4edd
commit
4ccd726141
2 changed files with 23 additions and 8 deletions
|
@ -85,10 +85,7 @@ delete = \path ->
|
||||||
## To read and decode data from a file, you can use `File.read` instead.
|
## 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 -> Task (List U8) [FileReadErr Path ReadErr]* [Read [File]*]*
|
||||||
readBytes = \path ->
|
readBytes = \path ->
|
||||||
InternalPath.toBytes path
|
toReadTask path \bytes -> Effect.fileReadBytes bytes
|
||||||
|> Effect.fileReadBytes
|
|
||||||
|> InternalTask.fromEffect
|
|
||||||
|> Task.mapFail \err -> FileReadErr path err
|
|
||||||
|
|
||||||
## Read a [Str] from a file containing [UTF-8](https://en.wikipedia.org/wiki/UTF-8)-encoded text.
|
## 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
|
|> toEffect
|
||||||
|> InternalTask.fromEffect
|
|> InternalTask.fromEffect
|
||||||
|> Task.mapFail \err -> FileWriteErr path err
|
|> 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
|
||||||
|
|
|
@ -210,11 +210,22 @@ pub fn os_str_from_list(bytes: &RocList<u8>) -> &OsStr {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn roc_fx_fileReadBytes(path: &RocList<u8>) -> RocResult<RocList<u8>, ReadErr> {
|
pub extern "C" fn roc_fx_fileReadBytes(roc_path: &RocList<u8>) -> RocResult<RocList<u8>, ReadErr> {
|
||||||
let path = path_from_roc_path(path);
|
use std::io::Read;
|
||||||
println!("TODO read bytes from {:?}", path);
|
|
||||||
|
|
||||||
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]
|
#[no_mangle]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue