on master help sign in
60 lines 2.0 kB history raw
1module Elm (makeElmModuleFromETypeDef) where
3import Data.List
4import Elm.Json
5import Elm.Module hiding (makeElmModuleWithVersion, makeModuleContent, makeModuleContentWithAlterations)
6import Elm.TyRender
7import Elm.TyRep
8import Elm.Versions
9import Prelude
11-- | Creates an Elm module for the given version. This will use the default
12-- type conversion rules (to -- convert @Vector@ to @List@, @HashMap a b@
13-- to @List (a,b)@, etc.).
14makeElmModuleWithVersion ::
15 ElmVersion ->
16 -- | Module name
17 String ->
18 -- | List of definitions to be included in the module
19 [ETypeDef] ->
20 String
21makeElmModuleWithVersion elmVersion moduleName defs =
22 unlines
23 [ moduleHeader elmVersion moduleName,
24 "",
25 "import Json.Decode",
26 "import Json.Encode exposing (Value)",
27 "-- The following module comes from bartavelle/json-helpers",
28 "import Json.Helpers exposing (..)",
29 "import Dict exposing (Dict)",
30 "import Set exposing (Set)",
31 "",
32 ""
33 ]
34 ++ makeModuleContent defs
36-- | Creates an Elm module. This will use the default type conversion rules (to
37-- convert @Vector@ to @List@, @HashMap a b@ to @List (a,b)@, etc.).
38--
39-- default to 0.19
40makeElmModuleFromETypeDef ::
41 -- | Module name
42 String ->
43 -- | List of definitions to be included in the module
44 [ETypeDef] ->
45 String
46makeElmModuleFromETypeDef = makeElmModuleWithVersion Elm0p19
48-- | Generates the content of a module. You will be responsible for
49-- including the required Elm headers. This uses the default type
50-- conversion rules.
51makeModuleContent :: [ETypeDef] -> String
52makeModuleContent = makeModuleContentWithAlterations defaultAlterations
54-- | Generates the content of a module, using custom type conversion rules.
55makeModuleContentWithAlterations :: (ETypeDef -> ETypeDef) -> [ETypeDef] -> String
56makeModuleContentWithAlterations alt = intercalate "\n\n" . map mkDef
57 where
58 mkDef typeDef =
59 let def = alt typeDef
60 in renderElm def ++ "\n" ++ jsonParserForDef def ++ "\n" ++ jsonSerForDef def ++ "\n"