From c61333dd11f3ba8a4c8155cb58d7b081d2096a94 Mon Sep 17 00:00:00 2001 From: Alexander Foremny Date: Wed, 5 Jun 2024 18:00:05 +0200 Subject: add `Value` instance for `readFile` --- src/Store/Store.hs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'src/Store/Store.hs') diff --git a/src/Store/Store.hs b/src/Store/Store.hs index e0bbdfb..1ec59ef 100644 --- a/src/Store/Store.hs +++ b/src/Store/Store.hs @@ -13,13 +13,14 @@ where import Bindings.Libgit2 qualified as B import Control.Arrow (first) -import Control.Exception (Exception, finally) +import Control.Exception (Exception, finally, throw) import Control.Monad (when) import Control.Monad.Catch (MonadCatch, MonadMask, MonadThrow, throwM) import Control.Monad.Reader (MonadReader, ReaderT, ask, runReaderT) import Control.Monad.State (MonadState, StateT, evalStateT, get, modify) import Control.Monad.Trans (MonadIO, MonadTrans, lift, liftIO) import Control.Monad.Trans.Resource (MonadResource, runResourceT) +import Data.Aeson qualified as J import Data.ByteString qualified as B import Data.ByteString.Lazy qualified as LB import Data.ByteString.UTF8 qualified as B @@ -146,6 +147,11 @@ data InappropriateType = InappropriateType String FilePath instance Exception InappropriateType +data DecodeException = DecodeException String + deriving (Show) + +instance Exception DecodeException + class Readable a where readFile :: FilePath -> StoreM a @@ -158,6 +164,9 @@ instance Readable B.ByteString where instance Readable LB.ByteString where readFile = readFile' G.catBlobLazy +instance Readable J.Value where + readFile = fmap (either (throw . DecodeException) id . J.eitherDecode) . readFile + readFile' :: (G.BlobOid GB.LgRepo -> ReaderT GB.LgRepo IO a) -> FilePath -> -- cgit v1.2.3