elm-street-0.0.0: Crossing the road between Haskell and Elm

Safe HaskellNone
LanguageHaskell2010

Elm.Print

Contents

Description

This module defines prettyprinter for ElmDefinition type. and exports the function to represent it in the convenient way.

Synopsis

Documentation

prettyShowDefinition :: ElmDefinition -> Text Source #

Pretty shows Elm types.

prettyShowEncoder :: ElmDefinition -> Text Source #

Returns the encoder for the given type.

TODO +-------------------+------------------+-----------------+---------------------+ | Haskell Type | Eml Type | Encoder | JSON | +===================+==================+=================+=====================+ | Int | Int | standard encoder | | +-------------------+------------------+------------------+--------------------+

prettyShowDecoder :: ElmDefinition -> Text Source #

  • *Sum Types:**

Haskell type

type User
    = Foo
    | Bar String Int

Encoded JSON on Haskell side

    [ { "tag" : Foo
      }
    , { "tag" : Bar
      , "contents" : ["asd", 42, "qwerty"]
      }
    ]

Elm decoder

userDecoder : Decoder User
userDecoder =
    let decide : String -> Decoder User
        decide x = case x of
            Foo -> D.succeed Foo
            Bar -> D.field "contents" <| D.map2 Bar (D.index 0 D.string) (D.index 1 D.int)
            x -> D.fail <| "There is no constructor for User type:" ++ x
    in D.andThen decide (D.field "tag" D.string)

Standard missing encoders

Standard missing decoders

Internal functions

elmAliasDoc :: ElmAlias -> Doc ann Source #

Pretty printer for Elm aliases:

type alias User =
    { userHeh : String
    , userMeh : Int
    }

elmTypeDoc :: ElmType -> Doc ann Source #

Pretty printer for Elm types with one or more constructors:

type Status a
    = Foo String Int
    | Bar a
    | Baz

If the type is a newtype then additionally unTYPENAME function is generated:

type Id a
    = Id String

unId : Id a -> String
unId (Id x) = x

If the type is Enum this function will add enum specific functions:

type Status
    = Approved
    | Yoyoyo
    | Wow

showStatus : Status -> String
showStatus x = case x of
    Approved -> Approved
    Yoyoyo -> Yoyoyo
    Wow -> Wow

readStatus : String -> Maybe Status
readStatus x = case x of
    Approved -> Just Approved
    Yoyoyo -> Just Yoyoyo
    Wow -> Just Wow
    _ -> Nothing

universeStatus : List Status
universeStatus = [Approved, Yoyoyo, Wow]