module HaskellWorks.Data.Xml.DecodeResult where
import Control.Applicative
import HaskellWorks.Data.Xml.DecodeError
data DecodeResult a
= DecodeOk a
| DecodeFailed DecodeError
deriving (Eq, Show, Functor)
instance Applicative DecodeResult where
pure = DecodeOk
(<*>) (DecodeOk f ) (DecodeOk a) = DecodeOk (f a)
(<*>) (DecodeOk _ ) (DecodeFailed e) = DecodeFailed e
(<*>) (DecodeFailed e) _ = DecodeFailed e
instance Monad DecodeResult where
return = DecodeOk
(>>=) (DecodeOk a) f = f a
(>>=) (DecodeFailed e) _ = DecodeFailed e
instance Alternative DecodeResult where
empty = DecodeFailed (DecodeError "Failed decode")
(<|>) (DecodeOk a) _ = DecodeOk a
(<|>) _ (DecodeOk b) = DecodeOk b
(<|>) _ (DecodeFailed e) = DecodeFailed e
instance Foldable DecodeResult where
foldr f z (DecodeOk a) = f a z
foldr _ z (DecodeFailed _) = z
toEither :: DecodeResult a -> Either DecodeError a
toEither (DecodeOk a) = Right a
toEither (DecodeFailed e) = Left e
isOk :: DecodeResult a -> Bool
isOk (DecodeOk _) = True
isOk _ = False
isFailed :: DecodeResult a -> Bool
isFailed (DecodeFailed _) = True
isFailed _ = False