{-# LANGUAGE MultiParamTypeClasses,
TypeSynonymInstances,
FlexibleInstances,
FunctionalDependencies #-}
module Test.Hspec.Attoparsec.Source where
import qualified Data.Attoparsec.ByteString as AB
import qualified Data.ByteString as B
import qualified Data.Attoparsec.ByteString.Lazy as ALB
import qualified Data.ByteString.Lazy as LB
import qualified Data.Attoparsec.Text as AT
import qualified Data.Text as T
import qualified Data.Attoparsec.Text.Lazy as ALT
import qualified Data.Text.Lazy as LT
import qualified Data.Attoparsec.Types as Atto
import Data.String (IsString)
class (Eq string, Show string, IsString string)
=> Source parser string string' result | string -> parser, string -> result, string -> string' where
(~>) :: string -> parser string' a -> Either String a
(~?>) :: string -> parser string' a -> result a
instance Source Atto.Parser B.ByteString B.ByteString AB.Result where
ByteString
t ~> :: forall a. ByteString -> Parser ByteString a -> Either String a
~> Parser ByteString a
p = Parser ByteString a -> ByteString -> Either String a
forall a. Parser a -> ByteString -> Either String a
AB.parseOnly Parser ByteString a
p ByteString
t
ByteString
t ~?> :: forall a. ByteString -> Parser ByteString a -> Result a
~?> Parser ByteString a
p = Parser ByteString a -> ByteString -> Result a
forall a. Parser a -> ByteString -> Result a
AB.parse Parser ByteString a
p ByteString
t
instance Source Atto.Parser LB.ByteString B.ByteString ALB.Result where
ByteString
t ~> :: forall a. ByteString -> Parser ByteString a -> Either String a
~> Parser ByteString a
p = Result a -> Either String a
forall r. Result r -> Either String r
ALB.eitherResult (Result a -> Either String a) -> Result a -> Either String a
forall a b. (a -> b) -> a -> b
$ ByteString
t ByteString -> Parser ByteString a -> Result a
forall (parser :: * -> * -> *) string string' (result :: * -> *) a.
Source parser string string' result =>
string -> parser string' a -> result a
~?> Parser ByteString a
p
ByteString
t ~?> :: forall a. ByteString -> Parser ByteString a -> Result a
~?> Parser ByteString a
p = Parser ByteString a -> ByteString -> Result a
forall a. Parser a -> ByteString -> Result a
ALB.parse Parser ByteString a
p ByteString
t
instance Source Atto.Parser T.Text T.Text AT.Result where
Text
t ~> :: forall a. Text -> Parser Text a -> Either String a
~> Parser Text a
p = Parser Text a -> Text -> Either String a
forall a. Parser a -> Text -> Either String a
AT.parseOnly Parser Text a
p Text
t
Text
t ~?> :: forall a. Text -> Parser Text a -> Result a
~?> Parser Text a
p = Parser Text a -> Text -> Result a
forall a. Parser a -> Text -> Result a
AT.parse Parser Text a
p Text
t
instance Source Atto.Parser LT.Text T.Text ALT.Result where
Text
t ~> :: forall a. Text -> Parser Text a -> Either String a
~> Parser Text a
p = Result a -> Either String a
forall r. Result r -> Either String r
ALT.eitherResult (Result a -> Either String a) -> Result a -> Either String a
forall a b. (a -> b) -> a -> b
$ Text
t Text -> Parser Text a -> Result a
forall (parser :: * -> * -> *) string string' (result :: * -> *) a.
Source parser string string' result =>
string -> parser string' a -> result a
~?> Parser Text a
p
Text
t ~?> :: forall a. Text -> Parser Text a -> Result a
~?> Parser Text a
p = Parser Text a -> Text -> Result a
forall a. Parser a -> Text -> Result a
ALT.parse Parser Text a
p Text
t
class Leftover r s | r -> s where
leftover :: r a -> Maybe s
instance Leftover AB.Result B.ByteString where
leftover :: forall a. Result a -> Maybe ByteString
leftover (AB.Done ByteString
t a
_) = ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just ByteString
t
leftover IResult ByteString a
_ = Maybe ByteString
forall a. Maybe a
Nothing
instance Leftover ALB.Result LB.ByteString where
leftover :: forall a. Result a -> Maybe ByteString
leftover (ALB.Done ByteString
t a
_) = ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just ByteString
t
leftover Result a
_ = Maybe ByteString
forall a. Maybe a
Nothing
instance Leftover AT.Result T.Text where
leftover :: forall a. Result a -> Maybe Text
leftover (AT.Done Text
t a
_) = Text -> Maybe Text
forall a. a -> Maybe a
Just Text
t
leftover IResult Text a
_ = Maybe Text
forall a. Maybe a
Nothing
instance Leftover ALT.Result LT.Text where
leftover :: forall a. Result a -> Maybe Text
leftover (ALT.Done Text
t a
_) = Text -> Maybe Text
forall a. a -> Maybe a
Just Text
t
leftover Result a
_ = Maybe Text
forall a. Maybe a
Nothing