on a3ad7d2 help sign in
elk/src/Elm/Compiler/Module.hs
88 lines 1.5 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 , nameToString
11 , nameToSlashPath
12 , nameToHyphenPath
13 , encode
14 , decoder
16 -- canonical names
17 , ModuleName.Canonical(..)
18 )
19 where
22import qualified Data.Char as Char
23import qualified Data.List as List
24import qualified Data.Text as Text
25import System.FilePath ((</>))
27import qualified AST.Module.Name as ModuleName
28import qualified Elm.Interface as I
29import qualified Elm.Name as N
30import qualified Json.Decode as Decode
31import qualified Json.Encode as Encode
35-- NAMES
38type Raw = N.Name
41nameToString :: Raw -> String
42nameToString =
43 N.toString
46nameToSlashPath :: Raw -> FilePath
47nameToSlashPath name =
48 List.foldl1 (</>) (map Text.unpack (Text.splitOn "." (N.toText name)))
51nameToHyphenPath :: Raw -> FilePath
52nameToHyphenPath name =
53 Text.unpack (Text.replace "." "-" (N.toText name))
57-- JSON
60encode :: Raw -> Encode.Value
61encode =
62 Encode.name
65decoder :: Decode.Decoder Text.Text Raw
66decoder =
67 do txt <- Decode.text
68 let chunks = Text.splitOn "." txt
69 if all isGoodChunk chunks
70 then Decode.succeed (N.fromText txt)
71 else Decode.fail txt
74isGoodChunk :: Text.Text -> Bool
75isGoodChunk chunk =
76 case Text.uncons chunk of
77 Nothing ->
78 False
80 Just (first, rest) ->
81 Char.isUpper first && Text.all isGoodChar rest
84isGoodChar :: Char -> Bool
85isGoodChar char =
86 Char.isAlphaNum char || char == '_'