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)
fromStringA :: ConvertAttempt String a => String -> Attempt a
fromStringA = convertAttempt
toString :: ConvertSuccess a String => a -> String
toString = convertSuccess
data InvalidDayException = InvalidDayException String
deriving (Show, Typeable)
instance Exception InvalidDayException
instance ConvertSuccess Day [Char] where
convertSuccess = show
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
data InvalidBoolException = InvalidBoolException String
deriving (Show, Typeable)
instance Exception InvalidBoolException
instance ConvertSuccess Bool [Char] where
convertSuccess b = if b then "true" else "false"
instance ConvertAttempt [Char] Bool where
convertAttempt s =
case s of
"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
instance ConvertSuccess Int [Char] where
convertSuccess = show
instance ConvertAttempt [Char] Int where
convertAttempt = SF.read
instance ConvertSuccess (Ratio Integer) [Char] where
convertSuccess = show
instance ConvertAttempt [Char] (Ratio Integer) where
convertAttempt = SF.read
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 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