on d4e9599 help sign in
96 lines 1.7 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 , fromHyphenPath
14 , encode
15 , decoder
17 -- canonical names
18 , ModuleName.Canonical(..)
19 )
20 where
23import qualified Data.Char as Char
24import qualified Data.List as List
25import qualified Data.Text as Text
26import System.FilePath ((</>))
28import qualified AST.Module.Name as ModuleName
29import qualified Elm.Interface as I
30import qualified Elm.Name as N
31import qualified Json.Decode as Decode
32import qualified Json.Encode as Encode
36-- NAMES
39type Raw = N.Name
42nameToString :: Raw -> String
43nameToString =
44 N.toString
47nameToSlashPath :: Raw -> FilePath
48nameToSlashPath name =
49 List.foldl1 (</>) (map Text.unpack (Text.splitOn "." (N.toText name)))
52nameToHyphenPath :: Raw -> FilePath
53nameToHyphenPath name =
54 Text.unpack (Text.replace "." "-" (N.toText name))
57fromHyphenPath :: Text.Text -> Maybe Raw
58fromHyphenPath txt =
59 if all isGoodChunk (Text.splitOn "-" txt)
60 then Just (N.fromText (Text.replace "-" "." txt))
61 else Nothing
65-- JSON
68encode :: Raw -> Encode.Value
69encode =
70 Encode.name
73decoder :: Decode.Decoder Text.Text Raw
74decoder =
75 do txt <- Decode.text
76 let chunks = Text.splitOn "." txt
77 if all isGoodChunk chunks
78 then Decode.succeed (N.fromText txt)
79 else Decode.fail txt
82isGoodChunk :: Text.Text -> Bool
83isGoodChunk chunk =
84 case Text.uncons chunk of
85 Nothing ->
86 False
88 Just (first, rest) ->
89 Char.isUpper first && Text.all isGoodChar rest
92isGoodChar :: Char -> Bool
93isGoodChar char =
94 Char.isAlphaNum char || char == '_'