Browse files

Moved Text and String conversions to convertible

  • Loading branch information...
1 parent 23b33ee commit bab694e490c2f9535b4c73ee80053b99ce715449 @snoyberg committed Nov 23, 2009
View
220 Data/Convertible/Instances/String.hs
@@ -1,220 +0,0 @@
-{-# 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
-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
View
24 Data/Convertible/Instances/StringHelper.hs
@@ -1,24 +0,0 @@
-types = ["BS.ByteString", "BL.ByteString", "ST.Text", "LT.Text"]
-fromStringAttempts = ["Day", "Bool", "Int", "(Ratio Integer)"]
-fromStringSuccesses = []
-toStringAttempts = []
-toStringSuccesses = ["Day", "Bool", "Int", "(Ratio Integer)"]
-
-main = do
- mapM_ fsa $ fromStringAttempts ++ fromStringSuccesses
- mapM_ fss fromStringSuccesses
- mapM_ tsa $ toStringAttempts ++ toStringSuccesses
- mapM_ tss toStringSuccesses
-
-fsa t = forM_ types (\f -> putStrLn $
- "instance ConvertAttempt " ++ f ++ " " ++ t ++ " where\n" ++
- " convertAttempt = fromStringA <=< convertAttempt")
-fss t = forM_ types (\f -> putStrLn $
- "instance ConvertSuccess " ++ f ++ " " ++ t ++ " where\n" ++
- " convertSuccess = fromString . convertSuccess")
-tsa f = forM_ types (\t -> putStrLn $
- "instance ConvertAttempt " ++ f ++ " " ++ t ++ " where\n" ++
- " convertAttempt = convertAttempt . toString")
-tss f = forM_ types (\t -> putStrLn $
- "instance ConvertSuccess " ++ f ++ " " ++ t ++ " where\n" ++
- " convertSuccess = convertSuccess . toString")
View
134 Data/Convertible/Instances/Text.hs
@@ -1,134 +0,0 @@
-{-# 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
-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
View
13 Data/Convertible/Instances/TextSkel.hs
@@ -1,13 +0,0 @@
-types = ["[Char]", "BS.ByteString", "BL.ByteString", "TS.Text", "TL.Text"]
-
-pairs = do
- from <- types
- to <- types
- return (from, to)
-
-main = do
- mapM_ cs pairs
- mapM_ ca pairs
-
-cs (f, t) = putStrLn $ "instance ConvertSuccess " ++ f ++ " " ++ t ++ " where\n convertSuccess = "
-ca (f, t) = putStrLn $ "instance ConvertAttempt " ++ f ++ " " ++ t ++ " where\n convertAttempt = return . convertSuccess"
View
3 Data/Object.hs
@@ -57,8 +57,6 @@ module Data.Object
-- $scalarToFromObject
, scalarToObject
, scalarFromObject
- -- Instances
- , module Data.Convertible.Instances.Text
) where
import Control.Arrow
@@ -77,7 +75,6 @@ import Control.Exception (Exception)
import Data.Attempt
import Data.Convertible
-import Data.Convertible.Instances.Text
-- | Can represent nested values as scalars, sequences and mappings. A
-- sequence is synonymous with a list, while a mapping is synonymous with a
View
1 Data/Object/String.hs
@@ -29,7 +29,6 @@ import Data.Object
import Data.Attempt
import Data.Convertible
-import Data.Convertible.Instances.String ()
import Data.Time.Calendar
import Data.Ratio (Ratio)
View
1 Data/Object/Text.hs
@@ -27,7 +27,6 @@ import Data.Text.Lazy (Text)
import Data.Attempt
import Data.Convertible
-import Data.Convertible.Instances.String ()
import Data.Time.Calendar
import Data.Ratio (Ratio)
View
2 data-object.cabal
@@ -30,6 +30,4 @@ library
Data.Object.Text
Data.Object.Scalar
Data.Object.String
- Data.Convertible.Instances.Text
- Data.Convertible.Instances.String
ghc-options: -Wall

0 comments on commit bab694e

Please sign in to comment.