From ad1432e0ee2f1d9569e87a1dbd2b9f8c698f48b0 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Wed, 9 Nov 2022 10:48:24 +0000 Subject: [PATCH] core: make parsing independent of the order (#1332) * core: make parsing independent of the order * test * fix Co-authored-by: JRoberts <8711996+jr-simplex@users.noreply.github.com> --- src/Simplex/Chat.hs | 8 ++++---- tests/ChatTests.hs | 5 +++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Simplex/Chat.hs b/src/Simplex/Chat.hs index 70efeee0d7..f69bdbbc2e 100644 --- a/src/Simplex/Chat.hs +++ b/src/Simplex/Chat.hs @@ -27,7 +27,6 @@ import Data.Bifunctor (bimap, first) import qualified Data.ByteString.Base64 as B64 import Data.ByteString.Char8 (ByteString) import qualified Data.ByteString.Char8 as B -import Data.Char (isSpace) import Data.Either (fromRight) import Data.Fixed (div') import Data.Functor (($>)) @@ -64,7 +63,7 @@ import Simplex.Messaging.Client (defaultNetworkConfig) import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Encoding import Simplex.Messaging.Encoding.String -import Simplex.Messaging.Parsers (base64P, parseAll) +import Simplex.Messaging.Parsers (base64P) import Simplex.Messaging.Protocol (ErrorType (..), MsgBody, MsgFlags (..), NtfServer) import qualified Simplex.Messaging.Protocol as SMP import qualified Simplex.Messaging.TMap as TM @@ -219,7 +218,7 @@ execChatCommand s = case parseChatCommand s of Right cmd -> either CRChatCmdError id <$> runExceptT (processChatCommand cmd) parseChatCommand :: ByteString -> Either String ChatCommand -parseChatCommand = parseAll chatCommandP . B.dropWhileEnd isSpace +parseChatCommand = A.parseOnly chatCommandP toView :: ChatMonad m => ChatResponse -> m () toView event = do @@ -3138,7 +3137,7 @@ withStore action = do chatCommandP :: Parser ChatCommand chatCommandP = - A.choice + choice [ "/mute " *> ((`ShowMessages` False) <$> chatNameP'), "/unmute " *> ((`ShowMessages` True) <$> chatNameP'), ("/user " <|> "/u ") *> (CreateActiveUser <$> userProfile), @@ -3280,6 +3279,7 @@ chatCommandP = "/debug locks" $> DebugLocks ] where + choice = A.choice . map (\p -> p <* A.takeWhile (== ' ') <* A.endOfInput) imagePrefix = (<>) <$> "data:" <*> ("image/png;base64," <|> "image/jpg;base64,") imageP = safeDecodeUtf8 <$> ((<>) <$> imagePrefix <*> (B64.encode <$> base64P)) chatTypeP = A.char '@' $> CTDirect <|> A.char '#' $> CTGroup <|> A.char ':' $> CTContactConnection diff --git a/tests/ChatTests.hs b/tests/ChatTests.hs index 1b8b06ff9a..a5857531c9 100644 --- a/tests/ChatTests.hs +++ b/tests/ChatTests.hs @@ -3467,6 +3467,11 @@ testGroupLink = alice ##> "/show link #team" alice <## "no group link, to create: /create link #team" alice ##> "/create link #team" + _ <- getGroupLink alice "team" True + alice ##> "/delete link #team" + alice <## "Group link is deleted - joined members will remain connected." + alice <## "To create a new group link use /create link #team" + alice ##> "/create link #team" gLink <- getGroupLink alice "team" True alice ##> "/show link #team" _ <- getGroupLink alice "team" False