0 | module Text.WebIDL.Codegen.Enum
 1 |
 2 | import Data.Refined
 3 | import Data.List
 4 | import Text.WebIDL.Codegen.Util
 5 |
 6 | %default total
 7 |
 8 | enum : {opts : _} -> Enum -> Doc opts
 9 | enum (MkEnum _ pn vs) =
10 |   let (s ::: ss) := map value vs
11 |       (c ::: cs) := map toDataConstructor (s ::: ss)
12 |       vals := the (List $ Doc opts) $ map (\sl => line "| \{sl}") cs
13 |       sl := line "data \{pn} =" <++> sep (line "\{c}" :: vals)
14 |       ml :=
15 |         vappend
16 |           (line "data \{pn} =")
17 |           (indent 2 $ vsep (indent 2 (line "\{c}") :: vals))
18 |
19 |       code :=
20 |         vsep
21 |           [ empty
22 |           , line "public export"
23 |           , ifMultiline sl ml
24 |           , empty
25 |           , line "public export"
26 |           , line "Show \{pn} where"
27 |           , indent 2 $ vsep $ zipWith showImpl (c :: cs) (s :: ss)
28 |           , empty
29 |           , line "public export"
30 |           , line "Eq \{pn} where"
31 |           , indent 2 $ line "(==) = (==) `on` show"
32 |           , empty
33 |           , line "public export"
34 |           , line "Ord \{pn} where"
35 |           , indent 2 $ line "compare = compare `on` show"
36 |           , empty
37 |           , line "public export"
38 |           , typeDecl "read" (line "Maybe \{pn}") [ line "String" ]
39 |
40 |           , vsep $ zipWith readImpl (s :: ss) (c :: cs)
41 |           , line "read _ = Nothing"
42 |           , empty
43 |           , line "export"
44 |           , line "ToFFI \{pn} String where"
45 |           , indent 2 $ line "toFFI = show"
46 |           , empty
47 |           , line "export"
48 |           , line "FromFFI \{pn} String where"
49 |           , indent 2 $ line "fromFFI = read"
50 |           ]
51 |
52 |    in vsep ["", line "namespace \{pn}", indent 2 code]
53 |
54 |   where
55 |     showImpl : String -> String -> Doc opts
56 |     showImpl x y = line #"show \#{x} = "\#{y}""#
57 |
58 |     readImpl : String -> String -> Doc opts
59 |     readImpl x y = line #"read "\#{x}" = Just \#{y}"#
60 |
61 | export
62 | enums : List Enum -> String
63 | enums = section "Enums" . map (render80 . enum)
64 |