|
| 1 | +module APrelude |
| 2 | + ( Text, |
| 3 | + ByteString, |
| 4 | + LByteString, |
| 5 | + Generic, |
| 6 | + fromMaybe, |
| 7 | + putErrLn, |
| 8 | + fromRight, |
| 9 | + isJust, |
| 10 | + decodeUtf8, |
| 11 | + encodeUtf8, |
| 12 | + MVar, |
| 13 | + readMVar, |
| 14 | + swapMVar, |
| 15 | + newMVar, |
| 16 | + STM, |
| 17 | + atomically, |
| 18 | + ThreadId, |
| 19 | + forkFinally, |
| 20 | + forkIO, |
| 21 | + killThread, |
| 22 | + threadDelay, |
| 23 | + (>=>), |
| 24 | + when, |
| 25 | + forever, |
| 26 | + void, |
| 27 | + panic, |
| 28 | + SomeException, |
| 29 | + throwError, |
| 30 | + liftIO, |
| 31 | + runExceptT, |
| 32 | + unpack, |
| 33 | + pack, |
| 34 | + showText, |
| 35 | + showBS, |
| 36 | + LBS.fromStrict, |
| 37 | + stdin, |
| 38 | + stdout, |
| 39 | + stderr, |
| 40 | + hPutStrLn, |
| 41 | + Word16, |
| 42 | + forM, |
| 43 | + forM_, |
| 44 | + takeMVar, |
| 45 | + newEmptyMVar, |
| 46 | + wait, |
| 47 | + headDef, |
| 48 | + tailSafe, |
| 49 | + withAsync, |
| 50 | + putMVar, |
| 51 | + die, |
| 52 | + myThreadId, |
| 53 | + replicateM, |
| 54 | + bracket, |
| 55 | + ) |
| 56 | +where |
| 57 | + |
| 58 | +import Control.Concurrent (ThreadId, forkFinally, forkIO, killThread, myThreadId, threadDelay) |
| 59 | +import Control.Concurrent.Async (wait, withAsync) |
| 60 | +import Control.Concurrent.MVar (MVar, newEmptyMVar, newMVar, putMVar, readMVar, swapMVar, takeMVar) |
| 61 | +import Control.Concurrent.STM (STM, atomically) |
| 62 | +import Control.Exception (Exception, SomeException, bracket, throw) |
| 63 | +import Control.Monad (forM, forM_, forever, replicateM, void, when, (>=>)) |
| 64 | +import Control.Monad.Error.Class (throwError) |
| 65 | +import Control.Monad.Except (runExceptT) |
| 66 | +import Control.Monad.IO.Class (liftIO) |
| 67 | +import Data.ByteString (ByteString) |
| 68 | +import qualified Data.ByteString.Char8 as BS |
| 69 | +import qualified Data.ByteString.Lazy as LBS |
| 70 | +import Data.Either (fromRight) |
| 71 | +import Data.Maybe (fromMaybe, isJust, listToMaybe) |
| 72 | +import Data.Text (Text, pack, unpack) |
| 73 | +import qualified Data.Text as T |
| 74 | +import Data.Text.Encoding |
| 75 | +import Data.Word (Word16) |
| 76 | +import GHC.Generics (Generic) |
| 77 | +import System.Exit (die) |
| 78 | +import System.IO (hPutStrLn, stderr, stdin, stdout) |
| 79 | + |
| 80 | +showBS :: (Show a) => a -> BS.ByteString |
| 81 | +showBS = BS.pack . show |
| 82 | + |
| 83 | +showText :: (Show a) => a -> Text |
| 84 | +showText = T.pack . show |
| 85 | + |
| 86 | +type LByteString = LBS.ByteString |
| 87 | + |
| 88 | +-- | Uncatchable exceptions thrown and never caught. |
| 89 | +newtype FatalError = FatalError {fatalErrorMessage :: Text} |
| 90 | + deriving (Show) |
| 91 | + |
| 92 | +instance Exception FatalError |
| 93 | + |
| 94 | +panic :: Text -> a |
| 95 | +panic a = throw (FatalError a) |
| 96 | + |
| 97 | +putErrLn :: Text -> IO () |
| 98 | +putErrLn = hPutStrLn stderr . unpack |
| 99 | + |
| 100 | +headDef :: a -> [a] -> a |
| 101 | +headDef def = fromMaybe def . listToMaybe |
| 102 | + |
| 103 | +tailSafe :: [a] -> [a] |
| 104 | +tailSafe = drop 1 |
0 commit comments