enum-text 0.1.0.0 → 0.5.0.0
raw patch · 3 files changed
+67/−8 lines, 3 filesdep +timePVP ok
version bump matches the API change (PVP)
Dependencies added: time
API changes (from Hackage documentation)
+ Text.Enum.Text: instance (a Data.Type.Equality.~ GHC.Types.Char) => Text.Enum.Text.TextParsable [a]
+ Text.Enum.Text: instance Text.Enum.Text.TextParsable Data.Text.Internal.Text
+ Text.Enum.Text: instance Text.Enum.Text.TextParsable Data.Time.Calendar.Days.Day
+ Text.Enum.Text: instance Text.Enum.Text.TextParsable Data.Time.Clock.Internal.UTCTime.UTCTime
+ Text.Enum.Text: instance Text.Enum.Text.TextParsable GHC.Types.Int
+ Text.Enum.Text: instance Text.Enum.Text.TextParsable a => Text.Enum.Text.TextParsable (GHC.Maybe.Maybe a)
Files
- ChangeLog.md +4/−0
- enum-text.cabal +3/−2
- src/Text/Enum/Text.hs +60/−6
ChangeLog.md view
@@ -1,3 +1,7 @@+# 0.5.0.0++ * add basic TextParsable instances and toolkit functions+ # 0.1.0.0 * first release
enum-text.cabal view
@@ -4,10 +4,10 @@ -- -- see: https://github.com/sol/hpack ----- hash: de41441f44fa188062adf3893567b62b7ee598e3ad3cac9d4a37c598b02684da+-- hash: 0aa3dafda03153aaeb2d912503d7c418a6e7beaba9d3a0830cf570112e6f8500 name: enum-text-version: 0.1.0.0+version: 0.5.0.0 synopsis: A text rendering and parsing toolkit for enumerated types description: A text rendering and parsing toolkit for enumerated types. Please see the README on GitHub at <https://github.com/cdornan/enum-text#readme> category: Text@@ -43,5 +43,6 @@ , hashable , possibly , text+ , time , unordered-containers default-language: Haskell2010
src/Text/Enum/Text.hs view
@@ -1,6 +1,8 @@+{-# LANGUAGE GADTs #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE RecordWildCards #-} {-# LANGUAGE ScopedTypeVariables #-}+{-# LANGUAGE LambdaCase #-} module Text.Enum.Text ( EnumText(..)@@ -17,14 +19,13 @@ import qualified Data.HashMap.Strict as HM import qualified Data.Text as T import qualified Data.Text.Encoding as TE+import Data.Text.Read+import Data.Time import Fmt+import Text.Read --- | a class for 'T.Text' parsers.-class TextParsable a where- parseText :: T.Text -> Possibly a--{- | our toolkit for enumerated types which should be defined as follows:+{- | Our toolkit for enumerated types which should be defined as follows: @ import Fmt@@ -86,7 +87,7 @@ -- EnumTextConfig, defaultEnumTextConfig ------------------------------------------------------------------------------- --- | configures the default implementation of 'renderEnumText'+-- | Configures the default implementation of 'renderEnumText' data EnumTextConfig = EnumTextConfig { _etc_text_prep :: T.Text -> T.Text -- ^ applied to the output of 'show'@@ -96,8 +97,15 @@ -- the first '_' , _etc_char_prep :: Char -> Char -- ^ applied to each character of -- the outpout of '_etc_text_prep'+ -- (by default flips underscores (@_@)+ -- to dashes (@-@) } +-- | The default 'configEnumText' for 'EnumText':+--+-- * '_etc_text_prep' removes the prefix up to and including the first+-- underscore ('_')+-- * '_etc_char_prep' flips the underscores (@_@) to dashes (@-@) defaultEnumTextConfig :: EnumTextConfig defaultEnumTextConfig = EnumTextConfig@@ -117,6 +125,26 @@ -------------------------------------------------------------------------------+-- TextParsable+-------------------------------------------------------------------------------++-- | a class for 'T.Text' parsers.+class TextParsable a where+ parseText :: T.Text -> Possibly a++instance TextParsable T.Text where parseText = return+instance TextParsable UTCTime where parseText = parseTextRead "UTCTime"+instance TextParsable Day where parseText = parseTextRead "Day"+instance TextParsable Int where parseText = parseDecimal+instance a ~ Char => TextParsable [a] where parseText = return . T.unpack++instance TextParsable a => TextParsable (Maybe a) where+ parseText = \case+ "" -> Right Nothing+ s -> Just <$> parseText s+++------------------------------------------------------------------------------- -- arrays ------------------------------------------------------------------------------- @@ -163,3 +191,29 @@ [ (TE.encodeUtf8 $ renderEnumText c,c) | c <- [minBound..maxBound] ]+++-------------------------------------------------------------------------------+-- internal parsers+-------------------------------------------------------------------------------++-- | parse a decimal integer using the "Text.Read" toolkit+parseDecimal :: T.Text -> Possibly Int+parseDecimal txt = either (Left . typeError "integer") return $ do+ (x,r) <- signed decimal txt+ case T.null r of+ True -> return x+ False -> Left $ "residual input: " ++ T.unpack r++-- | Convert a 'Read' parser into a 'TextParsable'+parseTextRead :: Read a+ => String -- ^ name of type bing parsed (for failure message)+ -> T.Text -- ^ 'T.Text' to be parsed+ -> Possibly a+parseTextRead ty_s txt =+ maybe (Left $ typeError ty_s $ show str) Right $ readMaybe str+ where+ str = T.unpack txt++typeError :: String -> String -> String+typeError ty_s msg = "failed to parse "++ty_s++": "++msg