1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-20 01:57:09 +03:00
mal/elm/IO.elm
2017-06-14 15:49:27 +02:00

70 lines
1.2 KiB
Elm

port module IO
exposing
( IO(..)
, writeLine
, readLine
, readFile
, input
, decodeIO
)
import Json.Decode exposing (..)
{-| Output a string to stdout
-}
port writeLine : String -> Cmd msg
{-| Read a line from the stdin
-}
port readLine : String -> Cmd msg
{-| Read the contents of a file
-}
port readFile : String -> Cmd msg
{-| Received a response for a command.
-}
port input : (Value -> msg) -> Sub msg
type IO
= LineRead (Maybe String)
| LineWritten
| FileRead String
| Exception String
decodeIO : Decoder IO
decodeIO =
field "tag" string
|> andThen decodeTag
decodeTag : String -> Decoder IO
decodeTag tag =
case tag of
"lineRead" ->
field "line" (nullable string)
|> map LineRead
"lineWritten" ->
succeed LineWritten
"fileRead" ->
field "contents" string
|> map FileRead
"exception" ->
field "message" string
|> map Exception
_ ->
fail <|
"Trying to decode IO, but tag "
++ tag
++ " is not supported."