Skip to content

Commit

Permalink
cleanup Dir example
Browse files Browse the repository at this point in the history
  • Loading branch information
lukewilliamboswell committed Nov 21, 2023
1 parent b2572a9 commit 4c8651e
Showing 1 changed file with 31 additions and 31 deletions.
62 changes: 31 additions & 31 deletions examples/dir.roc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ app "dir"
packages { pf: "../src/main.roc" }
imports [
pf.Stdout,
pf.Stderr,
pf.Dir.{ MakeErr },
pf.Path,
pf.Task.{ Task },
Expand All @@ -13,54 +12,55 @@ main : Task {} I32
main =

# Create a directory
createShouldSucceed <- Task.attempt (Dir.create (Path.fromStr "e"))
expect
createShouldSucceed == Ok {}
{} <-
Path.fromStr "e"
|> Dir.create
|> Task.onErr \_ -> crash "Failed to create directory"
|> Task.await

# Create a directory and its parents
createAllShouldSucceed <- Task.attempt (Dir.createAll (Path.fromStr "a/b/c/child"))
expect
createAllShouldSucceed == Ok {}
{} <-
Path.fromStr "a/b/c/child"
|> Dir.createAll
|> Task.onErr \_ -> crash "Failed to create directory and its parents"
|> Task.await

# Create a child directory
createChildShouldSucceed <- Task.attempt (Dir.create (Path.fromStr "a/child"))
expect
createChildShouldSucceed == Ok {}
{} <-
Path.fromStr "a/child"
|> Dir.create
|> Task.onErr \_ -> crash "Failed to create child directory a/child"
|> Task.await

# List the contents of a directory
_ <-
_ <-
Path.fromStr "a"
|> Dir.list
|> 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
createWithoutParentShouldFail == Err NotFound
{} <-
Path.fromStr "d/child"
|> Dir.create
|> Task.attempt \result ->
when result is
Ok {} -> crash "Should return error creating directory without a parent"
Err _ -> Task.ok {}
|> Task.await

# Delete an empty directory
_ <-
Task.attempt (Dir.deleteEmpty (Path.fromStr "e")) \removeChild ->
when removeChild is
Ok _ -> Task.ok {}
Err err ->
dbg
err

Stderr.line "Failed to delete an empty directory"
Path.fromStr "e"
|> Dir.deleteEmpty
|> Task.onErr \_ -> crash "Failed to delete an empty directory"
|> Task.await

# Delete all directories recursively
_ <-
Task.attempt (Dir.deleteAll (Path.fromStr "a")) \removeChild ->
when removeChild is
Ok _ -> Task.ok {}
Err err ->
dbg
err

Stderr.line "Failed to delete directory recursively"
Path.fromStr "a"
|> Dir.deleteAll
|> Task.onErr \_ -> crash "Failed to delete directory recursively"
|> Task.await

Stdout.line "Success!"

0 comments on commit 4c8651e

Please sign in to comment.