refactored readLoop

This commit is contained in:
Jonathan Lamothe 2021-11-21 02:22:40 -05:00
parent 440aee7536
commit 61fca70a5e

View File

@ -40,6 +40,7 @@ module Network.Gemini.Capsule.Internal (
stripCRLF
) where
import Control.Monad (when)
import Control.Monad.Trans.Class (lift)
import Control.Monad.Trans.Maybe (MaybeT (..))
import qualified Data.ByteString as BS
@ -119,9 +120,10 @@ readLoop :: Int -> S.InputStream BS.ByteString -> MaybeT IO Builder
readLoop maxLen src =
lift (S.read src) >>= \case
Nothing -> return mempty
Just bs -> let len = BS.length bs in
if len > maxLen
then MaybeT $ return Nothing
else (byteString bs <>) <$> readLoop (maxLen - len) src
Just bs -> do
let len = BS.length bs
when (len > maxLen) $
MaybeT $ return Nothing
(byteString bs <>) <$> readLoop (maxLen - len) src
--jl