Commit | Line | Data |
---|---|---|
c792f15e JB |
1 | port module IO |
2 | exposing | |
3 | ( IO(..) | |
4 | , writeLine | |
5 | , readLine | |
0bac0757 | 6 | , readFile |
c792f15e JB |
7 | , input |
8 | , decodeIO | |
9 | ) | |
10 | ||
11 | import Json.Decode exposing (..) | |
c9c948de | 12 | import Time exposing (Time) |
c792f15e JB |
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 | ||
0bac0757 JB |
25 | {-| Read the contents of a file |
26 | -} | |
27 | port readFile : String -> Cmd msg | |
28 | ||
29 | ||
c792f15e JB |
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 | |
0bac0757 JB |
38 | | FileRead String |
39 | | Exception String | |
c9c948de | 40 | | GotTime Time |
c792f15e JB |
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 | ||
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." |