Elm step A
[jackhill/mal.git] / elm / IO.elm
CommitLineData
c792f15e
JB
1port module IO
2 exposing
3 ( IO(..)
4 , writeLine
5 , readLine
0bac0757 6 , readFile
c792f15e
JB
7 , input
8 , decodeIO
9 )
10
11import Json.Decode exposing (..)
c9c948de 12import Time exposing (Time)
c792f15e
JB
13
14
15{-| Output a string to stdout
16-}
17port writeLine : String -> Cmd msg
18
19
20{-| Read a line from the stdin
21-}
22port readLine : String -> Cmd msg
23
24
0bac0757
JB
25{-| Read the contents of a file
26-}
27port readFile : String -> Cmd msg
28
29
c792f15e
JB
30{-| Received a response for a command.
31-}
32port input : (Value -> msg) -> Sub msg
33
34
35type IO
36 = LineRead (Maybe String)
37 | LineWritten
0bac0757
JB
38 | FileRead String
39 | Exception String
c9c948de 40 | GotTime Time
c792f15e
JB
41
42
43decodeIO : Decoder IO
44decodeIO =
45 field "tag" string
46 |> andThen decodeTag
47
48
49decodeTag : String -> Decoder IO
50decodeTag tag =
51 case tag of
52 "lineRead" ->
53 field "line" (nullable string)
54 |> map LineRead
55
56 "lineWritten" ->
57 succeed LineWritten
58
0bac0757
JB
59 "fileRead" ->
60 field "contents" string
61 |> map FileRead
62
63 "exception" ->
64 field "message" string
65 |> map Exception
66
c792f15e
JB
67 _ ->
68 fail <|
69 "Trying to decode IO, but tag "
70 ++ tag
71 ++ " is not supported."