This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.
1.0.1

Elm.Syntax.Infix

Infix Syntax

#Types

type alias Infix = { direction : InfixDirection , precedence : Int , operator : String }

Type annotation for a infix definition

type InfixDirection = Left | Right

Union type for infix direction

Json

encode : Infix -> Value

Encode an infix

encodeDirection : InfixDirection -> Value

Encode the infix direction

decode : Decoder Infix

Decode an infix

decodeDirection : Decoder InfixDirection

Decode a infix direction

module Elm.Syntax.Infix exposing (Infix, InfixDirection(Left, Right), decode, decodeDirection, encode, encodeDirection)

{-| Infix Syntax

#Types

@docs Infix, InfixDirection


# Json

@docs encode, encodeDirection, decode, decodeDirection

-}

import Json.Decode as JD exposing (Decoder)
import Json.Decode.Extra exposing ((|:))
import Json.Encode as JE exposing (Value)


{-| Type annotation for a infix definition
-}
type alias Infix =
    { direction : InfixDirection
    , precedence : Int
    , operator : String
    }


{-| Union type for infix direction
-}
type InfixDirection
    = Left
    | Right


{-| Encode an infix
-}
encode : Infix -> Value
encode inf =
    JE.object
        [ ( "direction", encodeDirection inf.direction )
        , ( "precedence", JE.int inf.precedence )
        , ( "operator", JE.string inf.operator )
        ]


{-| Encode the infix direction
-}
encodeDirection : InfixDirection -> Value
encodeDirection d =
    case d of
        Left ->
            JE.string "left"

        Right ->
            JE.string "right"


{-| Decode an infix
-}
decode : Decoder Infix
decode =
    JD.succeed Infix
        |: JD.field "direction" decodeDirection
        |: JD.field "precedence" JD.int
        |: JD.field "operator" JD.string


{-| Decode a infix direction
-}
decodeDirection : Decoder InfixDirection
decodeDirection =
    JD.string
        |> JD.andThen
            (\v ->
                case v of
                    "left" ->
                        JD.succeed Left

                    "right" ->
                        JD.succeed Right

                    _ ->
                        JD.fail "Invlalid direction"
            )