{-# LANGUAGE TemplateHaskell #-} -- | Command context typeclass module CalamityCommands.Context ( CommandContext (..), ConstructContext (..), constructContext, BasicContext (..), useBasicContext, ) where import qualified Data.Text.Lazy as L import GHC.Generics (Generic) import qualified Polysemy as P import CalamityCommands.Command class CommandContext m c a | c -> m, c -> a where -- | The prefix that was used to invoke the command ctxPrefix :: c -> L.Text -- | The command that was invoked ctxCommand :: c -> Command m c a -- | The message remaining after consuming the prefix ctxUnparsedParams :: c -> L.Text -- | An effect for constructing the context for a command data ConstructContext msg ctx m' a' m a where -- | Construct a context for a command invokation, returning Just @context@ on -- success, or Nothing if a context could not be constructed ConstructContext :: -- | The (prefix, command, remaining) (L.Text, Command m' ctx a', L.Text) -> -- | The message type to extract the context from msg -> ConstructContext msg ctx m' a' m (Maybe ctx) P.makeSem ''ConstructContext -- | A basic context that only knows the prefix used and the unparsed input data BasicContext m a = BasicContext { bcPrefix :: L.Text , bcCommand :: Command m (BasicContext m a) a , bcUnparsedParams :: L.Text } deriving (Show, Generic) instance CommandContext m (BasicContext m a) a where ctxPrefix = bcPrefix ctxCommand = bcCommand ctxUnparsedParams = bcUnparsedParams -- | A default interpretation for 'ConstructContext' that constructs a BasicContext useBasicContext :: P.Sem (ConstructContext msg (BasicContext m a') m a' ': r) a -> P.Sem r a useBasicContext = P.interpret ( \case ConstructContext (pre, cmd, up) _ -> pure . Just $ BasicContext pre cmd up )