slackbuilder/lib/SlackBuilder/Trans.hs

93 lines
3.3 KiB
Haskell
Raw Normal View History

2023-12-23 22:15:10 +01:00
{- This Source Code Form is subject to the terms of the Mozilla Public License,
v. 2.0. If a copy of the MPL was not distributed with this file, You can
obtain one at https://mozilla.org/MPL/2.0/. -}
2023-12-12 18:51:44 +01:00
-- | Transformers and exceptions.
2023-08-15 10:33:19 +02:00
module SlackBuilder.Trans
2023-12-11 08:14:55 +01:00
( SlackBuilderException(..)
, SlackBuilderT(..)
, relativeToRepository
2023-08-15 10:33:19 +02:00
) where
import Control.Monad.Trans.Reader (ReaderT(..), asks)
2023-12-11 08:14:55 +01:00
import Data.Text (Text)
2024-08-20 22:36:43 +02:00
import qualified Data.Text as Text
2023-08-15 10:33:19 +02:00
import SlackBuilder.Config
import Control.Monad.IO.Class (MonadIO(..))
2023-10-01 17:19:06 +02:00
import Control.Monad.Catch (MonadCatch(..), MonadThrow(..))
2023-12-11 08:14:55 +01:00
import Control.Exception (Exception(..))
import System.FilePath ((</>))
2024-03-03 17:12:29 +01:00
import Text.URI (URI)
2024-08-20 22:36:43 +02:00
import qualified Text.URI as URI
2024-08-08 11:03:02 +02:00
import qualified Codec.Compression.Lzma as Lzma
2023-12-11 08:14:55 +01:00
2024-03-03 17:12:29 +01:00
data SlackBuilderException
= UpdaterNotFound Text
2024-09-20 17:52:09 +02:00
| UnsupportedUrlType URI
2024-08-08 11:03:02 +02:00
| LzmaDecompressionFailed Lzma.LzmaRet
2023-12-11 08:14:55 +01:00
deriving Show
instance Exception SlackBuilderException
2024-08-20 22:36:43 +02:00
where
displayException (UpdaterNotFound updateName) = Text.unpack
$ Text.concat ["Requested package \"", updateName, "\" was not found"]
2024-09-20 17:52:09 +02:00
displayException (UnsupportedUrlType givenURI) = Text.unpack
2024-08-20 22:36:43 +02:00
$ "Only https URLs are supported, got: " <> URI.render givenURI
displayException (LzmaDecompressionFailed Lzma.LzmaRetOK) =
"Operation completed successfully"
displayException (LzmaDecompressionFailed Lzma.LzmaRetStreamEnd) =
"End of stream was reached"
displayException (LzmaDecompressionFailed Lzma.LzmaRetUnsupportedCheck) =
"Cannot calculate the integrity check"
displayException (LzmaDecompressionFailed Lzma.LzmaRetGetCheck) =
"Integrity check type is now available"
displayException (LzmaDecompressionFailed Lzma.LzmaRetMemError) =
"Cannot allocate memory"
displayException (LzmaDecompressionFailed Lzma.LzmaRetMemlimitError) =
"Memory usage limit was reached"
displayException (LzmaDecompressionFailed Lzma.LzmaRetFormatError) =
"File format not recognized"
displayException (LzmaDecompressionFailed Lzma.LzmaRetOptionsError) =
"Invalid or unsupported options"
displayException (LzmaDecompressionFailed Lzma.LzmaRetDataError) =
"Data is corrupt"
displayException (LzmaDecompressionFailed Lzma.LzmaRetBufError) =
"No progress is possible"
displayException (LzmaDecompressionFailed Lzma.LzmaRetProgError) =
"Programming error"
2023-08-15 10:33:19 +02:00
newtype SlackBuilderT a = SlackBuilderT
{ runSlackBuilderT :: ReaderT Settings IO a
}
relativeToRepository :: FilePath -> SlackBuilderT FilePath
relativeToRepository filePath =
(</> filePath) <$> SlackBuilderT (asks repository)
2023-08-15 10:33:19 +02:00
instance Functor SlackBuilderT
where
fmap f (SlackBuilderT slackBuilderT) = SlackBuilderT $ f <$> slackBuilderT
instance Applicative SlackBuilderT
where
pure = SlackBuilderT . pure
(SlackBuilderT f) <*> (SlackBuilderT x) = SlackBuilderT $ f <*> x
instance Monad SlackBuilderT
where
return = pure
(SlackBuilderT x) >>= f = SlackBuilderT $ x >>= runSlackBuilderT . f
instance MonadIO SlackBuilderT
where
liftIO = SlackBuilderT . liftIO
2023-10-01 17:19:06 +02:00
instance MonadThrow SlackBuilderT
where
throwM = SlackBuilderT . throwM
instance MonadCatch SlackBuilderT
where
catch (SlackBuilderT action) handler =
SlackBuilderT $ catch action $ runSlackBuilderT . handler