Add Dir and Env to CLI platform

This commit is contained in:
Richard Feldman 2022-09-12 16:12:36 -04:00
parent 4ccd726141
commit 21b74f6dbb
No known key found for this signature in database
GPG key ID: F1F21AA5B1D9E43B
10 changed files with 208 additions and 19 deletions

View file

@ -238,6 +238,50 @@ pub extern "C" fn roc_fx_fileDelete(roc_path: &RocList<u8>) -> RocResult<(), Rea
}
}
#[no_mangle]
pub extern "C" fn roc_fx_cwd() -> RocList<u8> {
// TODO instead, call getcwd on UNIX and GetCurrentDirectory on Windows
match std::env::current_dir() {
Ok(path_buf) => os_str_to_roc_path(path_buf.into_os_string().as_os_str()),
Err(_) => {
// Default to empty path
RocList::empty()
}
}
}
#[no_mangle]
pub extern "C" fn roc_fx_dirList(
// TODO: this RocResult should use Dir.WriteErr - but right now it's File.WriteErr
// because glue doesn't have Dir.WriteErr yet.
roc_path: &RocList<u8>,
) -> RocResult<RocList<RocList<u8>>, WriteErr> {
println!("Dir.list...");
match std::fs::read_dir(path_from_roc_path(roc_path)) {
Ok(dir_entries) => RocResult::ok(
dir_entries
.map(|opt_dir_entry| match opt_dir_entry {
Ok(entry) => os_str_to_roc_path(entry.path().into_os_string().as_os_str()),
Err(_) => {
todo!("handle dir_entry path didn't resolve")
}
})
.collect::<RocList<RocList<u8>>>(),
),
Err(_) => {
todo!("handle Dir.list error");
}
}
}
#[cfg(target_family = "unix")]
/// TODO convert from EncodeWide to RocPath on Windows
fn os_str_to_roc_path(os_str: &OsStr) -> RocList<u8> {
use std::os::unix::ffi::OsStrExt;
RocList::from(os_str.as_bytes())
}
#[no_mangle]
pub extern "C" fn roc_fx_sendRequest(roc_request: &glue::Request) -> glue::Response {
let mut builder = reqwest::blocking::ClientBuilder::new();