Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

failure 0.1.0

  • Loading branch information...
commit 8156fa84766f9a78368c74bd98f5d4b3d22bfd2e 1 parent 5d0ad19
@snoyberg authored
View
2  Data/Convertible/Base.hs
@@ -95,7 +95,7 @@ instance Exception ConversionException
'ConversionException'
-}
convertAttemptWrap :: (ConvertAttempt a b,
- MonadFailure ConversionException m
+ Failure ConversionException m
)
=> a
-> m b
View
29 Data/Convertible/Instances/String.hs
@@ -31,10 +31,9 @@ module Data.Convertible.Instances.String
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 Control.Monad ((<=<), unless)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BL
@@ -95,10 +94,10 @@ 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
+ unless (length s == 10) $ failure $ InvalidDayException s
+ y <- ca $ take 4 s
+ m <- ca $ take 2 $ drop 5 s
+ d <- ca $ take 2 $ drop 8 s
return $ fromGregorian y m d
-- Bool
@@ -142,7 +141,13 @@ instance ConvertAttempt [Char] Bool where
instance ConvertSuccess Int [Char] where
convertSuccess = show
instance ConvertAttempt [Char] Int where
- convertAttempt = SF.read
+ convertAttempt = readMsg "Invalid Int"
+
+-- Integer
+instance ConvertSuccess Integer [Char] where
+ convertSuccess = show
+instance ConvertAttempt [Char] Integer where
+ convertAttempt = readMsg "Invalid Int"
-- Rational
instance ConvertSuccess Rational [Char] where
@@ -150,7 +155,15 @@ instance ConvertSuccess Rational [Char] where
| denominator r == 1 = show $ numerator r
| otherwise = show $ (fromRational r :: Double)
instance ConvertAttempt [Char] Rational where
- convertAttempt = fmap realToFrac . (SF.read :: String -> Attempt Double)
+ convertAttempt = fmap realToFrac . readDouble
+
+readDouble :: String -> Attempt Double
+readDouble = readMsg "Invalid double"
+
+readMsg :: Read a => String -> String -> Attempt a
+readMsg msg s = case reads s of
+ (x, _):_ -> Success x
+ _ -> failureString $ msg ++ ": " ++ s
#if TEST
propRationalId :: Rational -> Bool
View
5 convertible-text.cabal
@@ -1,5 +1,5 @@
Name: convertible-text
-Version: 0.2.0.5
+Version: 0.3.0
License: LGPL
Maintainer: Michael Snoyman <michael@snoyman.com>
Author: John Goerzen, Michael Snoyman
@@ -43,8 +43,7 @@ library
containers >= 0.2.0.1 && < 0.4,
text >= 0.5 && < 0.8,
bytestring >= 0.9.1.4 && < 0.10,
- safe-failure >= 0.4 && < 0.5,
- attempt >= 0.2.1 && < 0.3,
+ attempt >= 0.3.0 && < 0.4,
template-haskell
if flag(time_gte_113)
Build-Depends: time>=1.1.3 && <= 1.1.4
Please sign in to comment.
Something went wrong with that request. Please try again.