on 00a3069 help sign in
elk/src/Elm/Compiler/Module.hs
113 lines 1.9 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Elm.Compiler.Module
4 -- interfaces
5 ( I.Interface
6 , I.Interfaces
8 -- module names
9 , Raw
10 , nameToPath
11 , nameToString
12 , nameFromText
13 , hyphenate
14 , dehyphenate
15 , encode
16 , decoder
17 , Error(..)
19 -- canonical names
20 , ModuleName.Canonical(..)
21 )
22 where
25import qualified Data.Char as Char
26import qualified Data.List as List
27import qualified Data.Text as Text
28import System.FilePath ((</>))
30import qualified AST.Module.Name as ModuleName
31import qualified Elm.Interface as I
32import qualified Elm.Name as N
33import qualified Json.Decode as Decode
34import qualified Json.Encode as Encode
38-- NAMES
41type Raw = N.Name
44nameToPath :: Raw -> FilePath
45nameToPath name =
46 List.foldl1 (</>) (map Text.unpack (Text.splitOn "." name))
49nameToString :: Raw -> String
50nameToString name =
51 Text.unpack name
54nameFromText :: Text.Text -> Maybe Raw
55nameFromText =
56 fromText '.'
59hyphenate :: Raw -> Text.Text
60hyphenate name =
61 Text.replace "." "-" name
64dehyphenate :: Text.Text -> Maybe Raw
65dehyphenate =
66 fromText '-'
69fromText :: Char -> Text.Text -> Maybe Raw
70fromText sep name =
71 let
72 chunks =
73 Text.splitOn (Text.singleton sep) name
74 in
75 if all isGoodChunk chunks then Just name else Nothing
78isGoodChunk :: Text.Text -> Bool
79isGoodChunk chunk =
80 case Text.uncons chunk of
81 Nothing ->
82 False
84 Just (first, rest) ->
85 Char.isUpper first && Text.all isGoodChar rest
88isGoodChar :: Char -> Bool
89isGoodChar char =
90 Char.isAlphaNum char || char == '_'
94-- JSON
97encode :: Raw -> Encode.Value
98encode =
99 Encode.text
102data Error = BadModuleName
105decoder :: Decode.Decoder Error Raw
106decoder =
107 do txt <- Decode.text
108 case nameFromText txt of
109 Nothing ->
110 Decode.fail BadModuleName
112 Just name ->
113 Decode.succeed name