{-# 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 :: String -> a
fS = String -> a
forall a. IsString a => String -> a
fromString
fromStrictMaybe :: S.Maybe a -> Maybe a
fromStrictMaybe :: Maybe a -> Maybe a
fromStrictMaybe = Maybe a -> (a -> Maybe a) -> Maybe a -> Maybe a
forall b a. b -> (a -> b) -> Maybe a -> b
S.maybe Maybe a
forall a. Maybe a
Nothing a -> Maybe a
forall a. a -> Maybe a
Just
fSM :: S.Maybe a -> Maybe a
fSM :: Maybe a -> Maybe a
fSM = Maybe a -> Maybe a
forall a. Maybe a -> Maybe a
fromStrictMaybe
toStrictMaybe :: Maybe a -> S.Maybe a
toStrictMaybe :: Maybe a -> Maybe a
toStrictMaybe = Maybe a -> (a -> Maybe a) -> Maybe a -> Maybe a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Maybe a
forall a. Maybe a
S.Nothing a -> Maybe a
forall a. a -> Maybe a
S.Just
tSM :: Maybe a -> S.Maybe a
tSM :: Maybe a -> Maybe a
tSM = Maybe a -> Maybe a
forall a. Maybe a -> Maybe a
toStrictMaybe
internalError :: String -> IO a
internalError :: String -> IO a
internalError = String -> IO a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> IO a) -> (String -> String) -> String -> IO a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
"Internal error: " String -> String -> String
forall a. Semigroup a => a -> a -> a
<>)
iE :: String -> IO a
iE :: String -> IO a
iE = String -> IO a
forall a. String -> IO a
internalError
showT :: Show a => a -> Text
showT :: a -> Text
showT = String -> Text
forall a. IsString a => String -> a
fS (String -> Text) -> (a -> String) -> a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> String
forall a. Show a => a -> String
show
whenM :: Monad m => m Bool -> m () -> m ()
whenM :: m Bool -> m () -> m ()
whenM ~m Bool
b ~m ()
t = m Bool -> m () -> m () -> m ()
forall (m :: * -> *) a. Monad m => m Bool -> m a -> m a -> m a
ifM m Bool
b m ()
t (() -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())
unlessM :: Monad m => m Bool -> m () -> m ()
unlessM :: m Bool -> m () -> m ()
unlessM ~m Bool
b ~m ()
f = m Bool -> m () -> m () -> m ()
forall (m :: * -> *) a. Monad m => m Bool -> m a -> m a -> m a
ifM m Bool
b (() -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()) m ()
f
ifM :: Monad m => m Bool -> m a -> m a -> m a
ifM :: 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 :: 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 m a -> (a -> m Bool) -> m a
forall (m :: * -> *) a. Monad m => m a -> (a -> m Bool) -> m a
whileM m a
action a -> m Bool
f else a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
a
whileM_ :: Monad m => m a -> (a -> m Bool) -> m ()
whileM_ :: m a -> (a -> m Bool) -> m ()
whileM_ ~m a
action = m a -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (m a -> m ()) -> ((a -> m Bool) -> m a) -> (a -> m Bool) -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m a -> (a -> m Bool) -> m a
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 :: m Bool -> m ()
guardM ~m Bool
f = Bool -> m ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> m ()) -> m Bool -> m ()
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' :: IOErrorType -> (IOException -> m a) -> m a -> m a
handleIO' IOErrorType
err IOException -> m a
handler = (IOException -> m a) -> m a -> m a
forall (m :: * -> *) a.
MonadCatch m =>
(IOException -> m a) -> m a -> m a
handleIO
(\IOException
e -> if IOErrorType
err IOErrorType -> IOErrorType -> Bool
forall a. Eq a => a -> a -> Bool
== IOException -> IOErrorType
ioeGetErrorType IOException
e then IOException -> m a
handler IOException
e else IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO a -> m a) -> IO a -> m a
forall a b. (a -> b) -> a -> b
$ IOException -> IO a
forall a. IOException -> IO a
ioError IOException
e)
(??) :: forall e es a m . (Monad m, e :< es) => Maybe a -> e -> Excepts es m a
?? :: Maybe a -> e -> Excepts es m a
(??) Maybe a
m e
e = Excepts es m a
-> (a -> Excepts es m a) -> Maybe a -> Excepts es m a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (e -> Excepts es m a
forall e (es :: [*]) a (m :: * -> *).
(Monad m, e :< es) =>
e -> Excepts es m a
throwE e
e) a -> Excepts es m a
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
!? :: m (Maybe a) -> e -> Excepts es m a
(!?) m (Maybe a)
em e
e = m (Maybe a) -> Excepts es m (Maybe a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m (Maybe a)
em Excepts es m (Maybe a)
-> (Maybe a -> Excepts es m a) -> Excepts es m a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Maybe a -> e -> Excepts es m a
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 :: Either e a -> Excepts es m a
lE = Excepts '[e] m a -> Excepts es m a
forall (es' :: [*]) (es :: [*]) a (m :: * -> *).
(Monad m, VEitherLift es es') =>
Excepts es m a -> Excepts es' m a
liftE (Excepts '[e] m a -> Excepts es m a)
-> (Either e a -> Excepts '[e] m a) -> Either e a -> Excepts es m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VEither '[e] a -> Excepts '[e] m a
forall (m :: * -> *) (es :: [*]) a.
Monad m =>
VEither es a -> Excepts es m a
veitherToExcepts (VEither '[e] a -> Excepts '[e] m a)
-> (Either e a -> VEither '[e] a) -> Either e a -> Excepts '[e] m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Either e a -> VEither '[e] a
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' :: (e' -> e) -> Either e' a -> Excepts es m a
lE' e' -> e
f = Excepts '[e] m a -> Excepts es m a
forall (es' :: [*]) (es :: [*]) a (m :: * -> *).
(Monad m, VEitherLift es es') =>
Excepts es m a -> Excepts es' m a
liftE (Excepts '[e] m a -> Excepts es m a)
-> (Either e' a -> Excepts '[e] m a)
-> Either e' a
-> Excepts es m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VEither '[e] a -> Excepts '[e] m a
forall (m :: * -> *) (es :: [*]) a.
Monad m =>
VEither es a -> Excepts es m a
veitherToExcepts (VEither '[e] a -> Excepts '[e] m a)
-> (Either e' a -> VEither '[e] a)
-> Either e' a
-> Excepts '[e] m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Either e a -> VEither '[e] a
forall a b. Either a b -> VEither '[a] b
fromEither (Either e a -> VEither '[e] a)
-> (Either e' a -> Either e a) -> Either e' a -> VEither '[e] a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (e' -> e) -> Either e' a -> Either e a
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 :: m (Either e a) -> Excepts es m a
lEM m (Either e a)
em = m (Either e a) -> Excepts es m (Either e a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m (Either e a)
em Excepts es m (Either e a)
-> (Either e a -> Excepts es m a) -> Excepts es m a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Either e a -> Excepts es m a
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' :: (e' -> e) -> m (Either e' a) -> Excepts es m a
lEM' e' -> e
f m (Either e' a)
em = m (Either e' a) -> Excepts es m (Either e' a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m (Either e' a)
em Excepts es m (Either e' a)
-> (Either e' a -> Excepts es m a) -> Excepts es m a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Either e a -> Excepts es m a
forall e (es :: [*]) a (m :: * -> *).
(Monad m, e :< es) =>
Either e a -> Excepts es m a
lE (Either e a -> Excepts es m a)
-> (Either e' a -> Either e a) -> Either e' a -> Excepts es m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (e' -> e) -> Either e' a -> Either e a
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 :: Either a b -> VEither '[a] b
fromEither = (a -> VEither '[a] b)
-> (b -> VEither '[a] b) -> Either a b -> VEither '[a] b
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (V '[a] -> VEither '[a] b
forall x (xs :: [*]). V xs -> VEither xs x
VLeft (V '[a] -> VEither '[a] b) -> (a -> V '[a]) -> a -> VEither '[a] b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> V '[a]
forall c (cs :: [*]). (c :< cs) => c -> V cs
V) b -> VEither '[a] b
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' :: IOErrorType -> e -> Excepts es m a -> Excepts es' m a
liftIOException' IOErrorType
errType e
ex =
(IOException -> Excepts es' m a)
-> Excepts es' m a -> Excepts es' m a
forall (m :: * -> *) a.
MonadCatch m =>
(IOException -> m a) -> m a -> m a
handleIO
(\IOException
e ->
if IOErrorType
errType IOErrorType -> IOErrorType -> Bool
forall a. Eq a => a -> a -> Bool
== IOException -> IOErrorType
ioeGetErrorType IOException
e then e -> Excepts es' m a
forall e (es :: [*]) a (m :: * -> *).
(Monad m, e :< es) =>
e -> Excepts es m a
throwE e
ex else IO a -> Excepts es' m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO a -> Excepts es' m a) -> IO a -> Excepts es' m a
forall a b. (a -> b) -> a -> b
$ IOException -> IO a
forall a. IOException -> IO a
ioError IOException
e
)
(Excepts es' m a -> Excepts es' m a)
-> (Excepts es m a -> Excepts es' m a)
-> Excepts es m a
-> Excepts es' m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Excepts es m a -> Excepts es' m a
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 :: IOErrorType -> e -> m a -> Excepts es' m a
liftIOException IOErrorType
errType e
ex =
(IOException -> Excepts es' m a)
-> Excepts es' m a -> Excepts es' m a
forall (m :: * -> *) a.
MonadCatch m =>
(IOException -> m a) -> m a -> m a
handleIO
(\IOException
e ->
if IOErrorType
errType IOErrorType -> IOErrorType -> Bool
forall a. Eq a => a -> a -> Bool
== IOException -> IOErrorType
ioeGetErrorType IOException
e then e -> Excepts es' m a
forall e (es :: [*]) a (m :: * -> *).
(Monad m, e :< es) =>
e -> Excepts es m a
throwE e
ex else IO a -> Excepts es' m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO a -> Excepts es' m a) -> IO a -> Excepts es' m a
forall a b. (a -> b) -> a -> b
$ IOException -> IO a
forall a. IOException -> IO a
ioError IOException
e
)
(Excepts es' m a -> Excepts es' m a)
-> (m a -> Excepts es' m a) -> m a -> Excepts es' m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m a -> Excepts es' m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift
hideError :: (MonadIO m, MonadCatch m) => IOErrorType -> m () -> m ()
hideError :: IOErrorType -> m () -> m ()
hideError IOErrorType
err = (IOException -> m ()) -> m () -> m ()
forall (m :: * -> *) a.
MonadCatch m =>
(IOException -> m a) -> m a -> m a
handleIO (\IOException
e -> if IOErrorType
err IOErrorType -> IOErrorType -> Bool
forall a. Eq a => a -> a -> Bool
== IOException -> IOErrorType
ioeGetErrorType IOException
e then () -> m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure () else IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> (IOException -> IO ()) -> IOException -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IOException -> IO ()
forall a. IOException -> IO a
ioError (IOException -> m ()) -> IOException -> m ()
forall a b. (a -> b) -> a -> b
$ IOException
e)
hideErrorDef :: (MonadIO m, MonadCatch m) => [IOErrorType] -> a -> m a -> m a
hideErrorDef :: [IOErrorType] -> a -> m a -> m a
hideErrorDef [IOErrorType]
errs a
def =
(IOException -> m a) -> m a -> m a
forall (m :: * -> *) a.
MonadCatch m =>
(IOException -> m a) -> m a -> m a
handleIO (\IOException
e -> if IOException -> IOErrorType
ioeGetErrorType IOException
e IOErrorType -> [IOErrorType] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [IOErrorType]
errs then a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
def else IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO a -> m a) -> IO a -> m a
forall a b. (a -> b) -> a -> b
$ IOException -> IO a
forall a. IOException -> IO a
ioError IOException
e)
hideErrorDefM :: (MonadIO m, MonadCatch m) => [IOErrorType] -> m a -> m a -> m a
hideErrorDefM :: [IOErrorType] -> m a -> m a -> m a
hideErrorDefM [IOErrorType]
errs m a
def =
(IOException -> m a) -> m a -> m a
forall (m :: * -> *) a.
MonadCatch m =>
(IOException -> m a) -> m a -> m a
handleIO (\IOException
e -> if IOException -> IOErrorType
ioeGetErrorType IOException
e IOErrorType -> [IOErrorType] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [IOErrorType]
errs then m a
def else IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO a -> m a) -> IO a -> m a
forall a b. (a -> b) -> a -> b
$ IOException -> IO a
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 :: e -> a -> Excepts es m a -> Excepts es' m a
hideExcept e
_ a
a =
(e -> Excepts es' m a) -> Excepts es m a -> Excepts es' m 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
_ -> a -> Excepts es' m a
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' :: e -> Excepts es m () -> Excepts es' m ()
hideExcept' e
_ =
(e -> Excepts es' m ()) -> Excepts es m () -> Excepts es' m ()
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
_ -> () -> Excepts es' m ()
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 :: (V es -> e) -> Excepts es m a -> Excepts es' m a
reThrowAll V es -> e
f = (V es -> Excepts es' m a) -> Excepts es m a -> Excepts es' m a
forall (m :: * -> *) (es :: [*]) (es' :: [*]) a.
Monad m =>
(V es -> Excepts es' m a) -> Excepts es m a -> Excepts es' m a
catchAllE (e -> Excepts es' m a
forall e (es :: [*]) a (m :: * -> *).
(Monad m, e :< es) =>
e -> Excepts es m a
throwE (e -> Excepts es' m a) -> (V es -> e) -> V es -> Excepts es' m a
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 :: (V es -> e)
-> (IOException -> e) -> Excepts es m a -> Excepts es' m a
reThrowAllIO V es -> e
f IOException -> e
g = (IOException -> Excepts es' m a)
-> Excepts es' m a -> Excepts es' m a
forall (m :: * -> *) a.
MonadCatch m =>
(IOException -> m a) -> m a -> m a
handleIO (e -> Excepts es' m a
forall e (es :: [*]) a (m :: * -> *).
(Monad m, e :< es) =>
e -> Excepts es m a
throwE (e -> Excepts es' m a)
-> (IOException -> e) -> IOException -> Excepts es' m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IOException -> e
g) (Excepts es' m a -> Excepts es' m a)
-> (Excepts es m a -> Excepts es' m a)
-> Excepts es m a
-> Excepts es' m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (V es -> Excepts es' m a) -> Excepts es m a -> Excepts es' m a
forall (m :: * -> *) (es :: [*]) (es' :: [*]) a.
Monad m =>
(V es -> Excepts es' m a) -> Excepts es m a -> Excepts es' m a
catchAllE (e -> Excepts es' m a
forall e (es :: [*]) a (m :: * -> *).
(Monad m, e :< es) =>
e -> Excepts es m a
throwE (e -> Excepts es' m a) -> (V es -> e) -> V es -> Excepts es' m a
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 :: Either a b -> m b
throwEither Either a b
a = case Either a b
a of
Left a
e -> a -> m b
forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM a
e
Right b
r -> b -> m b
forall (f :: * -> *) a. Applicative f => a -> f a
pure b
r
throwEither' :: (Exception a, MonadThrow m) => a -> Either x b -> m b
throwEither' :: a -> Either x b -> m b
throwEither' a
e Either x b
eth = case Either x b
eth of
Left x
_ -> a -> m b
forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM a
e
Right b
r -> b -> m b
forall (f :: * -> *) a. Applicative f => a -> f a
pure b
r
throwMaybe :: (Exception a, MonadThrow m) => a -> Maybe b -> m b
throwMaybe :: a -> Maybe b -> m b
throwMaybe a
a Maybe b
m = case Maybe b
m of
Maybe b
Nothing -> a -> m b
forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM a
a
Just b
r -> b -> m b
forall (f :: * -> *) a. Applicative f => a -> f a
pure b
r
throwMaybeM :: (Exception a, MonadThrow m) => a -> m (Maybe b) -> m b
throwMaybeM :: a -> m (Maybe b) -> m b
throwMaybeM a
a m (Maybe b)
am = do
Maybe b
m <- m (Maybe b)
am
a -> Maybe b -> m b
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 (Text -> ByteString) -> (Version -> Text) -> Version -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Version -> Text
prettyVer
verToS :: Version -> String
verToS :: Version -> String
verToS = Text -> String
T.unpack (Text -> String) -> (Version -> Text) -> Version -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Version -> Text
prettyVer
intToText :: Integral a => a -> T.Text
intToText :: a -> Text
intToText = Text -> Text
TL.toStrict (Text -> Text) -> (a -> Text) -> a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> Text
B.toLazyText (Builder -> Text) -> (a -> Builder) -> a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Builder
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 (Text -> Text) -> (ByteString -> Text) -> ByteString -> Text
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 ([Word8] -> ByteString)
-> (Version -> [Word8]) -> Version -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Word8] -> [Word8]
go ([Word8] -> [Word8]) -> (Version -> [Word8]) -> Version -> [Word8]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> [Word8]
B.unpack (ByteString -> [Word8])
-> (Version -> ByteString) -> Version -> [Word8]
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 Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
_period = [Word8
_backslash, Word8
_period] [Word8] -> [Word8] -> [Word8]
forall a. [a] -> [a] -> [a]
++ [Word8] -> [Word8]
go [Word8]
xs
| Bool
otherwise = Word8
x Word8 -> [Word8] -> [Word8]
forall a. a -> [a] -> [a]
: [Word8] -> [Word8]
go [Word8]
xs
recover :: (MonadIO m, MonadMask m) => m a -> m a
recover :: m a -> m a
recover m a
action =
RetryPolicyM m
-> [RetryStatus -> Handler m Bool] -> (RetryStatus -> m a) -> m a
forall (m :: * -> *) a.
(MonadIO m, MonadMask m) =>
RetryPolicyM m
-> [RetryStatus -> Handler m Bool] -> (RetryStatus -> m a) -> m a
recovering (Int -> RetryPolicyM m
forall (m :: * -> *). MonadIO m => Int -> RetryPolicyM m
fullJitterBackoff Int
25000 RetryPolicyM m -> RetryPolicyM m -> RetryPolicyM m
forall a. Semigroup a => a -> a -> a
<> Int -> RetryPolicy
limitRetries Int
10)
[\RetryStatus
_ -> (IOException -> m Bool) -> Handler m Bool
forall (m :: * -> *) a e. Exception e => (e -> m a) -> Handler m a
Handler (\IOException
e -> Bool -> m Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Bool -> m Bool) -> Bool -> m Bool
forall a b. (a -> b) -> a -> b
$ IOException -> Bool
isPermissionError IOException
e)
,\RetryStatus
_ -> (IOException -> m Bool) -> Handler m Bool
forall (m :: * -> *) a e. Exception e => (e -> m a) -> Handler m a
Handler (\IOException
e -> Bool -> m Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure (IOException -> IOErrorType
ioeGetErrorType IOException
e IOErrorType -> IOErrorType -> Bool
forall a. Eq a => a -> a -> Bool
== IOErrorType
InappropriateType))
,\RetryStatus
_ -> (IOException -> m Bool) -> Handler m Bool
forall (m :: * -> *) a e. Exception e => (e -> m a) -> Handler m a
Handler (\IOException
e -> Bool -> m Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure (IOException -> IOErrorType
ioeGetErrorType IOException
e IOErrorType -> IOErrorType -> Bool
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 :: (a -> m b) -> t a -> m b
traverseFold a -> m b
f = (m b -> a -> m b) -> m b -> t a -> m b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl (\m b
mb a
a -> b -> b -> b
forall a. Semigroup a => a -> a -> a
(<>) (b -> b -> b) -> m b -> m (b -> b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m b
mb m (b -> b) -> m b -> m b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> a -> m b
f a
a) (b -> m b
forall (f :: * -> *) a. Applicative f => a -> f a
pure b
forall a. Monoid a => a
mempty)
forFold :: (Foldable t, Applicative m, Monoid b) => t a -> (a -> m b) -> m b
forFold :: t a -> (a -> m b) -> m b
forFold = \t a
t -> ((a -> m b) -> t a -> m b
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 = (Char -> Bool) -> String -> String
forall a. (a -> Bool) -> [a] -> [a]
filter (Char -> String -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` String
"\n\r")
stripNewlineEnd :: String -> String
stripNewlineEnd :: String -> String
stripNewlineEnd = (Char -> Bool) -> String -> String
forall a. (a -> Bool) -> [a] -> [a]
dropWhileEnd (Char -> String -> Bool
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 (Char -> String -> Bool
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 Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
_lf = Bool
True
| Word8
w Word8 -> Word8 -> Bool
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 String -> String -> [String]
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 = [String] -> String
forall a. [a] -> a
last [String]
xs
, (Right PVP
_) <- Text -> Either ParsingError PVP
pvp (String -> Text
T.pack String
l) -> (String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
c ([String] -> [String]
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 :: (a -> m Bool) -> [a] -> m (Maybe a)
findM ~a -> m Bool
p = (a -> m (Maybe a) -> m (Maybe a))
-> m (Maybe a) -> [a] -> m (Maybe a)
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\a
x -> m Bool -> m (Maybe a) -> m (Maybe a) -> m (Maybe a)
forall (m :: * -> *) a. Monad m => m Bool -> m a -> m a -> m a
ifM (a -> m Bool
p a
x) (Maybe a -> m (Maybe a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe a -> m (Maybe a)) -> Maybe a -> m (Maybe a)
forall a b. (a -> b) -> a -> b
$ a -> Maybe a
forall a. a -> Maybe a
Just a
x)) (Maybe a -> m (Maybe a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe a
forall a. Maybe a
Nothing)
dropSuffix :: Eq a => [a] -> [a] -> [a]
dropSuffix :: [a] -> [a] -> [a]
dropSuffix [a]
a [a]
b = [a] -> Maybe [a] -> [a]
forall a. a -> Maybe a -> a
fromMaybe [a]
b (Maybe [a] -> [a]) -> Maybe [a] -> [a]
forall a b. (a -> b) -> a -> b
$ [a] -> [a] -> Maybe [a]
forall a. Eq a => [a] -> [a] -> Maybe [a]
stripSuffix [a]
a [a]
b
stripSuffix :: Eq a => [a] -> [a] -> Maybe [a]
stripSuffix :: [a] -> [a] -> Maybe [a]
stripSuffix [a]
a [a]
b = [a] -> [a]
forall a. [a] -> [a]
reverse ([a] -> [a]) -> Maybe [a] -> Maybe [a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [a] -> [a] -> Maybe [a]
forall a. Eq a => [a] -> [a] -> Maybe [a]
stripPrefix ([a] -> [a]
forall a. [a] -> [a]
reverse [a]
a) ([a] -> [a]
forall a. [a] -> [a]
reverse [a]
b)
dropPrefix :: Eq a => [a] -> [a] -> [a]
dropPrefix :: [a] -> [a] -> [a]
dropPrefix [a]
a [a]
b = [a] -> Maybe [a] -> [a]
forall a. a -> Maybe a -> a
fromMaybe [a]
b (Maybe [a] -> [a]) -> Maybe [a] -> [a]
forall a b. (a -> b) -> a -> b
$ [a] -> [a] -> Maybe [a]
forall a. Eq a => [a] -> [a] -> Maybe [a]
stripPrefix [a]
a [a]
b
splitOn :: Eq a => [a] -> [a] -> [[a]]
splitOn :: [a] -> [a] -> [[a]]
splitOn [] [a]
_ = String -> [[a]]
forall a. HasCallStack => String -> a
error String
"splitOn, needle may not be empty"
splitOn [a]
_ [] = [[]]
splitOn [a]
needle [a]
haystack = [a]
a [a] -> [[a]] -> [[a]]
forall a. a -> [a] -> [a]
: if [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [a]
b then [] else [a] -> [a] -> [[a]]
forall a. Eq a => [a] -> [a] -> [[a]]
splitOn [a]
needle ([a] -> [[a]]) -> [a] -> [[a]]
forall a b. (a -> b) -> a -> b
$ Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
drop ([a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
needle) [a]
b
where ([a]
a,[a]
b) = [a] -> [a] -> ([a], [a])
forall a. Eq a => [a] -> [a] -> ([a], [a])
breakOn [a]
needle [a]
haystack
split :: (a -> Bool) -> [a] -> [[a]]
split :: (a -> Bool) -> [a] -> [[a]]
split a -> Bool
_ [] = [[]]
split a -> Bool
f (a
x:[a]
xs)
| a -> Bool
f a
x = [] [a] -> [[a]] -> [[a]]
forall a. a -> [a] -> [a]
: (a -> Bool) -> [a] -> [[a]]
forall a. (a -> Bool) -> [a] -> [[a]]
split a -> Bool
f [a]
xs
| [a]
y:[[a]]
ys <- (a -> Bool) -> [a] -> [[a]]
forall a. (a -> Bool) -> [a] -> [[a]]
split a -> Bool
f [a]
xs = (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
y) [a] -> [[a]] -> [[a]]
forall a. a -> [a] -> [a]
: [[a]]
ys
| Bool
otherwise = [[]]
breakOn :: Eq a => [a] -> [a] -> ([a], [a])
breakOn :: [a] -> [a] -> ([a], [a])
breakOn [a]
needle [a]
haystack | [a]
needle [a] -> [a] -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isPrefixOf` [a]
haystack = ([], [a]
haystack)
breakOn [a]
_ [] = ([], [])
breakOn [a]
needle (a
x:[a]
xs) = ([a] -> [a]) -> ([a], [a]) -> ([a], [a])
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:) (([a], [a]) -> ([a], [a])) -> ([a], [a]) -> ([a], [a])
forall a b. (a -> b) -> a -> b
$ [a] -> [a] -> ([a], [a])
forall a. Eq a => [a] -> [a] -> ([a], [a])
breakOn [a]
needle [a]
xs