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