How to mapProblems for elm/parser?

Does anyone know a way to write a mapProblems function for elm/parser that works like mapError does for Result? The closest thing I can figure out (see below) seems complicated and probably expensive. My read after doing some digging is that the parser library doesn’t expose all the stuff necessary to write something more concise (it exposes the Parser type but not as Parser(..), and I can’t just copy the whole Parser library into my app and edit it there because it has Kernel code in it). But maybe I’m wrong? Any ideas?

mapProblem : (problem1 -> problem2) -> problem1 -> Parser context problem1 data -> Parser context problem2 data
mapProblem problemFunc defaultProblem parser =
    withOffsetAndSource <|
        \( offset, source ) ->
            case parseStartingAt offset source parser of
                Ok okResult ->
                    succeed okResult

                Err deadEnds ->
                        |> List.head
                        |> .problem
                        |> Maybe.withDefault defaultProblem
                        |> problemFunc
                        |> problem

parseStartingAt : Int -> String -> Parser context problem resultType -> Result (List (DeadEnd context problem)) resultType
parseStartingAt offset source parser =
        |> String.dropLeft offset
        |> run parser

withOffsetAndSource : (( Int, String ) -> Parser context problem data) -> Parser context problem data
withOffsetAndSource decoder =
    succeed Tuple.pair
        |= getOffset
        |= getSource
        |> andThen decoder

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.