Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added String, ByteString and Text instances.
This commit also changes the name of the package to convertible-text. The main module is renamed to Data.Convertible.Text, which should be the module name even after the release of convertible 1.2.0.
- Loading branch information
Showing
7 changed files
with
406 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,220 @@ | ||
{-# LANGUAGE MultiParamTypeClasses #-} | ||
{-# LANGUAGE FlexibleInstances #-} | ||
{-# LANGUAGE FlexibleContexts #-} | ||
{-# LANGUAGE DeriveDataTypeable #-} | ||
{-# OPTIONS_GHC -fno-warn-orphans #-} | ||
--------------------------------------------------------- | ||
-- | ||
-- Module : Data.Convertible.Instances.String | ||
-- Copyright : Michael Snoyman | ||
-- License : BSD3 | ||
-- | ||
-- Maintainer : Michael Snoyman <michael@snoyman.com> | ||
-- Stability : Stable | ||
-- Portability : portable | ||
-- | ||
--------------------------------------------------------- | ||
|
||
-- | Instances of 'ConvertSuccess' and 'ConvertAttempt' for 'String', along | ||
-- with instances for bytestrings and text (lazy and strict). | ||
module Data.Convertible.Instances.String | ||
( InvalidDayException (..) | ||
, InvalidBoolException (..) | ||
) where | ||
|
||
import Data.Convertible.Base | ||
import Data.Typeable (Typeable) | ||
import Control.Exception (Exception) | ||
import qualified Safe.Failure as SF | ||
import Data.Convertible.Instances.Text () | ||
import Data.Attempt | ||
import Control.Monad ((<=<)) | ||
|
||
import qualified Data.ByteString as BS | ||
import qualified Data.ByteString.Lazy as BL | ||
import qualified Data.Text as ST | ||
import qualified Data.Text.Lazy as LT | ||
|
||
import Data.Time.Calendar | ||
import Data.Ratio (Ratio) | ||
|
||
{- Not needed yet | ||
fromString :: ConvertSuccess String a => String -> a | ||
fromString = convertSuccess | ||
-} | ||
|
||
fromStringA :: ConvertAttempt String a => String -> Attempt a | ||
fromStringA = convertAttempt | ||
|
||
toString :: ConvertSuccess a String => a -> String | ||
toString = convertSuccess | ||
|
||
-- Day | ||
data InvalidDayException = InvalidDayException String | ||
deriving (Show, Typeable) | ||
instance Exception InvalidDayException | ||
|
||
instance ConvertSuccess Day [Char] where | ||
convertSuccess = show | ||
instance ConvertAttempt Day [Char] where | ||
convertAttempt = return . convertSuccess | ||
instance ConvertAttempt [Char] Day where | ||
convertAttempt s = wrapFailure (const $ InvalidDayException s) $ do | ||
SF.assert (length s == 10) () $ InvalidDayException s | ||
y <- SF.read $ take 4 s | ||
m <- SF.read $ take 2 $ drop 5 s | ||
d <- SF.read $ take 2 $ drop 8 s | ||
return $ fromGregorian y m d | ||
|
||
-- Bool | ||
data InvalidBoolException = InvalidBoolException String | ||
deriving (Show, Typeable) | ||
instance Exception InvalidBoolException | ||
|
||
instance ConvertAttempt Bool [Char] where | ||
convertAttempt = return . convertSuccess | ||
instance ConvertSuccess Bool [Char] where | ||
convertSuccess b = if b then "true" else "false" | ||
instance ConvertAttempt [Char] Bool where | ||
convertAttempt s = | ||
case s of | ||
-- list comes from http://yaml.org/type/bool.html | ||
"y" -> return True | ||
"Y" -> return True | ||
"yes" -> return True | ||
"Yes" -> return True | ||
"YES" -> return True | ||
"true" -> return True | ||
"True" -> return True | ||
"TRUE" -> return True | ||
"on" -> return True | ||
"On" -> return True | ||
"ON" -> return True | ||
|
||
"n" -> return False | ||
"N" -> return False | ||
"no" -> return False | ||
"No" -> return False | ||
"NO" -> return False | ||
"false" -> return False | ||
"False" -> return False | ||
"FALSE" -> return False | ||
"off" -> return False | ||
"Off" -> return False | ||
"OFF" -> return False | ||
|
||
_ -> failure $ InvalidBoolException s | ||
|
||
-- Int | ||
instance ConvertSuccess Int [Char] where | ||
convertSuccess = show | ||
instance ConvertAttempt Int [Char] where | ||
convertAttempt = return . convertSuccess | ||
instance ConvertAttempt [Char] Int where | ||
convertAttempt = SF.read | ||
|
||
-- Rational | ||
instance ConvertSuccess (Ratio Integer) [Char] where | ||
convertSuccess = show | ||
instance ConvertAttempt (Ratio Integer) [Char] where | ||
convertAttempt = return . convertSuccess | ||
instance ConvertAttempt [Char] (Ratio Integer) where | ||
convertAttempt = SF.read | ||
|
||
-- Instances for bytestrings and text | ||
instance ConvertAttempt BS.ByteString Day where | ||
convertAttempt = fromStringA <=< convertAttempt | ||
instance ConvertAttempt BL.ByteString Day where | ||
convertAttempt = fromStringA <=< convertAttempt | ||
instance ConvertAttempt ST.Text Day where | ||
convertAttempt = fromStringA <=< convertAttempt | ||
instance ConvertAttempt LT.Text Day where | ||
convertAttempt = fromStringA <=< convertAttempt | ||
instance ConvertAttempt BS.ByteString Bool where | ||
convertAttempt = fromStringA <=< convertAttempt | ||
instance ConvertAttempt BL.ByteString Bool where | ||
convertAttempt = fromStringA <=< convertAttempt | ||
instance ConvertAttempt ST.Text Bool where | ||
convertAttempt = fromStringA <=< convertAttempt | ||
instance ConvertAttempt LT.Text Bool where | ||
convertAttempt = fromStringA <=< convertAttempt | ||
instance ConvertAttempt BS.ByteString Int where | ||
convertAttempt = fromStringA <=< convertAttempt | ||
instance ConvertAttempt BL.ByteString Int where | ||
convertAttempt = fromStringA <=< convertAttempt | ||
instance ConvertAttempt ST.Text Int where | ||
convertAttempt = fromStringA <=< convertAttempt | ||
instance ConvertAttempt LT.Text Int where | ||
convertAttempt = fromStringA <=< convertAttempt | ||
instance ConvertAttempt BS.ByteString (Ratio Integer) where | ||
convertAttempt = fromStringA <=< convertAttempt | ||
instance ConvertAttempt BL.ByteString (Ratio Integer) where | ||
convertAttempt = fromStringA <=< convertAttempt | ||
instance ConvertAttempt ST.Text (Ratio Integer) where | ||
convertAttempt = fromStringA <=< convertAttempt | ||
instance ConvertAttempt LT.Text (Ratio Integer) where | ||
convertAttempt = fromStringA <=< convertAttempt | ||
instance ConvertAttempt Day BS.ByteString where | ||
convertAttempt = convertAttempt . toString | ||
instance ConvertAttempt Day BL.ByteString where | ||
convertAttempt = convertAttempt . toString | ||
instance ConvertAttempt Day ST.Text where | ||
convertAttempt = convertAttempt . toString | ||
instance ConvertAttempt Day LT.Text where | ||
convertAttempt = convertAttempt . toString | ||
instance ConvertAttempt Bool BS.ByteString where | ||
convertAttempt = convertAttempt . toString | ||
instance ConvertAttempt Bool BL.ByteString where | ||
convertAttempt = convertAttempt . toString | ||
instance ConvertAttempt Bool ST.Text where | ||
convertAttempt = convertAttempt . toString | ||
instance ConvertAttempt Bool LT.Text where | ||
convertAttempt = convertAttempt . toString | ||
instance ConvertAttempt Int BS.ByteString where | ||
convertAttempt = convertAttempt . toString | ||
instance ConvertAttempt Int BL.ByteString where | ||
convertAttempt = convertAttempt . toString | ||
instance ConvertAttempt Int ST.Text where | ||
convertAttempt = convertAttempt . toString | ||
instance ConvertAttempt Int LT.Text where | ||
convertAttempt = convertAttempt . toString | ||
instance ConvertAttempt (Ratio Integer) BS.ByteString where | ||
convertAttempt = convertAttempt . toString | ||
instance ConvertAttempt (Ratio Integer) BL.ByteString where | ||
convertAttempt = convertAttempt . toString | ||
instance ConvertAttempt (Ratio Integer) ST.Text where | ||
convertAttempt = convertAttempt . toString | ||
instance ConvertAttempt (Ratio Integer) LT.Text where | ||
convertAttempt = convertAttempt . toString | ||
instance ConvertSuccess Day BS.ByteString where | ||
convertSuccess = convertSuccess . toString | ||
instance ConvertSuccess Day BL.ByteString where | ||
convertSuccess = convertSuccess . toString | ||
instance ConvertSuccess Day ST.Text where | ||
convertSuccess = convertSuccess . toString | ||
instance ConvertSuccess Day LT.Text where | ||
convertSuccess = convertSuccess . toString | ||
instance ConvertSuccess Bool BS.ByteString where | ||
convertSuccess = convertSuccess . toString | ||
instance ConvertSuccess Bool BL.ByteString where | ||
convertSuccess = convertSuccess . toString | ||
instance ConvertSuccess Bool ST.Text where | ||
convertSuccess = convertSuccess . toString | ||
instance ConvertSuccess Bool LT.Text where | ||
convertSuccess = convertSuccess . toString | ||
instance ConvertSuccess Int BS.ByteString where | ||
convertSuccess = convertSuccess . toString | ||
instance ConvertSuccess Int BL.ByteString where | ||
convertSuccess = convertSuccess . toString | ||
instance ConvertSuccess Int ST.Text where | ||
convertSuccess = convertSuccess . toString | ||
instance ConvertSuccess Int LT.Text where | ||
convertSuccess = convertSuccess . toString | ||
instance ConvertSuccess (Ratio Integer) BS.ByteString where | ||
convertSuccess = convertSuccess . toString | ||
instance ConvertSuccess (Ratio Integer) BL.ByteString where | ||
convertSuccess = convertSuccess . toString | ||
instance ConvertSuccess (Ratio Integer) ST.Text where | ||
convertSuccess = convertSuccess . toString | ||
instance ConvertSuccess (Ratio Integer) LT.Text where | ||
convertSuccess = convertSuccess . toString |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
{-# LANGUAGE MultiParamTypeClasses #-} | ||
{-# LANGUAGE FlexibleInstances #-} | ||
{-# LANGUAGE FlexibleContexts #-} | ||
{-# OPTIONS_GHC -fno-warn-orphans #-} | ||
--------------------------------------------------------- | ||
-- | ||
-- Module : Data.Convertible.Instances.Text | ||
-- Copyright : Michael Snoyman | ||
-- License : BSD3 | ||
-- | ||
-- Maintainer : Michael Snoyman <michael@snoyman.com> | ||
-- Stability : Stable | ||
-- Portability : portable | ||
-- | ||
--------------------------------------------------------- | ||
|
||
-- | Instances to convert amongst 'String's, strict bytestrings, lazy | ||
-- bytestrings, strict text and lazy text. | ||
module Data.Convertible.Instances.Text () where | ||
|
||
import Data.Convertible.Base | ||
import qualified Data.ByteString as BS | ||
import qualified Data.ByteString.Lazy as BL | ||
import qualified Data.Text as ST | ||
import qualified Data.Text.Lazy as LT | ||
import qualified Data.Text.Encoding as STE | ||
import qualified Data.Text.Lazy.Encoding as LTE | ||
|
||
toST :: ConvertSuccess a ST.Text => a -> ST.Text | ||
toST = convertSuccess | ||
|
||
toLT :: ConvertSuccess a LT.Text => a -> LT.Text | ||
toLT = convertSuccess | ||
|
||
instance ConvertSuccess [Char] [Char] where | ||
convertSuccess = id | ||
instance ConvertSuccess [Char] BS.ByteString where | ||
convertSuccess = convertSuccess . toST | ||
instance ConvertSuccess [Char] BL.ByteString where | ||
convertSuccess = convertSuccess . toLT | ||
instance ConvertSuccess [Char] ST.Text where | ||
convertSuccess = ST.pack | ||
instance ConvertSuccess [Char] LT.Text where | ||
convertSuccess = LT.pack | ||
instance ConvertSuccess BS.ByteString [Char] where | ||
convertSuccess = convertSuccess . toST | ||
instance ConvertSuccess BS.ByteString BS.ByteString where | ||
convertSuccess = id | ||
instance ConvertSuccess BS.ByteString BL.ByteString where | ||
convertSuccess = BL.fromChunks . return | ||
instance ConvertSuccess BS.ByteString ST.Text where | ||
convertSuccess = STE.decodeUtf8 | ||
instance ConvertSuccess BS.ByteString LT.Text where | ||
convertSuccess = convertSuccess . toST | ||
instance ConvertSuccess BL.ByteString [Char] where | ||
convertSuccess = convertSuccess . toLT | ||
instance ConvertSuccess BL.ByteString BS.ByteString where | ||
convertSuccess = BS.concat . BL.toChunks | ||
instance ConvertSuccess BL.ByteString BL.ByteString where | ||
convertSuccess = id | ||
instance ConvertSuccess BL.ByteString ST.Text where | ||
convertSuccess = convertSuccess . BS.concat . BL.toChunks | ||
instance ConvertSuccess BL.ByteString LT.Text where | ||
convertSuccess = LTE.decodeUtf8 | ||
instance ConvertSuccess ST.Text [Char] where | ||
convertSuccess = ST.unpack | ||
instance ConvertSuccess ST.Text BS.ByteString where | ||
convertSuccess = STE.encodeUtf8 | ||
instance ConvertSuccess ST.Text BL.ByteString where | ||
convertSuccess = convertSuccess . STE.encodeUtf8 | ||
instance ConvertSuccess ST.Text ST.Text where | ||
convertSuccess = id | ||
instance ConvertSuccess ST.Text LT.Text where | ||
convertSuccess = LT.fromChunks . return | ||
instance ConvertSuccess LT.Text [Char] where | ||
convertSuccess = LT.unpack | ||
instance ConvertSuccess LT.Text BS.ByteString where | ||
convertSuccess = convertSuccess . toST | ||
instance ConvertSuccess LT.Text BL.ByteString where | ||
convertSuccess = LTE.encodeUtf8 | ||
instance ConvertSuccess LT.Text ST.Text where | ||
convertSuccess = ST.concat . LT.toChunks | ||
instance ConvertSuccess LT.Text LT.Text where | ||
convertSuccess = id | ||
instance ConvertAttempt [Char] [Char] where | ||
convertAttempt = return . convertSuccess | ||
instance ConvertAttempt [Char] BS.ByteString where | ||
convertAttempt = return . convertSuccess | ||
instance ConvertAttempt [Char] BL.ByteString where | ||
convertAttempt = return . convertSuccess | ||
instance ConvertAttempt [Char] ST.Text where | ||
convertAttempt = return . convertSuccess | ||
instance ConvertAttempt [Char] LT.Text where | ||
convertAttempt = return . convertSuccess | ||
instance ConvertAttempt BS.ByteString [Char] where | ||
convertAttempt = return . convertSuccess | ||
instance ConvertAttempt BS.ByteString BS.ByteString where | ||
convertAttempt = return . convertSuccess | ||
instance ConvertAttempt BS.ByteString BL.ByteString where | ||
convertAttempt = return . convertSuccess | ||
instance ConvertAttempt BS.ByteString ST.Text where | ||
convertAttempt = return . convertSuccess | ||
instance ConvertAttempt BS.ByteString LT.Text where | ||
convertAttempt = return . convertSuccess | ||
instance ConvertAttempt BL.ByteString [Char] where | ||
convertAttempt = return . convertSuccess | ||
instance ConvertAttempt BL.ByteString BS.ByteString where | ||
convertAttempt = return . convertSuccess | ||
instance ConvertAttempt BL.ByteString BL.ByteString where | ||
convertAttempt = return . convertSuccess | ||
instance ConvertAttempt BL.ByteString ST.Text where | ||
convertAttempt = return . convertSuccess | ||
instance ConvertAttempt BL.ByteString LT.Text where | ||
convertAttempt = return . convertSuccess | ||
instance ConvertAttempt ST.Text [Char] where | ||
convertAttempt = return . convertSuccess | ||
instance ConvertAttempt ST.Text BS.ByteString where | ||
convertAttempt = return . convertSuccess | ||
instance ConvertAttempt ST.Text BL.ByteString where | ||
convertAttempt = return . convertSuccess | ||
instance ConvertAttempt ST.Text ST.Text where | ||
convertAttempt = return . convertSuccess | ||
instance ConvertAttempt ST.Text LT.Text where | ||
convertAttempt = return . convertSuccess | ||
instance ConvertAttempt LT.Text [Char] where | ||
convertAttempt = return . convertSuccess | ||
instance ConvertAttempt LT.Text BS.ByteString where | ||
convertAttempt = return . convertSuccess | ||
instance ConvertAttempt LT.Text BL.ByteString where | ||
convertAttempt = return . convertSuccess | ||
instance ConvertAttempt LT.Text ST.Text where | ||
convertAttempt = return . convertSuccess | ||
instance ConvertAttempt LT.Text LT.Text where | ||
convertAttempt = return . convertSuccess |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.