{-# LANGUAGE CPP #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
module GHCup.Prelude.Internal where
import Control.Applicative
import Control.Exception.Safe
import Control.Monad
import Control.Monad.IO.Class
import Control.Monad.Reader
import Data.Bifunctor
import Data.ByteString ( ByteString )
import Data.List ( intercalate, stripPrefix, isPrefixOf, dropWhileEnd )
import Data.Maybe
import Data.String
import Data.Text ( Text )
import Data.Versions
import Data.Word8 hiding ( isDigit )
import Haskus.Utils.Types.List
import Haskus.Utils.Variant.Excepts
import System.IO.Error
import Control.Retry
import GHC.IO.Exception
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as L
import qualified Data.Strict.Maybe as S
import qualified Data.List.Split as Split
import qualified Data.Text as T
import qualified Data.Text.Encoding as E
import qualified Data.Text.Encoding.Error as E
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Builder as B
import qualified Data.Text.Lazy.Builder.Int as B
import qualified Data.Text.Lazy.Encoding as TLE
fS :: IsString a => String -> a
fS :: forall a. IsString a => String -> a
fS = forall a. IsString a => String -> a
fromString
fromStrictMaybe :: S.Maybe a -> Maybe a
fromStrictMaybe :: forall a. Maybe a -> Maybe a
fromStrictMaybe = forall b a. b -> (a -> b) -> Maybe a -> b
S.maybe forall a. Maybe a
Nothing forall a. a -> Maybe a
Just
fSM :: S.Maybe a -> Maybe a
fSM :: forall a. Maybe a -> Maybe a
fSM = forall a. Maybe a -> Maybe a
fromStrictMaybe
toStrictMaybe :: Maybe a -> S.Maybe a
toStrictMaybe :: forall a. Maybe a -> Maybe a
toStrictMaybe = forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Maybe a
S.Nothing forall a. a -> Maybe a
S.Just
tSM :: Maybe a -> S.Maybe a
tSM :: forall a. Maybe a -> Maybe a
tSM = forall a. Maybe a -> Maybe a
toStrictMaybe
internalError :: String -> IO a
internalError :: forall a. String -> IO a
internalError = forall (m :: * -> *) a. MonadFail m => String -> m a
fail forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
"Internal error: " forall a. Semigroup a => a -> a -> a
<>)
iE :: String -> IO a
iE :: forall a. String -> IO a
iE = forall a. String -> IO a
internalError
showT :: Show a => a -> Text
showT :: forall a. Show a => a -> Text
showT = forall a. IsString a => String -> a
fS forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show
whenM :: Monad m => m Bool -> m () -> m ()
whenM :: forall (m :: * -> *). Monad m => m Bool -> m () -> m ()
whenM ~m Bool
b ~m ()
t = forall (m :: * -> *) a. Monad m => m Bool -> m a -> m a -> m a
ifM m Bool
b m ()
t (forall (m :: * -> *) a. Monad m => a -> m a
return ())
unlessM :: Monad m => m Bool -> m () -> m ()
unlessM :: forall (m :: * -> *). Monad m => m Bool -> m () -> m ()
unlessM ~m Bool
b ~m ()
f = forall (m :: * -> *) a. Monad m => m Bool -> m a -> m a -> m a
ifM m Bool
b (forall (m :: * -> *) a. Monad m => a -> m a
return ()) m ()
f
ifM :: Monad m => m Bool -> m a -> m a -> m a
ifM :: forall (m :: * -> *) a. Monad m => m Bool -> m a -> m a -> m a
ifM ~m Bool
b ~m a
t ~m a
f = do
Bool
b' <- m Bool
b
if Bool
b' then m a
t else m a
f
whileM :: Monad m => m a -> (a -> m Bool) -> m a
whileM :: forall (m :: * -> *) a. Monad m => m a -> (a -> m Bool) -> m a
whileM ~m a
action ~a -> m Bool
f = do
a
a <- m a
action
Bool
b' <- a -> m Bool
f a
a
if Bool
b' then forall (m :: * -> *) a. Monad m => m a -> (a -> m Bool) -> m a
whileM m a
action a -> m Bool
f else forall (f :: * -> *) a. Applicative f => a -> f a
pure a
a
whileM_ :: Monad m => m a -> (a -> m Bool) -> m ()
whileM_ :: forall (m :: * -> *) a. Monad m => m a -> (a -> m Bool) -> m ()
whileM_ ~m a
action = forall (f :: * -> *) a. Functor f => f a -> f ()
void forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. Monad m => m a -> (a -> m Bool) -> m a
whileM m a
action
guardM :: (Monad m, Alternative m) => m Bool -> m ()
guardM :: forall (m :: * -> *). (Monad m, Alternative m) => m Bool -> m ()
guardM ~m Bool
f = forall (f :: * -> *). Alternative f => Bool -> f ()
guard forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< m Bool
f
handleIO' :: (MonadIO m, MonadCatch m)
=> IOErrorType
-> (IOException -> m a)
-> m a
-> m a
handleIO' :: forall (m :: * -> *) a.
(MonadIO m, MonadCatch m) =>
IOErrorType -> (IOException -> m a) -> m a -> m a
handleIO' IOErrorType
err IOException -> m a
handler = forall (m :: * -> *) a.
MonadCatch m =>
(IOException -> m a) -> m a -> m a
handleIO
(\IOException
e -> if IOErrorType
err forall a. Eq a => a -> a -> Bool
== IOException -> IOErrorType
ioeGetErrorType IOException
e then IOException -> m a
handler IOException
e else forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. IOException -> IO a
ioError IOException
e)
(??) :: forall e es a m . (Monad m, e :< es) => Maybe a -> e -> Excepts es m a
?? :: forall e (es :: [*]) a (m :: * -> *).
(Monad m, e :< es) =>
Maybe a -> e -> Excepts es m a
(??) Maybe a
m e
e = forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall e (es :: [*]) a (m :: * -> *).
(Monad m, e :< es) =>
e -> Excepts es m a
throwE e
e) forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe a
m
(!?) :: forall e es a m
. (Monad m, e :< es)
=> m (Maybe a)
-> e
-> Excepts es m a
!? :: forall e (es :: [*]) a (m :: * -> *).
(Monad m, e :< es) =>
m (Maybe a) -> e -> Excepts es m a
(!?) m (Maybe a)
em e
e = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m (Maybe a)
em forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (forall e (es :: [*]) a (m :: * -> *).
(Monad m, e :< es) =>
Maybe a -> e -> Excepts es m a
?? e
e)
lE :: forall e es a m . (Monad m, e :< es) => Either e a -> Excepts es m a
lE :: forall e (es :: [*]) a (m :: * -> *).
(Monad m, e :< es) =>
Either e a -> Excepts es m a
lE = forall (es' :: [*]) (es :: [*]) a (m :: * -> *).
(Monad m, VEitherLift es es') =>
Excepts es m a -> Excepts es' m a
liftE forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) (es :: [*]) a.
Monad m =>
VEither es a -> Excepts es m a
veitherToExcepts forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. Either a b -> VEither '[a] b
fromEither
lE' :: forall e' e es a m
. (Monad m, e :< es)
=> (e' -> e)
-> Either e' a
-> Excepts es m a
lE' :: forall e' e (es :: [*]) a (m :: * -> *).
(Monad m, e :< es) =>
(e' -> e) -> Either e' a -> Excepts es m a
lE' e' -> e
f = forall (es' :: [*]) (es :: [*]) a (m :: * -> *).
(Monad m, VEitherLift es es') =>
Excepts es m a -> Excepts es' m a
liftE forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) (es :: [*]) a.
Monad m =>
VEither es a -> Excepts es m a
veitherToExcepts forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. Either a b -> VEither '[a] b
fromEither forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first e' -> e
f
lEM :: forall e es a m . (Monad m, e :< es) => m (Either e a) -> Excepts es m a
lEM :: forall e (es :: [*]) a (m :: * -> *).
(Monad m, e :< es) =>
m (Either e a) -> Excepts es m a
lEM m (Either e a)
em = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m (Either e a)
em forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall e (es :: [*]) a (m :: * -> *).
(Monad m, e :< es) =>
Either e a -> Excepts es m a
lE
lEM' :: forall e' e es a m
. (Monad m, e :< es)
=> (e' -> e)
-> m (Either e' a)
-> Excepts es m a
lEM' :: forall e' e (es :: [*]) a (m :: * -> *).
(Monad m, e :< es) =>
(e' -> e) -> m (Either e' a) -> Excepts es m a
lEM' e' -> e
f m (Either e' a)
em = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m (Either e' a)
em forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall e (es :: [*]) a (m :: * -> *).
(Monad m, e :< es) =>
Either e a -> Excepts es m a
lE forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first e' -> e
f
fromEither :: Either a b -> VEither '[a] b
fromEither :: forall a b. Either a b -> VEither '[a] b
fromEither = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall x (xs :: [*]). V xs -> VEither xs x
VLeft forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall c (cs :: [*]). (c :< cs) => c -> V cs
V) forall x (xs :: [*]). x -> VEither xs x
VRight
liftIOException' :: ( MonadCatch m
, MonadIO m
, Monad m
, e :< es'
, LiftVariant es es'
)
=> IOErrorType
-> e
-> Excepts es m a
-> Excepts es' m a
liftIOException' :: forall (m :: * -> *) e (es' :: [*]) (es :: [*]) a.
(MonadCatch m, MonadIO m, Monad m, e :< es', LiftVariant es es') =>
IOErrorType -> e -> Excepts es m a -> Excepts es' m a
liftIOException' IOErrorType
errType e
ex =
forall (m :: * -> *) a.
MonadCatch m =>
(IOException -> m a) -> m a -> m a
handleIO
(\IOException
e ->
if IOErrorType
errType forall a. Eq a => a -> a -> Bool
== IOException -> IOErrorType
ioeGetErrorType IOException
e then forall e (es :: [*]) a (m :: * -> *).
(Monad m, e :< es) =>
e -> Excepts es m a
throwE e
ex else forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. IOException -> IO a
ioError IOException
e
)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (es' :: [*]) (es :: [*]) a (m :: * -> *).
(Monad m, VEitherLift es es') =>
Excepts es m a -> Excepts es' m a
liftE
liftIOException :: (MonadCatch m, MonadIO m, Monad m, e :< es')
=> IOErrorType
-> e
-> m a
-> Excepts es' m a
liftIOException :: forall (m :: * -> *) e (es' :: [*]) a.
(MonadCatch m, MonadIO m, Monad m, e :< es') =>
IOErrorType -> e -> m a -> Excepts es' m a
liftIOException IOErrorType
errType e
ex =
forall (m :: * -> *) a.
MonadCatch m =>
(IOException -> m a) -> m a -> m a
handleIO
(\IOException
e ->
if IOErrorType
errType forall a. Eq a => a -> a -> Bool
== IOException -> IOErrorType
ioeGetErrorType IOException
e then forall e (es :: [*]) a (m :: * -> *).
(Monad m, e :< es) =>
e -> Excepts es m a
throwE e
ex else forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. IOException -> IO a
ioError IOException
e
)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift
hideError :: (MonadIO m, MonadCatch m) => IOErrorType -> m () -> m ()
hideError :: forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
IOErrorType -> m () -> m ()
hideError IOErrorType
err = forall (m :: * -> *) a.
MonadCatch m =>
(IOException -> m a) -> m a -> m a
handleIO (\IOException
e -> if IOErrorType
err forall a. Eq a => a -> a -> Bool
== IOException -> IOErrorType
ioeGetErrorType IOException
e then forall (f :: * -> *) a. Applicative f => a -> f a
pure () else forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IOException -> IO a
ioError forall a b. (a -> b) -> a -> b
$ IOException
e)
hideErrorDef :: (MonadIO m, MonadCatch m) => [IOErrorType] -> a -> m a -> m a
hideErrorDef :: forall (m :: * -> *) a.
(MonadIO m, MonadCatch m) =>
[IOErrorType] -> a -> m a -> m a
hideErrorDef [IOErrorType]
errs a
def =
forall (m :: * -> *) a.
MonadCatch m =>
(IOException -> m a) -> m a -> m a
handleIO (\IOException
e -> if IOException -> IOErrorType
ioeGetErrorType IOException
e forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [IOErrorType]
errs then forall (f :: * -> *) a. Applicative f => a -> f a
pure a
def else forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. IOException -> IO a
ioError IOException
e)
hideErrorDefM :: (MonadIO m, MonadCatch m) => [IOErrorType] -> m a -> m a -> m a
hideErrorDefM :: forall (m :: * -> *) a.
(MonadIO m, MonadCatch m) =>
[IOErrorType] -> m a -> m a -> m a
hideErrorDefM [IOErrorType]
errs m a
def =
forall (m :: * -> *) a.
MonadCatch m =>
(IOException -> m a) -> m a -> m a
handleIO (\IOException
e -> if IOException -> IOErrorType
ioeGetErrorType IOException
e forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [IOErrorType]
errs then m a
def else forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. IOException -> IO a
ioError IOException
e)
hideExcept :: forall e es es' a m
. (Monad m, e :< es, LiftVariant (Remove e es) es')
=> e
-> a
-> Excepts es m a
-> Excepts es' m a
hideExcept :: forall e (es :: [*]) (es' :: [*]) a (m :: * -> *).
(Monad m, e :< es, LiftVariant (Remove e es) es') =>
e -> a -> Excepts es m a -> Excepts es' m a
hideExcept e
_ a
a =
forall e (es :: [*]) (es' :: [*]) a (m :: * -> *).
(Monad m, e :< es, LiftVariant (Remove e es) es') =>
(e -> Excepts es' m a) -> Excepts es m a -> Excepts es' m a
catchLiftLeft ((\e
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure a
a) :: (e -> Excepts es' m a))
hideExcept' :: forall e es es' m
. (Monad m, e :< es, LiftVariant (Remove e es) es')
=> e
-> Excepts es m ()
-> Excepts es' m ()
hideExcept' :: forall e (es :: [*]) (es' :: [*]) (m :: * -> *).
(Monad m, e :< es, LiftVariant (Remove e es) es') =>
e -> Excepts es m () -> Excepts es' m ()
hideExcept' e
_ =
forall e (es :: [*]) (es' :: [*]) a (m :: * -> *).
(Monad m, e :< es, LiftVariant (Remove e es) es') =>
(e -> Excepts es' m a) -> Excepts es m a -> Excepts es' m a
catchLiftLeft ((\e
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ()) :: (e -> Excepts es' m ()))
reThrowAll :: forall e es es' a m
. (Monad m, e :< es')
=> (V es -> e)
-> Excepts es m a
-> Excepts es' m a
reThrowAll :: forall e (es :: [*]) (es' :: [*]) a (m :: * -> *).
(Monad m, e :< es') =>
(V es -> e) -> Excepts es m a -> Excepts es' m a
reThrowAll V es -> e
f = forall (m :: * -> *) (es :: [*]) (es' :: [*]) a.
Monad m =>
(V es -> Excepts es' m a) -> Excepts es m a -> Excepts es' m a
catchAllE (forall e (es :: [*]) a (m :: * -> *).
(Monad m, e :< es) =>
e -> Excepts es m a
throwE forall b c a. (b -> c) -> (a -> b) -> a -> c
. V es -> e
f)
reThrowAllIO :: forall e es es' a m
. (MonadCatch m, Monad m, MonadIO m, e :< es')
=> (V es -> e)
-> (IOException -> e)
-> Excepts es m a
-> Excepts es' m a
reThrowAllIO :: forall e (es :: [*]) (es' :: [*]) a (m :: * -> *).
(MonadCatch m, Monad m, MonadIO m, e :< es') =>
(V es -> e)
-> (IOException -> e) -> Excepts es m a -> Excepts es' m a
reThrowAllIO V es -> e
f IOException -> e
g = forall (m :: * -> *) a.
MonadCatch m =>
(IOException -> m a) -> m a -> m a
handleIO (forall e (es :: [*]) a (m :: * -> *).
(Monad m, e :< es) =>
e -> Excepts es m a
throwE forall b c a. (b -> c) -> (a -> b) -> a -> c
. IOException -> e
g) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) (es :: [*]) (es' :: [*]) a.
Monad m =>
(V es -> Excepts es' m a) -> Excepts es m a -> Excepts es' m a
catchAllE (forall e (es :: [*]) a (m :: * -> *).
(Monad m, e :< es) =>
e -> Excepts es m a
throwE forall b c a. (b -> c) -> (a -> b) -> a -> c
. V es -> e
f)
throwEither :: (Exception a, MonadThrow m) => Either a b -> m b
throwEither :: forall a (m :: * -> *) b.
(Exception a, MonadThrow m) =>
Either a b -> m b
throwEither Either a b
a = case Either a b
a of
Left a
e -> forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM a
e
Right b
r -> forall (f :: * -> *) a. Applicative f => a -> f a
pure b
r
throwEither' :: (Exception a, MonadThrow m) => a -> Either x b -> m b
throwEither' :: forall a (m :: * -> *) x b.
(Exception a, MonadThrow m) =>
a -> Either x b -> m b
throwEither' a
e Either x b
eth = case Either x b
eth of
Left x
_ -> forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM a
e
Right b
r -> forall (f :: * -> *) a. Applicative f => a -> f a
pure b
r
throwMaybe :: (Exception a, MonadThrow m) => a -> Maybe b -> m b
throwMaybe :: forall a (m :: * -> *) b.
(Exception a, MonadThrow m) =>
a -> Maybe b -> m b
throwMaybe a
a Maybe b
m = case Maybe b
m of
Maybe b
Nothing -> forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM a
a
Just b
r -> forall (f :: * -> *) a. Applicative f => a -> f a
pure b
r
throwMaybeM :: (Exception a, MonadThrow m) => a -> m (Maybe b) -> m b
throwMaybeM :: forall a (m :: * -> *) b.
(Exception a, MonadThrow m) =>
a -> m (Maybe b) -> m b
throwMaybeM a
a m (Maybe b)
am = do
Maybe b
m <- m (Maybe b)
am
forall a (m :: * -> *) b.
(Exception a, MonadThrow m) =>
a -> Maybe b -> m b
throwMaybe a
a Maybe b
m
verToBS :: Version -> ByteString
verToBS :: Version -> ByteString
verToBS = Text -> ByteString
E.encodeUtf8 forall b c a. (b -> c) -> (a -> b) -> a -> c
. Version -> Text
prettyVer
verToS :: Version -> String
verToS :: Version -> String
verToS = Text -> String
T.unpack forall b c a. (b -> c) -> (a -> b) -> a -> c
. Version -> Text
prettyVer
intToText :: Integral a => a -> T.Text
intToText :: forall a. Integral a => a -> Text
intToText = Text -> Text
TL.toStrict forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> Text
B.toLazyText forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Integral a => a -> Builder
B.decimal
decUTF8Safe :: ByteString -> Text
decUTF8Safe :: ByteString -> Text
decUTF8Safe = OnDecodeError -> ByteString -> Text
E.decodeUtf8With OnDecodeError
E.lenientDecode
decUTF8Safe' :: L.ByteString -> Text
decUTF8Safe' :: ByteString -> Text
decUTF8Safe' = Text -> Text
TL.toStrict forall b c a. (b -> c) -> (a -> b) -> a -> c
. OnDecodeError -> ByteString -> Text
TLE.decodeUtf8With OnDecodeError
E.lenientDecode
escapeVerRex :: Version -> ByteString
escapeVerRex :: Version -> ByteString
escapeVerRex = [Word8] -> ByteString
B.pack forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Word8] -> [Word8]
go forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> [Word8]
B.unpack forall b c a. (b -> c) -> (a -> b) -> a -> c
. Version -> ByteString
verToBS
where
go :: [Word8] -> [Word8]
go [] = []
go (Word8
x : [Word8]
xs) | Word8
x forall a. Eq a => a -> a -> Bool
== Word8
_period = [Word8
_backslash, Word8
_period] forall a. [a] -> [a] -> [a]
++ [Word8] -> [Word8]
go [Word8]
xs
| Bool
otherwise = Word8
x forall a. a -> [a] -> [a]
: [Word8] -> [Word8]
go [Word8]
xs
recover :: (MonadIO m, MonadMask m) => m a -> m a
recover :: forall (m :: * -> *) a. (MonadIO m, MonadMask m) => m a -> m a
recover m a
action =
forall (m :: * -> *) a.
(MonadIO m, MonadMask m) =>
RetryPolicyM m
-> [RetryStatus -> Handler m Bool] -> (RetryStatus -> m a) -> m a
recovering (forall (m :: * -> *). MonadIO m => Int -> RetryPolicyM m
fullJitterBackoff Int
25000 forall a. Semigroup a => a -> a -> a
<> Int -> RetryPolicy
limitRetries Int
10)
[\RetryStatus
_ -> forall (m :: * -> *) a e. Exception e => (e -> m a) -> Handler m a
Handler (\IOException
e -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ IOException -> Bool
isPermissionError IOException
e)
,\RetryStatus
_ -> forall (m :: * -> *) a e. Exception e => (e -> m a) -> Handler m a
Handler (\IOException
e -> forall (f :: * -> *) a. Applicative f => a -> f a
pure (IOException -> IOErrorType
ioeGetErrorType IOException
e forall a. Eq a => a -> a -> Bool
== IOErrorType
InappropriateType))
,\RetryStatus
_ -> forall (m :: * -> *) a e. Exception e => (e -> m a) -> Handler m a
Handler (\IOException
e -> forall (f :: * -> *) a. Applicative f => a -> f a
pure (IOException -> IOErrorType
ioeGetErrorType IOException
e forall a. Eq a => a -> a -> Bool
== IOErrorType
UnsatisfiedConstraints))
]
(\RetryStatus
_ -> m a
action)
traverseFold :: (Foldable t, Applicative m, Monoid b) => (a -> m b) -> t a -> m b
traverseFold :: forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Applicative m, Monoid b) =>
(a -> m b) -> t a -> m b
traverseFold a -> m b
f = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl (\m b
mb a
a -> forall a. Semigroup a => a -> a -> a
(<>) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m b
mb forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> a -> m b
f a
a) (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Monoid a => a
mempty)
forFold :: (Foldable t, Applicative m, Monoid b) => t a -> (a -> m b) -> m b
forFold :: forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Applicative m, Monoid b) =>
t a -> (a -> m b) -> m b
forFold = \t a
t -> (forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Applicative m, Monoid b) =>
(a -> m b) -> t a -> m b
`traverseFold` t a
t)
stripNewline :: String -> String
stripNewline :: String -> String
stripNewline = forall a. (a -> Bool) -> [a] -> [a]
filter (forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` String
"\n\r")
stripNewlineEnd :: String -> String
stripNewlineEnd :: String -> String
stripNewlineEnd = forall a. (a -> Bool) -> [a] -> [a]
dropWhileEnd (forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` String
"\n\r")
stripNewline' :: T.Text -> T.Text
stripNewline' :: Text -> Text
stripNewline' = (Char -> Bool) -> Text -> Text
T.filter (forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` String
"\n\r")
isNewLine :: Word8 -> Bool
isNewLine :: Word8 -> Bool
isNewLine Word8
w
| Word8
w forall a. Eq a => a -> a -> Bool
== Word8
_lf = Bool
True
| Word8
w forall a. Eq a => a -> a -> Bool
== Word8
_cr = Bool
True
| Bool
otherwise = Bool
False
splitOnPVP :: String -> String -> (String, String)
splitOnPVP :: String -> String -> (String, String)
splitOnPVP String
c String
s = case forall a. Eq a => [a] -> [a] -> [[a]]
Split.splitOn String
c String
s of
[] -> (String, String)
def
[String
_] -> (String, String)
def
[String]
xs
| let l :: String
l = forall a. [a] -> a
last [String]
xs
, (Right PVP
_) <- Text -> Either ParsingError PVP
pvp (String -> Text
T.pack String
l) -> (forall a. [a] -> [[a]] -> [a]
intercalate String
c (forall a. [a] -> [a]
init [String]
xs), String
l)
| Bool
otherwise -> (String, String)
def
where
def :: (String, String)
def = (String
s, String
"")
findM :: Monad m => (a -> m Bool) -> [a] -> m (Maybe a)
findM :: forall (m :: * -> *) a.
Monad m =>
(a -> m Bool) -> [a] -> m (Maybe a)
findM ~a -> m Bool
p = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\a
x -> forall (m :: * -> *) a. Monad m => m Bool -> m a -> m a -> m a
ifM (a -> m Bool
p a
x) (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just a
x)) (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Maybe a
Nothing)
dropSuffix :: Eq a => [a] -> [a] -> [a]
dropSuffix :: forall a. Eq a => [a] -> [a] -> [a]
dropSuffix [a]
a [a]
b = forall a. a -> Maybe a -> a
fromMaybe [a]
b forall a b. (a -> b) -> a -> b
$ forall a. Eq a => [a] -> [a] -> Maybe [a]
stripSuffix [a]
a [a]
b
stripSuffix :: Eq a => [a] -> [a] -> Maybe [a]
stripSuffix :: forall a. Eq a => [a] -> [a] -> Maybe [a]
stripSuffix [a]
a [a]
b = forall a. [a] -> [a]
reverse forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Eq a => [a] -> [a] -> Maybe [a]
stripPrefix (forall a. [a] -> [a]
reverse [a]
a) (forall a. [a] -> [a]
reverse [a]
b)
dropPrefix :: Eq a => [a] -> [a] -> [a]
dropPrefix :: forall a. Eq a => [a] -> [a] -> [a]
dropPrefix [a]
a [a]
b = forall a. a -> Maybe a -> a
fromMaybe [a]
b forall a b. (a -> b) -> a -> b
$ forall a. Eq a => [a] -> [a] -> Maybe [a]
stripPrefix [a]
a [a]
b
splitOn :: Eq a => [a] -> [a] -> [[a]]
splitOn :: forall a. Eq a => [a] -> [a] -> [[a]]
splitOn [] [a]
_ = forall a. HasCallStack => String -> a
error String
"splitOn, needle may not be empty"
splitOn [a]
_ [] = [[]]
splitOn [a]
needle [a]
haystack = [a]
a forall a. a -> [a] -> [a]
: if forall (t :: * -> *) a. Foldable t => t a -> Bool
null [a]
b then [] else forall a. Eq a => [a] -> [a] -> [[a]]
splitOn [a]
needle forall a b. (a -> b) -> a -> b
$ forall a. Int -> [a] -> [a]
drop (forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
needle) [a]
b
where ([a]
a,[a]
b) = forall a. Eq a => [a] -> [a] -> ([a], [a])
breakOn [a]
needle [a]
haystack
split :: (a -> Bool) -> [a] -> [[a]]
split :: forall a. (a -> Bool) -> [a] -> [[a]]
split a -> Bool
_ [] = [[]]
split a -> Bool
f (a
x:[a]
xs)
| a -> Bool
f a
x = [] forall a. a -> [a] -> [a]
: forall a. (a -> Bool) -> [a] -> [[a]]
split a -> Bool
f [a]
xs
| [a]
y:[[a]]
ys <- forall a. (a -> Bool) -> [a] -> [[a]]
split a -> Bool
f [a]
xs = (a
xforall a. a -> [a] -> [a]
:[a]
y) forall a. a -> [a] -> [a]
: [[a]]
ys
| Bool
otherwise = [[]]
breakOn :: Eq a => [a] -> [a] -> ([a], [a])
breakOn :: forall a. Eq a => [a] -> [a] -> ([a], [a])
breakOn [a]
needle [a]
haystack | [a]
needle forall a. Eq a => [a] -> [a] -> Bool
`isPrefixOf` [a]
haystack = ([], [a]
haystack)
breakOn [a]
_ [] = ([], [])
breakOn [a]
needle (a
x:[a]
xs) = forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (a
xforall a. a -> [a] -> [a]
:) forall a b. (a -> b) -> a -> b
$ forall a. Eq a => [a] -> [a] -> ([a], [a])
breakOn [a]
needle [a]
xs