on 474ec0d help sign in
elk/src/Elm/Compiler/Module.hs
127 lines 2.3 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Elm.Compiler.Module
4 -- interfaces
5 ( I.Interface
6 , I.Interfaces
7 , interfaceAliasedTypes
8 , programTypes
10 -- module names
11 , Raw
12 , nameToPath
13 , nameToString
14 , nameFromText
15 , hyphenate
16 , dehyphenate
17 , encode
18 , decoder
20 -- canonical names
21 , ModuleName.Canonical(..)
22 )
23 where
25import qualified Data.Char as Char
26import qualified Data.List as List
27import qualified Data.Map as Map
28import qualified Data.Text as Text
29import System.FilePath ((</>))
31import qualified AST.Module.Name as ModuleName
32import qualified Elm.Compiler.Type as Type
33import qualified Elm.Compiler.Type.Extract as Extract
34import qualified Elm.Interface as I
35import qualified Elm.Name as N
36import qualified Json.Decode as Decode
37import qualified Json.Encode as Encode
41-- INTERFACES
44interfaceAliasedTypes :: I.Interface -> Map.Map N.Name Type.Type
45interfaceAliasedTypes interface =
46 Map.map Extract.fromAnnotation (I._types interface)
49programTypes :: I.Interfaces -> ModuleName.Canonical -> Maybe Type.Program
50programTypes =
51 Extract.fromProgram
55-- NAMES
58type Raw = N.Name
61nameToPath :: Raw -> FilePath
62nameToPath name =
63 List.foldl1 (</>) (map Text.unpack (Text.splitOn "." name))
66nameToString :: Raw -> String
67nameToString name =
68 Text.unpack name
71nameFromText :: Text.Text -> Maybe Raw
72nameFromText =
73 fromText '.'
76hyphenate :: Raw -> Text.Text
77hyphenate name =
78 Text.replace "." "-" name
81dehyphenate :: Text.Text -> Maybe Raw
82dehyphenate =
83 fromText '-'
86fromText :: Char -> Text.Text -> Maybe Raw
87fromText sep name =
88 let
89 chunks =
90 Text.splitOn (Text.singleton sep) name
91 in
92 if all isGoodChunk chunks then Just name else Nothing
95isGoodChunk :: Text.Text -> Bool
96isGoodChunk chunk =
97 case Text.uncons chunk of
98 Nothing ->
99 False
101 Just (first, rest) ->
102 Char.isUpper first && Text.all isGoodChar rest
105isGoodChar :: Char -> Bool
106isGoodChar char =
107 Char.isAlphaNum char || char == '_'
111-- JSON
114encode :: Raw -> Encode.Value
115encode =
116 Encode.text
119decoder :: Decode.Decoder Raw
120decoder =
121 do txt <- Decode.text
122 case nameFromText txt of
123 Nothing ->
124 Decode.fail "Expecting a module name like \"Html.Events\""
126 Just name ->
127 Decode.succeed name