@@ -1,6 +1,7 @@
1{-# LANGUAGE OverloadedStrings #-}
1{-# LANGUAGE OverloadedStrings #-}
2module Elm.ModuleName
2module Elm.ModuleName
4 , nameToString
5 , nameToString
5 , nameToSlashPath
6 , nameToSlashPath
6 , nameToHyphenPath
7 , nameToHyphenPath
@@ -24,7 +25,7 @@ import Control.Monad (liftM2)
24import Data.Binary (Binary(..))
25import Data.Binary (Binary(..))
25import qualified Data.Char as Char
26import qualified Data.Char as Char
26import qualified Data.Name as Name
27import qualified Data.Name as Name
27−import qualified Data.Text as Text
28+import qualified Data.Utf8 as Utf8
28import Prelude hiding (maybe)
29import Prelude hiding (maybe)
29import qualified System.FilePath as FP
30import qualified System.FilePath as FP
@@ -40,26 +41,34 @@ import qualified Json.Encode as Encode
40type Raw = Name.Name
41type Raw = Name.Name
44+nameToChars :: Raw -> String
43−nameToString :: Raw -> String
49+nameToString :: Raw -> Utf8.String
44nameToString =
50nameToString =
45− Name.toString
51+ Name.toUtf8
48nameToSlashPath :: Raw -> FilePath
54nameToSlashPath :: Raw -> FilePath
49nameToSlashPath name =
55nameToSlashPath 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)
53nameToHyphenPath :: Raw -> FilePath
59nameToHyphenPath :: Raw -> FilePath
54nameToHyphenPath name =
60nameToHyphenPath name =
55− map (\c -> if c == '.' then '-' else c) (Name.toString name)
61+ map (\c -> if c == '.' then '-' else c) (Name.toChars name)
58−fromHyphenPath :: Text.Text -> Maybe Raw
64+fromHyphenPath :: Utf8.String -> Maybe Raw
59−fromHyphenPath txt =
65+fromHyphenPath str =
60− let str = Text.unpack txt in
66+ let
68+ Utf8.split 0x2D {- - -} str
61− if all isGoodChunk (splitOn '-' str)
70+ if all isGoodChunk chunks
62− then Just (Name.fromString (map (\c -> if c == '-' then '.' else c) str))
71+ then Just (Name.fromUtf8 (Utf8.join 0x2E {- . -} chunks))
63 else Nothing
72 else Nothing
@@ -72,49 +81,18 @@ encode =
72 Encode.name
81 Encode.name
75−decoder :: Decode.Decoder Text.Text Raw
84+decoder :: Decode.Decoder Utf8.String Raw
77− do txt <- Decode.text
86+ do str <- Decode.string
78− let str = Text.unpack txt
79− if all isGoodChunk (splitOn '.' str)
87+ if all isGoodChunk (Utf8.split 0x2E {- . -} str)
80− then Decode.succeed (Name.fromString str)
88+ then return (Name.fromUtf8 str)
81− else Decode.fail txt
89+ else Decode.failure str
84−isGoodChunk :: String -> Bool
92+isGoodChunk :: Utf8.String -> Bool
85isGoodChunk chunk =
93isGoodChunk chunk =
86− case chunk of
94+ Utf8.startsWithChar Char.isUpper chunk
91− Char.isUpper first && all isGoodChar rest
94−isGoodChar :: Char -> Bool
96− Char.isAlphaNum c || c == '_'
95+ && Utf8.all (\c -> Char.isAlphaNum c || c == '_') chunk
99−splitOn :: Char -> String -> [String]
101− uncurry (:) (splitOnHelp sep str)
104−splitOnHelp :: Char -> String -> (String, [String])
105−splitOnHelp sep str =
112− (chunk,chunks) = splitOnHelp sep rest