Skip to content

Commit 2ac1bf1

Browse files
authored
Merge pull request #27 from jamesrweb/EISO8601-IN25
EISO8601-IN25: Unhelpful TODO output from decoder failure
2 parents 795f191 + 4688e55 commit 2ac1bf1

File tree

3 files changed

+78
-6
lines changed

3 files changed

+78
-6
lines changed

src/DeadEnds.elm

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
module DeadEnds exposing (..)
2+
3+
import Parser exposing (..)
4+
5+
6+
7+
{- This implementation is taken from elm/parser PR #16 by @bburdette and provides clearer dead ends output for the end user -}
8+
9+
10+
deadEndsToString : List DeadEnd -> String
11+
deadEndsToString deadEnds =
12+
String.concat (List.intersperse "; " (List.map deadEndToString deadEnds))
13+
14+
15+
deadEndToString : DeadEnd -> String
16+
deadEndToString deadend =
17+
problemToString deadend.problem ++ " at row " ++ String.fromInt deadend.row ++ ", col " ++ String.fromInt deadend.col
18+
19+
20+
problemToString : Problem -> String
21+
problemToString p =
22+
case p of
23+
Expecting s ->
24+
"expecting '" ++ s ++ "'"
25+
26+
ExpectingInt ->
27+
"expecting int"
28+
29+
ExpectingHex ->
30+
"expecting hex"
31+
32+
ExpectingOctal ->
33+
"expecting octal"
34+
35+
ExpectingBinary ->
36+
"expecting binary"
37+
38+
ExpectingFloat ->
39+
"expecting float"
40+
41+
ExpectingNumber ->
42+
"expecting number"
43+
44+
ExpectingVariable ->
45+
"expecting variable"
46+
47+
ExpectingSymbol s ->
48+
"expecting symbol '" ++ s ++ "'"
49+
50+
ExpectingKeyword s ->
51+
"expecting keyword '" ++ s ++ "'"
52+
53+
ExpectingEnd ->
54+
"expecting end"
55+
56+
UnexpectedChar ->
57+
"unexpected char"
58+
59+
Problem s ->
60+
"problem " ++ s
61+
62+
BadRepeat ->
63+
"bad repeat"

src/Iso8601.elm

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ module Iso8601 exposing (fromTime, toTime, decoder, encode)
66
77
-}
88

9+
import DeadEnds exposing (deadEndsToString)
910
import Json.Decode as Decode exposing (Decoder)
1011
import Json.Encode as Encode
11-
import Parser exposing ((|.), (|=), Parser, andThen, end, int, map, oneOf, succeed, symbol)
12+
import Parser exposing ((|.), (|=), Parser, andThen, end, map, oneOf, succeed, symbol)
1213
import Time exposing (Month(..), utc)
1314

1415

@@ -21,7 +22,7 @@ decoder =
2122
(\str ->
2223
case toTime str of
2324
Err deadEnds ->
24-
Decode.fail <| Parser.deadEndsToString deadEnds
25+
Decode.fail <| deadEndsToString deadEnds
2526

2627
Ok time ->
2728
Decode.succeed time
@@ -366,7 +367,7 @@ utcOffsetInMinutes =
366367
-- No "Z" is valid
367368
, succeed 0
368369
|. end
369-
]
370+
]
370371

371372

372373
{-| Parse fractions of a second, and convert to milliseconds

tests/Example.elm

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
module Example exposing (knownValues, reflexive)
22

3-
import Expect exposing (Expectation)
4-
import Fuzz exposing (Fuzzer, float, int, list, string)
3+
import Expect
4+
import Fuzz
55
import Iso8601
66
import Test exposing (..)
77
import Time
8+
import Json.Decode exposing (decodeString, errorToString)
89

910

1011
knownValues : Test
@@ -94,12 +95,19 @@ knownValues =
9495
\_ ->
9596
Iso8601.toTime "2012-11-12T00:00:00+0130546"
9697
|> Expect.err
98+
, test "decoder returns clearer error for dead ends" <|
99+
\_ ->
100+
case decodeString Iso8601.decoder "2010-09-31T14:29:25.01235Z" of
101+
Err error ->
102+
Expect.notEqual (errorToString error) "TODO deadEndsToString"
103+
Ok _ ->
104+
Expect.fail "Should fail on dead ends"
97105
]
98106

99107

100108
reflexive : Test
101109
reflexive =
102-
fuzz int "(fromTime >> toTime) is a no-op" <|
110+
fuzz Fuzz.int "(fromTime >> toTime) is a no-op" <|
103111
\num ->
104112
let
105113
time =

0 commit comments

Comments
 (0)