calamity-commands-0.1.0.0: A library for declaring, parsing, and invoking text-input based commands
Safe HaskellNone
LanguageHaskell2010

CalamityCommands.CommandUtils

Description

Command utilities

Synopsis

Documentation

type TypedCommandC ps a r = (ApplyTupRes (ParserResult (ListToTup ps)) (CommandSemType r a) ~ CommandForParsers ps r a, ParameterParser (ListToTup ps) r, ApplyTup (ParserResult (ListToTup ps)) (CommandSemType r a), ParamNamesForParsers ps r) Source #

Some constraints used for making parameter typed commands work

type family CommandForParsers (ps :: [Type]) r a where ... Source #

Transform a type level list of types implementing the Parser typeclass into the type a command callback matching those parameters should be.

As an example:

CommandForParsers [ Text, Int, Named "something" Text ] r a ~
  (Text -> Int -> Text -> Sem r (Fail ': r) a)

Equations

CommandForParsers '[] r a = Sem (Fail ': r) a 
CommandForParsers (x ': xs) r a = ParserResult x -> CommandForParsers xs r a 

buildCommand Source #

Arguments

:: forall ps c m a r. (Monad m, Member (Final m) r, TypedCommandC ps a r, CommandContext m c a) 
=> NonEmpty Text

The name (and aliases) of the command

-> Maybe (Group m c a)

The parent group of the command

-> Bool

If the command is hidden

-> [Check m c]

The checks for the command

-> (c -> Text)

The help generator for this command

-> (c -> CommandForParsers ps r a)

The callback foor this command

-> Sem r (Command m c a) 

Given the properties of a Command, a callback, and a type level list of the parameters, build a command by constructing a parser and wiring it up to the callback.

Examples

Building a command that adds two numbers.

buildCommand @'[Named "a" Int, Named "b" Int]
   "add" Nothing [] (const "Add two integers") $ \ctx a b ->
     pure $ Right (a + b)

buildCommand' Source #

Arguments

:: forall c m a p r. (Monad m, Member (Final m) r) 
=> NonEmpty Text

The name (and aliases) of the command

-> Maybe (Group m c a)

The parent group of the command

-> Bool

If the command is hidden

-> [Check m c]

The checks for the command

-> [Text]

The names of the command's parameters

-> (c -> Text)

The help generator for this command

-> (c -> Sem r (Either CommandError p))

The parser for this command

-> ((c, p) -> Sem (Fail ': r) a)

The callback for this command

-> Sem r (Command m c a) 

Given the properties of a Command with the parser and callback in the Sem monad, build a command by transforming the Polysemy actions into m actions.

buildParser :: (Monad m, Member (Final m) r) => Text -> (c -> Sem r (Either CommandError a)) -> Sem r (c -> m (Either CommandError a)) Source #

Given the name of the command the parser is for and a parser function in the Sem monad, build a parser by transforming the Polysemy action into an m action.

buildCallback :: (Monad m, Member (Final m) r) => ((c, p) -> Sem (Fail ': r) a) -> Sem r ((c, p) -> m (Either Text a)) Source #

Given a callback for a command in the Sem monad, build a command callback by transforming the Polysemy action into an m action.

runCommand :: (Monad m, Member (Embed m) r) => c -> Command m c a -> Sem r (Either CommandError a) Source #

Given an invokation Context c, run a command. This does not perform the command's checks.

invokeCommand :: (Monad m, Member (Embed m) r) => c -> Command m c a -> Sem r (Either CommandError a) Source #

Given an invokation Context c, first run all of the command's checks, then run the command if they all pass.

groupPath :: Group m c a -> [Text] Source #

commandParams :: Command m c a -> Text Source #

Format a command's parameters