Permalink
Browse files

Add numeric FromField instances

  • Loading branch information...
1 parent 6b5430a commit dc86d42eb6377ef22e60018b7418928ab2778832 @tibbe tibbe committed Mar 24, 2012
Showing with 74 additions and 4 deletions.
  1. +74 −4 Data/Ceason.hs
View
@@ -22,15 +22,19 @@ module Data.Ceason
) where
import Control.Applicative
-import Data.Attoparsec.Char8 (number, parseOnly)
+import Data.Attoparsec.Char8 (double, number, parseOnly)
import qualified Data.ByteString as S
+import qualified Data.ByteString.Char8 as B8
import qualified Data.ByteString.Lazy as L
+import Data.Int
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Data.Text.Lazy as LT
import Data.Traversable
import Data.Vector (Vector, (!))
import qualified Data.Vector as V
+import Data.Word
+import GHC.Float (double2Float)
import Prelude hiding (takeWhile)
import Data.Ceason.Parser.Internal
@@ -189,20 +193,86 @@ instance FromField a => FromRecord (V.Vector a) where
class FromField a where
parseField :: Field -> Parser a
+instance FromField Char where
+ parseField s
+ | T.compareLength t 1 == EQ = pure (T.head t)
+ | otherwise = fail $ "when expecting a Char, encountered \"" ++
+ B8.unpack s ++ "\" instead"
+ where t = T.decodeUtf8 s
+ {-# INLINE parseField #-}
+
+instance FromField Double where
+ parseField = parseDouble
+ {-# INLINE parseField #-}
+
+instance FromField Float where
+ parseField s = double2Float <$> parseDouble s
+ {-# INLINE parseField #-}
+
+parseDouble :: S.ByteString -> Parser Double
+parseDouble s = case parseOnly double s of
+ Left err -> fail err
+ Right n -> pure n
+{-# INLINE parseDouble #-}
+
+instance FromField Int where
+ parseField = parseIntegral
+ {-# INLINE parseField #-}
+
+instance FromField Integer where
+ parseField = parseIntegral
+ {-# INLINE parseField #-}
+
+instance FromField Int8 where
+ parseField = parseIntegral
+ {-# INLINE parseField #-}
+
+instance FromField Int16 where
+ parseField = parseIntegral
+ {-# INLINE parseField #-}
+
+instance FromField Int32 where
+ parseField = parseIntegral
+ {-# INLINE parseField #-}
+
+instance FromField Int64 where
+ parseField = parseIntegral
+ {-# INLINE parseField #-}
+
+instance FromField Word where
+ parseField = parseIntegral
+ {-# INLINE parseField #-}
+
+instance FromField Word8 where
+ parseField = parseIntegral
+ {-# INLINE parseField #-}
+
+instance FromField Word16 where
+ parseField = parseIntegral
+ {-# INLINE parseField #-}
+
+instance FromField Word32 where
+ parseField = parseIntegral
+ {-# INLINE parseField #-}
+
+instance FromField Word64 where
+ parseField = parseIntegral
+ {-# INLINE parseField #-}
+
instance FromField S.ByteString where
parseField = pure
+ {-# INLINE parseField #-}
instance FromField L.ByteString where
parseField s = pure (L.fromChunks [s])
+ {-# INLINE parseField #-}
instance FromField T.Text where
parseField = pure . T.decodeUtf8
+ {-# INLINE parseField #-}
instance FromField LT.Text where
parseField s = pure (LT.fromChunks [T.decodeUtf8 s])
-
-instance FromField Int where
- parseField = parseIntegral
{-# INLINE parseField #-}
parseIntegral :: Integral a => S.ByteString -> Parser a

0 comments on commit dc86d42

Please sign in to comment.