on 247359c help sign in
elk/src/Elm/Compiler/Module.hs
121 lines 2.2 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Elm.Compiler.Module
4 -- interfaces
5 ( I.Interface
6 , I.Interfaces
7 , interfaceAliasedTypes
9 -- module names
10 , Raw
11 , nameToPath
12 , nameToString
13 , nameFromText
14 , hyphenate
15 , dehyphenate
16 , encode
17 , decoder
19 -- canonical names
20 , ModuleName.Canonical(..)
21 )
22 where
24import qualified Data.Char as Char
25import qualified Data.List as List
26import qualified Data.Map as Map
27import qualified Data.Text as Text
28import System.FilePath ((</>))
30import qualified AST.Module.Name as ModuleName
31import qualified Elm.Compiler.Type as Type
32import qualified Elm.Compiler.Type.Extract as Extract
33import qualified Elm.Interface as I
34import qualified Elm.Name as N
35import qualified Json.Decode as Decode
36import qualified Json.Encode as Encode
40-- INTERFACES
43interfaceAliasedTypes :: I.Interface -> Map.Map N.Name Type.Type
44interfaceAliasedTypes interface =
45 Map.map Extract.fromAnnotation (I._types interface)
49-- NAMES
52type Raw = N.Name
55nameToPath :: Raw -> FilePath
56nameToPath name =
57 List.foldl1 (</>) (map Text.unpack (Text.splitOn "." name))
60nameToString :: Raw -> String
61nameToString name =
62 Text.unpack name
65nameFromText :: Text.Text -> Maybe Raw
66nameFromText =
67 fromText '.'
70hyphenate :: Raw -> Text.Text
71hyphenate name =
72 Text.replace "." "-" name
75dehyphenate :: Text.Text -> Maybe Raw
76dehyphenate =
77 fromText '-'
80fromText :: Char -> Text.Text -> Maybe Raw
81fromText sep name =
82 let
83 chunks =
84 Text.splitOn (Text.singleton sep) name
85 in
86 if all isGoodChunk chunks then Just name else Nothing
89isGoodChunk :: Text.Text -> Bool
90isGoodChunk chunk =
91 case Text.uncons chunk of
92 Nothing ->
93 False
95 Just (first, rest) ->
96 Char.isUpper first && Text.all isGoodChar rest
99isGoodChar :: Char -> Bool
100isGoodChar char =
101 Char.isAlphaNum char || char == '_'
105-- JSON
108encode :: Raw -> Encode.Value
109encode =
110 Encode.text
113decoder :: Decode.Decoder Raw
114decoder =
115 do txt <- Decode.text
116 case nameFromText txt of
117 Nothing ->
118 Decode.fail "Expecting a module name like \"Html.Events\""
120 Just name ->
121 Decode.succeed name