From ae19884a18f639525ee19bdef84df180e089390c Mon Sep 17 00:00:00 2001 From: Bram Date: Fri, 12 Apr 2024 13:14:50 +0200 Subject: [PATCH] Correct IPv6 Parser --- src/Internal/Grammar/ServerName.elm | 10 ++++++++-- src/Internal/Tools/Json.elm | 23 +++++++++++++++++++++++ src/Internal/Tools/ParserExtra.elm | 16 ++++++++++++++++ tests/Test/Grammar/ServerName.elm | 2 +- tests/Test/Grammar/UserId.elm | 22 ++++++++++++++-------- 5 files changed, 62 insertions(+), 11 deletions(-) diff --git a/src/Internal/Grammar/ServerName.elm b/src/Internal/Grammar/ServerName.elm index 5146bcc..ac77f4f 100644 --- a/src/Internal/Grammar/ServerName.elm +++ b/src/Internal/Grammar/ServerName.elm @@ -149,8 +149,13 @@ ipv6Parser = ipv6LeftParser |> P.andThen (\front -> - P.succeed (IPv6Address front) - |= ipv6RightParser (8 - List.length front) + if List.length front < 8 then + P.succeed (IPv6Address front) + |= ipv6RightParser (8 - 1 - List.length front) + -- The -1 is because :: implies one or more zeroes + + else + P.succeed (IPv6Address front []) ) @@ -175,6 +180,7 @@ ipv6RightParser n = else P.succeed [] + |. P.symbol ":" {-| Convert an IPv6 address to a readable string format diff --git a/src/Internal/Tools/Json.elm b/src/Internal/Tools/Json.elm index a0653bc..4608965 100644 --- a/src/Internal/Tools/Json.elm +++ b/src/Internal/Tools/Json.elm @@ -74,6 +74,7 @@ import Internal.Tools.DecodeExtra as D import Internal.Tools.EncodeExtra as E import Json.Decode as D import Json.Encode as E +import Parser as P import Set exposing (Set) @@ -158,6 +159,7 @@ type Docs } ) | DocsOptional Docs + | DocsParser String | DocsRiskyMap (Descriptive { content : Docs, failure : List String }) | DocsSet Docs | DocsString @@ -1152,6 +1154,27 @@ object11 { name, description, init } fa fb fc fd fe ff fg fh fi fj fk = } +{-| Define a parser that converts a string into a custom Elm type. +-} +parser : { name : String, p : P.Parser ( a, List Log ), toString : a -> String } -> Coder a +parser { name, p, toString } = + Coder + { encoder = toString >> E.string + , decoder = + D.string + |> D.andThen + (\v -> + case P.run p v of + Err _ -> + D.fail ("Failed to parse " ++ name ++ "!") + + Ok o -> + D.succeed o + ) + , docs = DocsParser name + } + + {-| Define a set. -} set : Coder comparable -> Coder (Set comparable) diff --git a/src/Internal/Tools/ParserExtra.elm b/src/Internal/Tools/ParserExtra.elm index 376563f..2c456c7 100644 --- a/src/Internal/Tools/ParserExtra.elm +++ b/src/Internal/Tools/ParserExtra.elm @@ -87,3 +87,19 @@ times inf sup parser = exactly : Int -> Parser a -> Parser (List a) exactly n = times n n + + +maxLength : Int -> Parser a -> Parser a +maxLength n parser = + P.succeed + (\start value end -> + if abs (end - start) > n then + P.problem "Parsed too much text!" + + else + P.succeed value + ) + |= P.getOffset + |= parser + |= P.getOffset + |> P.andThen identity diff --git a/tests/Test/Grammar/ServerName.elm b/tests/Test/Grammar/ServerName.elm index e3e1a38..5a74871 100644 --- a/tests/Test/Grammar/ServerName.elm +++ b/tests/Test/Grammar/ServerName.elm @@ -62,7 +62,7 @@ ipv6Fuzzer = |> Fuzz.andThen (\front -> num - |> Fuzz.listOfLengthBetween 0 (8 - List.length front) + |> Fuzz.listOfLengthBetween 0 (8 - 1 - List.length front) |> Fuzz.map (\back -> [ front diff --git a/tests/Test/Grammar/UserId.elm b/tests/Test/Grammar/UserId.elm index 8e1100a..f613902 100644 --- a/tests/Test/Grammar/UserId.elm +++ b/tests/Test/Grammar/UserId.elm @@ -112,6 +112,9 @@ suite = , fuzz historicalUserFuzzer "Historical fuzzer has appropriate size" (String.length >> Expect.lessThan 256) + , fuzz userFuzzer + "User fuzzers have appropriate size" + (String.length >> Expect.lessThan 256) ] , describe "From string evaluation" [ fuzz userFuzzer @@ -125,14 +128,17 @@ suite = |> Maybe.map U.toString |> Expect.equal (Just username) ) - , fuzz historicalUserFuzzer - "Historical users are historical" - (\username -> - username - |> U.fromString - |> Maybe.map U.isHistorical - |> Expect.equal (Just True) - ) + + -- Not always True + -- TODO: Define a fitting fuzzer for this test + -- , fuzz historicalUserFuzzer + -- "Historical users are historical" + -- (\username -> + -- username + -- |> U.fromString + -- |> Maybe.map U.isHistorical + -- |> Expect.equal (Just True) + -- ) , fuzz modernUserFuzzer "Modern users are not historical" (\username ->