@@ -772,9 +772,11 @@ class FromField a where
772772genericParseField
773773 :: forall a rep meta . (Generic a , GFromField rep , Rep a ~ D1 meta rep , Datatype meta )
774774 => Options -> Field -> Parser a
775- genericParseField opts field = fmap (to . M1 ) (gParseField opts onFail field)
775+ genericParseField opts field = Parser $ \ onFailure onSuccess ->
776+ unParser (gParseField opts field) (\ _ -> onFailure err) (onSuccess . to . M1 )
776777 where
777- onFail _ = fail $ " Can't parseField of type " <> datatypeName (Proxy :: Proxy meta d f )
778+ err = " Can't parseField of type " <> datatypeName (Proxy :: Proxy meta d f )
779+ <> " from " <> show field
778780{-# INLINE genericParseField #-}
779781
780782-- | A type that can be converted to a single CSV field.
@@ -1396,12 +1398,14 @@ instance (ToField a, Selector s) => GToRecord (M1 S s (K1 i a)) (B.ByteString, B
13961398 name = T. encodeUtf8 (T. pack (fieldLabelModifier opts (selName m)))
13971399
13981400class GFromField f where
1399- gParseField :: Options -> ( Field -> Parser ( f p )) -> Field -> Parser (f p )
1401+ gParseField :: Options -> Field -> Parser (f p )
14001402
14011403-- Type with single nullary constructor
14021404instance (Constructor c ) => GFromField (C1 c U1 ) where
1403- gParseField opts onFail field = do
1404- if field == expected then pure val else onFail field
1405+ gParseField opts field = Parser $ \ onFailure onSuccess ->
1406+ if field == expected
1407+ then onSuccess val
1408+ else onFailure $ " Can't parse " <> show expected <> " from " <> show field
14051409 where
14061410 expected = encodeConstructor opts val
14071411 val :: C1 c U1 p
@@ -1410,18 +1414,15 @@ instance (Constructor c) => GFromField (C1 c U1) where
14101414
14111415-- Type with single unary constructor
14121416instance (FromField a ) => GFromField (C1 c (S1 meta (K1 i a ))) where
1413- gParseField _ onFail field =
1414- fmap (M1 . M1 . K1 ) (parseField field) <|> onFail field
1417+ gParseField _opts = fmap (M1 . M1 . K1 ) . parseField
14151418 {-# INLINE gParseField #-}
14161419
14171420-- Sum type
14181421instance (GFromField c1 , GFromField c2 ) => GFromField (c1 :+: c2 ) where
1419- gParseField opts onFail field =
1420- case runParser $ gParseField opts mempty field of
1421- Left _ -> case runParser $ gParseField opts mempty field of
1422- Left _ -> onFail field
1423- Right res -> pure $ R1 res
1424- Right res -> pure $ L1 res
1422+ gParseField opts field = Parser $ \ onFailure onSuccess ->
1423+ unParser (gParseField opts field)
1424+ (\ _ -> unParser (gParseField opts field) onFailure $ onSuccess . R1 )
1425+ (onSuccess . L1 )
14251426 {-# INLINE gParseField #-}
14261427
14271428class GToField f where
0 commit comments