@@ -1,6 +1,7 @@
11{-# LANGUAGE OverloadedStrings #-}
22module Elm.ModuleName
33 ( Raw
4+ , nameToChars
45 , nameToString
56 , nameToSlashPath
67 , nameToHyphenPath
@@ -24,7 +25,7 @@ import Control.Monad (liftM2)
2425import Data.Binary (Binary(..))
2526import qualified Data.Char as Char
2627import qualified Data.Name as Name
27−import qualified Data.Text as Text
28+import qualified Data.Utf8 as Utf8
2829import Prelude hiding (maybe)
2930import qualified System.FilePath as FP
3031
@@ -40,26 +41,34 @@ import qualified Json.Encode as Encode
4041type Raw = Name.Name
4142
4243
43−nameToString :: Raw -> String
44+nameToChars :: Raw -> String
45+nameToChars =
46+ Name.toChars
47+
48+
49+nameToString :: Raw -> Utf8.String
4450nameToString =
45− Name.toString
51+ Name.toUtf8
4652
4753
4854nameToSlashPath :: Raw -> FilePath
4955nameToSlashPath name =
50− map (\c -> if c == '.' then FP.pathSeparator else c) (Name.toString name)
56+ map (\c -> if c == '.' then FP.pathSeparator else c) (Name.toChars name)
5157
5258
5359nameToHyphenPath :: Raw -> FilePath
5460nameToHyphenPath name =
55− map (\c -> if c == '.' then '-' else c) (Name.toString name)
61+ map (\c -> if c == '.' then '-' else c) (Name.toChars name)
5662
5763
58−fromHyphenPath :: Text.Text -> Maybe Raw
59−fromHyphenPath txt =
60− let str = Text.unpack txt in
61− if all isGoodChunk (splitOn '-' str)
62− then Just (Name.fromString (map (\c -> if c == '-' then '.' else c) str))
64+fromHyphenPath :: Utf8.String -> Maybe Raw
65+fromHyphenPath str =
66+ let
67+ chunks =
68+ Utf8.split 0x2D {- - -} str
69+ in
70+ if all isGoodChunk chunks
71+ then Just (Name.fromUtf8 (Utf8.join 0x2E {- . -} chunks))
6372 else Nothing
6473
6574
@@ -72,49 +81,18 @@ encode =
7281 Encode.name
7382
7483
75−decoder :: Decode.Decoder Text.Text Raw
84+decoder :: Decode.Decoder Utf8.String Raw
7685decoder =
77− do txt <- Decode.text
78− let str = Text.unpack txt
79− if all isGoodChunk (splitOn '.' str)
80− then Decode.succeed (Name.fromString str)
81− else Decode.fail txt
86+ do str <- Decode.string
87+ if all isGoodChunk (Utf8.split 0x2E {- . -} str)
88+ then return (Name.fromUtf8 str)
89+ else Decode.failure str
8290
8391
84−isGoodChunk :: String -> Bool
92+isGoodChunk :: Utf8.String -> Bool
8593isGoodChunk chunk =
86− case chunk of
87− [] ->
88− False
89−
90− first : rest ->
91− Char.isUpper first && all isGoodChar rest
92−
93−
94−isGoodChar :: Char -> Bool
95−isGoodChar c =
96− Char.isAlphaNum c || c == '_'
97−
98−
99−splitOn :: Char -> String -> [String]
100−splitOn sep str =
101− uncurry (:) (splitOnHelp sep str)
102−
103−
104−splitOnHelp :: Char -> String -> (String, [String])
105−splitOnHelp sep str =
106− case str of
107− [] ->
108− ("",[])
109−
110− c : rest ->
111− let
112− (chunk,chunks) = splitOnHelp sep rest
113− in
114− if c == sep then
115− ("", chunk:chunks)
116− else
117− (c:chunk, chunks)
94+ Utf8.startsWithChar Char.isUpper chunk
95+ && Utf8.all (\c -> Char.isAlphaNum c || c == '_') chunk
11896
11997
12098