mdgriffith/elm-codegen/6.0.3

Elm.Case

Generate a case expression!

Here's an example for extracting a Maybe Int

Elm.Case.maybe myMaybe
    { nothing = Elm.int 0
    , just =
        ( "value"
        , \content ->
            Elm.plus (Elm.int 5) content
        )
    }

Generates

case myMaybe of
    Nothing ->
        0


    Just value ->
        value + 5
Expression
-> { nothing : Expression
, just : ( String, Expression -> Expression )
}
-> Expression
Expression
-> { err : ( String, Expression -> Expression )
, ok : ( String, Expression -> Expression )
}
-> Expression
Elm.Case.result myResult
    { ok =
        Tuple.pair "ok" <|
            \ok ->
                Elm.string "No errors"
    , err =
        Tuple.pair "err" <|
            \err ->
                err
    }

Generates

case myResult of
    Ok ok ->
        "No errors"


    Err err ->
        err
Expression
-> { cases : List ( String, Expression )
, otherwise : Expression
}
-> Expression

Case on a Custom Type

Elm.Case.custom maybeString
    (Elm.Annotation.maybe Elm.Annotation.string)
    [ Elm.Case.branch (Elm.Arg.customType "Nothing" ())
        (\_ ->
            Elm.string "It's nothing, I swear!"
        )
    , Elm.Case.branch
        (Elm.Arg.customType "Just" identity
            |> Arg.item (Arg.var "val")
        )
        (\val ->
            Elm.append (Elm.string "Actually, it's: ") val
        )
    ]

Generates

case maybeString of
    Nothing ->
        "It's nothing, I swear!"


    Just just ->
        "Actually, it's: " ++ just
custom : Expression -> Annotation -> List Branch -> Expression
type alias Branch =
Branch
branch : Arg val -> (val -> Expression) -> Branch