-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
46 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,72 @@ | ||
-- | A Bot Behavior for managing todo lists | ||
-- | A Bot Behavior for managing lists | ||
module CofreeBot.Bot.Behaviors.Lists | ||
( listBot | ||
, TodoAction(..) | ||
( listsBot | ||
) where | ||
|
||
-------------------------------------------------------------------------------- | ||
|
||
import CofreeBot.Bot | ||
import CofreeBot.Utils ( indistinct ) | ||
import Control.Applicative | ||
import Data.Attoparsec.ByteString.Char8 | ||
( isSpace ) | ||
import Data.Attoparsec.Text | ||
import qualified Data.IntMap.Strict as IntMap | ||
import Data.IntMap.Strict ( IntMap ) | ||
import qualified Data.Map.Strict as Map | ||
import Data.Map.Strict ( Map ) | ||
import Data.Profunctor | ||
import qualified Data.Text as T | ||
|
||
-------------------------------------------------------------------------------- | ||
|
||
data TodoAction = Create T.Text | Modify Int T.Text | Remove Int | List | ||
data ListItemAction = Insert T.Text | Modify Int T.Text | Remove Int | ||
|
||
listBot' :: Monad m => Bot m (IntMap T.Text) TodoAction T.Text | ||
listBot' = Bot $ \s -> \case | ||
Create todo -> | ||
data ListAction = CreateList T.Text | ModifyList T.Text ListItemAction | RemoveList T.Text | List T.Text | ||
|
||
listItemBot :: Monad m => Bot m (IntMap T.Text) ListItemAction T.Text | ||
listItemBot = Bot $ \s -> \case | ||
Insert todo -> | ||
let k = freshKey s in pure ("Entry added", IntMap.insert k todo s) | ||
Modify k todo -> pure ("Entry updated", IntMap.insert k todo s) | ||
Remove k -> pure ("Entry deleted", IntMap.delete k s) | ||
List -> pure (T.pack $ show s, s) | ||
|
||
freshKey :: IntMap a -> Int | ||
freshKey state = case IntMap.lookupMax state of | ||
Nothing -> 0 | ||
Just (k, _) -> k + 1 | ||
|
||
listBot :: Monad m => Bot m (IntMap T.Text) T.Text T.Text | ||
listBot = dimap (parseOnly parseAction) indistinct $ emptyBot \/ listBot' | ||
listsBot' :: Monad m => Bot m (Map T.Text (IntMap T.Text)) ListAction T.Text | ||
listsBot' = Bot $ \s -> \case | ||
CreateList name -> pure ("List Created", Map.insert name mempty s) | ||
ModifyList name action -> do | ||
case Map.lookup name s of | ||
Nothing -> do | ||
let t = IntMap.empty | ||
(_, t') <- runBot listItemBot t action | ||
pure ("List Created", Map.insert name t' s) | ||
Just t -> do | ||
(_, t') <- runBot listItemBot t action | ||
pure ("Entry Modified", Map.insert name t' s) | ||
RemoveList name -> pure ("List deleted", Map.delete name s) | ||
List name -> pure (T.pack $ show $ Map.lookup name s, s) | ||
|
||
listsBot :: Monad m => Bot m (Map T.Text (IntMap T.Text)) T.Text T.Text | ||
listsBot = dimap (parseOnly parseListAction) indistinct $ emptyBot \/ listsBot' | ||
|
||
parseAction :: Parser TodoAction | ||
parseAction = parseAdd <|> parseRemove <|> parseModify <|> parseList | ||
parseListAction :: Parser ListAction | ||
parseListAction = | ||
parseCreateList <|> parseModifyList <|> parseRemoveList <|> parseListList | ||
where | ||
parseAdd = "create-todo:" *> skipSpace *> fmap Create takeText | ||
parseRemove = "remove-todo:" *> skipSpace *> fmap Remove decimal | ||
parseModify = | ||
"modify-todo:" *> skipSpace *> fmap (Modify) decimal <* space <*> takeText | ||
parseList = "list-todos" *> pure List | ||
parseCreateList = "create" *> skipSpace *> fmap CreateList (takeTill isSpace) | ||
parseModifyList = | ||
"add" | ||
*> skipSpace | ||
*> fmap ModifyList (takeTill isSpace) | ||
<*> fmap Insert takeText | ||
parseRemoveList = | ||
"remove" | ||
*> skipSpace | ||
*> fmap ModifyList (takeTill isSpace) | ||
<*> fmap Remove decimal | ||
parseListList = "show" *> skipSpace *> fmap List takeText |