module Hemokit.Internal.Utils
( withJustM
, untilNothing
, textBase64
) where
import qualified Data.ByteString as BS
import qualified Data.ByteString.Base64 as Base64
import Data.Text (Text)
import qualified Data.Text.Encoding as T
withJustM :: (Monad m) => m (Maybe a) -> (a -> m ()) -> m Bool
withJustM act f = act >>= maybe (return False) (\x -> f x >> return True)
untilNothing :: (Monad m) => m (Maybe a) -> (a -> m ()) -> m Bool
untilNothing act f = act `withJustM` (\x -> f x >> again)
where
again = untilNothing act f >> return ()
textBase64 :: BS.ByteString -> Text
textBase64 bs = case T.decodeUtf8' (Base64.encode bs) of
Left ex -> error $ "textBase64: BUG: base64 encoding cannot be encoded: " ++ show ex
Right t -> t