From b2572a94e08a4896838d331f998638a7a380f1b4 Mon Sep 17 00:00:00 2001 From: Luke Boswell Date: Tue, 21 Nov 2023 21:10:24 +1100 Subject: [PATCH 1/2] add roc_fx_dirList --- examples/dir.roc | 7 +++++++ src/src/lib.rs | 45 +++++++++++++++++++++++++-------------------- 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/examples/dir.roc b/examples/dir.roc index 6f8d292b..9f40d8b6 100644 --- a/examples/dir.roc +++ b/examples/dir.roc @@ -27,6 +27,13 @@ main = expect createChildShouldSucceed == Ok {} + # List the contents of a directory + _ <- + Path.fromStr "a" + |> Dir.list + |> Task.onErr \_ -> crash "Failed to list directory" + |> Task.await + # Try to create a directory without a parent createWithoutParentShouldFail <- Task.attempt (Dir.create (Path.fromStr "d/child")) expect diff --git a/src/src/lib.rs b/src/src/lib.rs index a859336a..1d568cb4 100644 --- a/src/src/lib.rs +++ b/src/src/lib.rs @@ -516,28 +516,33 @@ pub extern "C" fn roc_fx_sleepMillis(milliseconds: u64) { #[no_mangle] pub extern "C" fn roc_fx_dirList( - _roc_path: &RocList, + roc_path: &RocList, ) -> RocResult>, IOError> { - // match std::fs::read_dir(path_from_roc_path(roc_path)) { - // Ok(dir_entries) => { - - // let entries = dir_entries - // .filter_map(|opt_dir_entry| match opt_dir_entry { - // Ok(entry) => Some(os_str_to_roc_path(entry.path().into_os_string().as_os_str())), - // Err(_) => None - // }) - // .collect::>>(); - - // dbg!(&entries); - - // RocResult::ok(entries) - - // }, - // Err(err) => RocResult::err(toRocIOError(err)), - // } + let path = path_from_roc_path(roc_path); + + if path.is_dir() { + let dir = match std::fs::read_dir(path) { + Ok(dir) => dir, + Err(err) => return RocResult::err(toRocIOError(err)), + }; + + let mut entries = Vec::new(); + + for entry in dir { + match entry { + Ok(entry) => { + let path = entry.path(); + let str = path.as_os_str(); + entries.push(os_str_to_roc_path(str)); + } + Err(_) => {} // TODO should we ignore errors reading directory?? + } + } - // TODO implement this function - RocResult::err(IOError::Other()) + return roc_std::RocResult::ok(RocList::from_iter(entries)); + } else { + return roc_std::RocResult::err(dir_glue::IOError::NotADirectory()); + } } #[cfg(target_family = "unix")] From 9d60e91bda4a8b0f437beea17b236890ec446654 Mon Sep 17 00:00:00 2001 From: Luke Boswell Date: Thu, 23 Nov 2023 08:55:39 +1100 Subject: [PATCH 2/2] add check for expected contents --- examples/dir.roc | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/examples/dir.roc b/examples/dir.roc index 9f40d8b6..3a371ae2 100644 --- a/examples/dir.roc +++ b/examples/dir.roc @@ -28,12 +28,16 @@ main = createChildShouldSucceed == Ok {} # List the contents of a directory - _ <- + paths <- Path.fromStr "a" - |> Dir.list + |> Dir.list |> Task.onErr \_ -> crash "Failed to list directory" |> Task.await + # Check the contents of the directory + expect + (List.map paths Path.display) == ["b", "child"] + # Try to create a directory without a parent createWithoutParentShouldFail <- Task.attempt (Dir.create (Path.fromStr "d/child")) expect