{-# Language ExistentialQuantification #-}
module Client.Commands.Types where
import Client.Commands.Arguments.Spec (Args)
import Client.State (ClientState, clientErrorMsg, clientConnection)
import Client.State.Network (NetworkState, csNetwork)
import Control.Lens (set, view)
import Data.List.NonEmpty (NonEmpty)
import Data.Text (Text)
import Irc.Identifier (Identifier)
import LensUtils (setStrict)
data CommandResult
= CommandSuccess ClientState
| CommandFailure ClientState
| CommandQuit ClientState
type ClientCommand a = ClientState -> a -> IO CommandResult
type NetworkCommand a = NetworkState -> ClientCommand a
type ChannelCommand a = Identifier -> NetworkCommand a
data CommandImpl a
= ClientCommand (ClientCommand a) (Bool -> ClientCommand String)
| NetworkCommand (NetworkCommand a) (Bool -> NetworkCommand String)
| ChatCommand (ChannelCommand a) (Bool -> ChannelCommand String)
| ChannelCommand (ChannelCommand a) (Bool -> ChannelCommand String)
data Command = forall a. Command
{
Command -> NonEmpty Text
cmdNames :: NonEmpty Text
, ()
cmdArgumentSpec :: Args ClientState a
, Command -> Text
cmdDocumentation :: Text
, ()
cmdImplementation :: CommandImpl a
}
data CommandSection = CommandSection
{ CommandSection -> Text
cmdSectionName :: Text
, CommandSection -> [Command]
cmdSectionCmds :: [Command]
}
commandSuccess :: Monad m => ClientState -> m CommandResult
commandSuccess :: forall (m :: * -> *). Monad m => ClientState -> m CommandResult
commandSuccess = forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. ClientState -> CommandResult
CommandSuccess
commandSuccessUpdateCS :: NetworkState -> ClientState -> IO CommandResult
commandSuccessUpdateCS :: NetworkState -> ClientState -> IO CommandResult
commandSuccessUpdateCS NetworkState
cs ClientState
st =
do let network :: Text
network = forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Lens' NetworkState Text
csNetwork NetworkState
cs
forall (m :: * -> *). Monad m => ClientState -> m CommandResult
commandSuccess
forall a b. (a -> b) -> a -> b
$ forall s t a b. ASetter s t a b -> b -> s -> t
setStrict (forall (f :: * -> *).
Applicative f =>
Text -> LensLike' f ClientState NetworkState
clientConnection Text
network) NetworkState
cs ClientState
st
commandFailure :: Monad m => ClientState -> m CommandResult
commandFailure :: forall (m :: * -> *). Monad m => ClientState -> m CommandResult
commandFailure = forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. ClientState -> CommandResult
CommandFailure
commandFailureMsg :: Text -> ClientState -> IO CommandResult
commandFailureMsg :: Text -> ClientState -> IO CommandResult
commandFailureMsg Text
e ClientState
st =
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! ClientState -> CommandResult
CommandFailure forall a b. (a -> b) -> a -> b
$! forall s t a b. ASetter s t a b -> b -> s -> t
set Lens' ClientState (Maybe Text)
clientErrorMsg (forall a. a -> Maybe a
Just Text
e) ClientState
st