diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9bea433 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ + +.DS_Store diff --git a/package/Path.roc b/package/Path.roc index dc737d5..3ae019d 100644 --- a/package/Path.roc +++ b/package/Path.roc @@ -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)) @@ -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 -> @@ -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]).