packages feed

packstream-bolt-0.1.0.0: src/Data/PackStream/Result.hs

-- | Internal module. Not part of the public API.
module Data.PackStream.Result
    ( Result(..)
    ) where

import           Compat.Prelude
import qualified Control.Monad.Fail as Fail
import           Data.Kind (Type)
import Data.Text

-- | The result of decoding from PackStream
type Result :: Type -> Type
type role Result representational
data Result a = Error Text
              | Success a
              deriving stock (Eq, Show, Functor, Generic, Foldable, Traversable)

instance NFData a => NFData (Result a) where
  rnf (Error e)   = rnf e
  rnf (Success a) = rnf a

instance Applicative Result where
  pure = Success
  (<*>) = ap

instance Monad Result where
  Success a >>= m = m a
  Error err >>= _ = Error err

instance Fail.MonadFail Result where
  fail str = Error $ pack str

instance Alternative Result where
  empty = fail "Alternative(empty)"
  a@(Success _) <|> _ = a
  _              <|> b = b