Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

.DS_Store
31 changes: 18 additions & 13 deletions package/Path.roc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ Path := [
Unix (List U8),
Windows (List U16),
]
implements [Eq]
implements [Eq, Inspect { toInspector: pathInspector }]

pathInspector : Path -> Inspector f where f implements InspectFormatter
pathInspector = \path -> Inspect.str (display path)

unix : Str -> Path
unix = \str -> @Path (Unix (Str.toUtf8 str))
Expand Down Expand Up @@ -145,13 +148,13 @@ filename = \@Path path ->
Ok lastSepIndex -> Ok (@Path (Unix (afterSep bytes lastSepIndex)))
Err NotFound -> Ok (@Path path) # No separators? Entire path is the filename!

expect Path.filename (Path.unix "foo/bar.txt") == Ok (Path.unix "bar.txt")
expect Path.filename (Path.unix "foo/bar") == Ok (Path.unix "bar")
expect Path.filename (Path.unix "foo/bar/") == Err IsDirPath
expect Path.filename (Path.windows "foo\\bar\\") == Err IsDirPath
expect Path.filename (Path.unix "foo/bar..") == Err EndsInDots
expect Path.filename (Path.unix "foo") == Ok (Path.unix "foo")
expect Path.filename (Path.unix "") == Ok (Path.unix "")
expect filename (unix "foo/bar.txt") == Ok (unix "bar.txt")
expect filename (unix "foo/bar") == Ok (unix "bar")
expect filename (unix "foo/bar/") == Err IsDirPath
expect filename (windows "foo\\bar\\") == Err IsDirPath
expect filename (unix "foo/bar..") == Err EndsInDots
expect filename (unix "foo") == Ok (unix "foo")
expect filename (unix "") == Ok (unix "")

afterSep : List (Num a), U64 -> List (Num a)
afterSep = \list, lastSepIndex ->
Expand Down Expand Up @@ -406,15 +409,17 @@ normalizeUnix = \answer, remaining ->
|> normalizeUnix (remaining |> List.dropFirst 1)

## Returns a string representation of the path, replacing anything that can't be
## represented in a string with the Unicode Replacement Character.
## represented in a string with the Unicode Replacement Character (U+FFFD) �.
##
## To get back an `Err` instead of silently replacing problems with the Unicode Replacement Character,
## use [toStr] instead.
display : Path -> Str
# display = \@Path path ->
# when path is
# Unix bytes -> crash "TODO fromUtf8Lossy"
# Windows u16s -> crash "TODO fromUtf16Lossy"
display = \@Path path ->
when path is
Unix u8s ->
# TODO replace with Str.fromUtf8Lossy when it exists
Str.fromUtf8 u8s |> Result.withDefault "INVALID UTF-8"
Windows _u16s -> crash "TODO use fromUtf16Lossy"

## Like [display], but renders the path Windows-style (with backslashes for directory separators)
## even if it was originally created as a UNIX path (e.g. using [Path.unix]).
Expand Down