on 7dfbc30 help sign in
elk/src/Elm/Compiler/Module.hs
109 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
18 -- canonical names
19 , ModuleName.Canonical(..)
20 )
21 where
24import qualified Data.Char as Char
25import qualified Data.List as List
26import qualified Data.Text as Text
27import System.FilePath ((</>))
29import qualified AST.Module.Name as ModuleName
30import qualified Elm.Interface as I
31import qualified Elm.Name as N
32import qualified Json.Decode as Decode
33import qualified Json.Encode as Encode
37-- NAMES
40type Raw = N.Name
43nameToPath :: Raw -> FilePath
44nameToPath name =
45 List.foldl1 (</>) (map Text.unpack (Text.splitOn "." name))
48nameToString :: Raw -> String
49nameToString name =
50 Text.unpack name
53nameFromText :: Text.Text -> Maybe Raw
54nameFromText =
55 fromText '.'
58hyphenate :: Raw -> Text.Text
59hyphenate name =
60 Text.replace "." "-" name
63dehyphenate :: Text.Text -> Maybe Raw
64dehyphenate =
65 fromText '-'
68fromText :: Char -> Text.Text -> Maybe Raw
69fromText sep name =
70 let
71 chunks =
72 Text.splitOn (Text.singleton sep) name
73 in
74 if all isGoodChunk chunks then Just name else Nothing
77isGoodChunk :: Text.Text -> Bool
78isGoodChunk chunk =
79 case Text.uncons chunk of
80 Nothing ->
81 False
83 Just (first, rest) ->
84 Char.isUpper first && Text.all isGoodChar rest
87isGoodChar :: Char -> Bool
88isGoodChar char =
89 Char.isAlphaNum char || char == '_'
93-- JSON
96encode :: Raw -> Encode.Value
97encode =
98 Encode.text
101decoder :: Decode.Decoder Raw
102decoder =
103 do txt <- Decode.text
104 case nameFromText txt of
105 Nothing ->
106 Decode.fail "Expecting a module name like \"Html.Events\""
108 Just name ->
109 Decode.succeed name