{-# LANGUAGE CPP                 #-}
{-# LANGUAGE DataKinds           #-}
{-# LANGUAGE FlexibleContexts    #-}
{-# LANGUAGE FlexibleInstances   #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies        #-}
{-# LANGUAGE TypeOperators       #-}

{-|
Module      : GHCup.Prelude.Internal
Description : MegaParsec utilities
Copyright   : (c) Julian Ospald, 2020
License     : LGPL-3.0
Maintainer  : hasufell@hasufell.de
Stability   : experimental
Portability : portable

Stuff that doesn't need GHCup modules, so we can avoid
recursive imports.
-}
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



-- $setup
-- >>> import Data.ByteString.Internal (c2w, w2c)
-- >>> import Test.QuickCheck
-- >>> import Data.Word8
-- >>> import qualified Data.Text as T
-- >>> import qualified Data.Char as C
-- >>> import Data.List
-- >>> instance Arbitrary T.Text where arbitrary = T.pack <$> arbitrary


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

-- | Like 'when', but where the test can be monadic.
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 ())

-- | Like 'unless', but where the test can be monadic.
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

-- | Like @if@, but where the test can be monadic.
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


-- | Uses safe-exceptions.
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)


-- TODO: does this work?
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



-- | Safe 'decodeUtf8With'. Replaces an invalid input byte with
-- the Unicode replacement character U+FFFD.
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


-- | Escape a version for use in regex
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)


-- | Gathering monoidal values
--
-- >>> traverseFold (pure . (:["0"])) ["1","2"]
-- ["1","0","2","0"]
-- >>> traverseFold Just ["1","2","3","4","5"]
-- Just "12345"
--
-- prop> \t -> traverseFold Just t === Just (mconcat t)
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)

-- | Gathering monoidal values
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)


-- | Strip @\\r@ and @\\n@ from 'String's
--
-- >>> stripNewline "foo\n\n\n"
-- "foo"
-- >>> stripNewline "foo\n\n\nfoo"
-- "foofoo"
-- >>> stripNewline "foo\r"
-- "foo"
-- >>> stripNewline "foo"
-- "foo"
--
-- prop> \t -> stripNewline (t <> "\n") === stripNewline t
-- prop> \t -> not (any (isNewLine . c2w) t) ==> stripNewline t == 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")


-- | Strip @\\r@ and @\\n@ from end of 'String'.
--
-- >>> stripNewlineEnd "foo\n\n\n"
-- "foo"
-- >>> stripNewlineEnd "foo\n\n\nfoo"
-- "foo\n\n\nfoo"
-- >>> stripNewlineEnd "foo\r"
-- "foo"
-- >>> stripNewlineEnd "foo"
-- "foo"
--
-- prop> \t -> stripNewlineEnd (t <> "\n") === stripNewlineEnd t
-- prop> \t -> not (any (isNewLine . c2w) t) ==> stripNewlineEnd t == t
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")


-- | Strip @\\r@ and @\\n@ from 'Text's
--
-- >>> stripNewline' "foo\n\n\n"
-- "foo"
-- >>> stripNewline' "foo\n\n\nfoo"
-- "foofoo"
-- >>> stripNewline' "foo\r"
-- "foo"
-- >>> stripNewline' "foo"
-- "foo"
--
-- prop> \t -> stripNewline' (t <> "\n") === stripNewline' t
-- prop> \t -> not (T.any (isNewLine . c2w) t) ==> stripNewline' t == t
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")


-- | Is the word8 a newline?
--
-- >>> isNewLine (c2w '\n')
-- True
-- >>> isNewLine (c2w '\r')
-- True
--
-- prop> \w -> w /= _lf && w /= _cr ==> not (isNewLine w)
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


-- | Split on a PVP suffix.
--
-- >>> splitOnPVP "-" "ghc-iserv-dyn-9.3.20210706"
-- ("ghc-iserv-dyn","9.3.20210706")
-- >>> splitOnPVP "-" "ghc-iserv-dyn"
-- ("ghc-iserv-dyn","")
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
"")



-- | Like 'find', but where the test can be monadic.
--
-- >>> findM (Just . C.isUpper) "teST"
-- Just (Just 'S')
-- >>> findM (Just . C.isUpper) "test"
-- Just Nothing
-- >>> findM (Just . const True) ["x",undefined]
-- Just (Just "x")
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)


-- | Drops the given suffix from a list.
--   It returns the original sequence if the sequence doesn't end with the given suffix.
--
-- >>> dropSuffix "!" "Hello World!"
-- "Hello World"
-- >>> dropSuffix "!" "Hello World!!"
-- "Hello World!"
-- >>> dropSuffix "!" "Hello World."
-- "Hello World."
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

-- | Return the prefix of the second list if its suffix
--   matches the entire first list.
--
-- >>> stripSuffix "bar" "foobar"
-- Just "foo"
-- >>> stripSuffix ""    "baz"
-- Just "baz"
-- >>> stripSuffix "foo" "quux"
-- Nothing
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)


-- | Drops the given prefix from a list.
--   It returns the original sequence if the sequence doesn't start with the given prefix.
--
-- >>> dropPrefix "Mr. " "Mr. Men"
-- "Men"
-- >>> dropPrefix "Mr. " "Dr. Men"
-- "Dr. Men"
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



-- | Break a list into pieces separated by the first
-- list argument, consuming the delimiter. An empty delimiter is
-- invalid, and will cause an error to be raised.
--
-- >>> splitOn "\r\n" "a\r\nb\r\nd\r\ne"
-- ["a","b","d","e"]
-- >>> splitOn "aaa"  "aaaXaaaXaaaXaaa"
-- ["","X","X","X",""]
-- >>> splitOn "x"    "x"
-- ["",""]
-- >>> splitOn "x"    ""
-- [""]
--
-- prop> \s x -> s /= "" ==> intercalate s (splitOn s x) == x
-- prop> \c x -> splitOn [c] x                           == split (==c) x
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


-- | Splits a list into components delimited by separators,
-- where the predicate returns True for a separator element.  The
-- resulting components do not contain the separators.  Two adjacent
-- separators result in an empty component in the output.
--
-- >>> split (== 'a') "aabbaca"
-- ["","","bb","c",""]
-- >>> split (== 'a') ""
-- [""]
-- >>> split (== ':') "::xyz:abc::123::"
-- ["","","xyz","abc","","123","",""]
-- >>> split (== ',') "my,list,here"
-- ["my","list","here"]
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 = [[]]


-- | Find the first instance of @needle@ in @haystack@.
-- The first element of the returned tuple
-- is the prefix of @haystack@ before @needle@ is matched.  The second
-- is the remainder of @haystack@, starting with the match.
-- If you want the remainder /without/ the match, use 'stripInfix'.
--
-- >>> breakOn "::" "a::b::c"
-- ("a","::b::c")
-- >>> breakOn "/" "foobar"
-- ("foobar","")
--
-- prop> \needle haystack -> let (prefix,match) = breakOn needle haystack in prefix ++ match == haystack
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