mdgriffith / elm-codegen / 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

maybe : Elm.Expression -> { nothing : Elm.Expression, just : ( String, Elm.Expression -> Elm.Expression ) } -> Elm.Expression

result : Elm.Expression -> { err : ( String, Elm.Expression -> Elm.Expression ), ok : ( String, Elm.Expression -> Elm.Expression ) } -> Elm.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

list : Elm.Expression -> { empty : Elm.Expression, nonEmpty : Elm.Expression -> Elm.Expression -> Elm.Expression } -> Elm.Expression

Let's unpack the first value from a list.

Elm.Case.list myList
    { empty = Elm.int 0
    , nonEmpty =
        \top remaining ->
            Elm.plus (Elm.int 5) top
    }

Generates

case myList of
    [] ->
        0

    top :: remaining ->
        top + 5

Note if you want more control over unpacking lists, check out branchList

string : Elm.Expression -> { cases : List ( String, Elm.Expression ), otherwise : Elm.Expression } -> Elm.Expression

tuple : Elm.Expression -> String -> String -> (Elm.Expression -> Elm.Expression -> Elm.Expression) -> Elm.Expression

Elm.Case.tuple myTuple
    "first"
    "second"
    (\one two ->
        Elm.plus (Elm.int 5) two
    )

Generates

case myTuple of
    ( first, second ) ->
        5 + second

triple : Elm.Expression -> String -> String -> String -> (Elm.Expression -> Elm.Expression -> Elm.Expression -> Elm.Expression) -> Elm.Expression

Elm.Case.triple myTriple
    "one"
    "two"
    "three"
    (\one two three ->
        Elm.plus (Elm.int 5) two
    )

Generates

case myTriple of
    ( one, two, three ) ->
        5 + two

Case on a Custom Type

Elm.Case.custom maybeString
    (Elm.Annotation.maybe Elm.Annotation.string)
    [ Elm.Case.branch0 "Nothing"
        (Elm.string "It's nothing, I swear!")
    , Elm.Case.branch1 "Just" ( "val", Elm.Annotation.string ) <|
        \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 : Elm.Expression -> Elm.Annotation.Annotation -> List Branch -> Elm.Expression


type alias Branch =
Internal.Branch.Branch

otherwise : (Elm.Expression -> Elm.Expression) -> Branch

A catchall branch in case you want the case to be nonexhaustive.

branch0 : String -> Elm.Expression -> Branch

branch1 : String -> ( String, Elm.Annotation.Annotation ) -> (Elm.Expression -> Elm.Expression) -> Branch

branch2 : String -> ( String, Elm.Annotation.Annotation ) -> ( String, Elm.Annotation.Annotation ) -> (Elm.Expression -> Elm.Expression -> Elm.Expression) -> Branch

branch3 : String -> ( String, Elm.Annotation.Annotation ) -> ( String, Elm.Annotation.Annotation ) -> ( String, Elm.Annotation.Annotation ) -> (Elm.Expression -> Elm.Expression -> Elm.Expression -> Elm.Expression) -> Branch

branch4 : String -> ( String, Elm.Annotation.Annotation ) -> ( String, Elm.Annotation.Annotation ) -> ( String, Elm.Annotation.Annotation ) -> ( String, Elm.Annotation.Annotation ) -> (Elm.Expression -> Elm.Expression -> Elm.Expression -> Elm.Expression -> Elm.Expression) -> Branch

branch5 : String -> ( String, Elm.Annotation.Annotation ) -> ( String, Elm.Annotation.Annotation ) -> ( String, Elm.Annotation.Annotation ) -> ( String, Elm.Annotation.Annotation ) -> ( String, Elm.Annotation.Annotation ) -> (Elm.Expression -> Elm.Expression -> Elm.Expression -> Elm.Expression -> Elm.Expression -> Elm.Expression) -> Branch

branch6 : String -> ( String, Elm.Annotation.Annotation ) -> ( String, Elm.Annotation.Annotation ) -> ( String, Elm.Annotation.Annotation ) -> ( String, Elm.Annotation.Annotation ) -> ( String, Elm.Annotation.Annotation ) -> ( String, Elm.Annotation.Annotation ) -> (Elm.Expression -> Elm.Expression -> Elm.Expression -> Elm.Expression -> Elm.Expression -> Elm.Expression -> Elm.Expression) -> Branch

branchWith : String -> Basics.Int -> (List Elm.Expression -> Elm.Expression) -> Branch

branchList : Basics.Int -> (List Elm.Expression -> Elm.Expression) -> Branch