From 4a6dbda7d3d33fca1f35c0bd080cccbd40f045f7 Mon Sep 17 00:00:00 2001 From: Tissevert Date: Sat, 15 Feb 2020 10:22:42 +0100 Subject: [PATCH] Move Error type from Pages to Navigation as a candidate for MonadFail required by PDFContent defined there --- src/PDF/Object/Navigation.hs | 13 +++++++++++-- src/PDF/Pages.hs | 13 +++---------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/PDF/Object/Navigation.hs b/src/PDF/Object/Navigation.hs index 96d8230..19805b3 100644 --- a/src/PDF/Object/Navigation.hs +++ b/src/PDF/Object/Navigation.hs @@ -1,8 +1,10 @@ {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE ConstraintKinds #-} {-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE GeneralizedNewtypeDeriving #-} module PDF.Object.Navigation ( - (//) + Error(..) + , (//) , dictionaryById , getDictionary , getField @@ -13,6 +15,8 @@ module PDF.Object.Navigation ( ) where import Codec.Compression.Zlib (decompress) +import Control.Applicative (Alternative(..)) +import Control.Monad (MonadPlus(..)) import Control.Monad.Reader (MonadReader(..)) import Control.Monad.Fail (MonadFail(..)) import Data.ByteString (ByteString) @@ -27,7 +31,12 @@ import PDF.Output (ObjectId) import Prelude hiding (fail) import Text.Printf (printf) -type PDFContent m = (MonadReader Content m, MonadFail m) +type PDFContent m = (Alternative m, MonadReader Content m, MonadFail m) +newtype Error a = Error { + runError :: Either String a + } deriving (Alternative, Functor, Applicative, Monad, MonadPlus) +instance MonadFail Error where + fail = Error . Left castDictionary :: MonadFail m => Object -> m Dictionary castDictionary (Direct (Dictionary aDict)) = return aDict diff --git a/src/PDF/Pages.hs b/src/PDF/Pages.hs index e24281e..7c9b0c2 100755 --- a/src/PDF/Pages.hs +++ b/src/PDF/Pages.hs @@ -1,13 +1,12 @@ {-# LANGUAGE NamedFieldPuns #-} -{-# LANGUAGE GeneralizedNewtypeDeriving #-} module PDF.Pages ( Page(..) , get , getAll ) where -import Control.Applicative (Alternative(..), (<|>)) -import Control.Monad (MonadPlus(..), foldM) +import Control.Applicative ((<|>)) +import Control.Monad (foldM) import Control.Monad.Fail (MonadFail(..)) import Control.Monad.RWS (RWST(..), evalRWST, modify) import qualified Control.Monad.RWS as RWS (get) @@ -24,25 +23,19 @@ import PDF.Object ( , Name(..) ,) import PDF.Object.Navigation ( - (//), dictionaryById, getDictionary, getField, follow, openStream, origin + Error(..), (//), dictionaryById, getDictionary, getField, follow, openStream, origin ) import PDF.Output (ObjectId(..)) import Prelude hiding (fail) import Text.Printf (printf) type CachedFonts = Map ObjectId Font -newtype Error a = Error { - runError :: Either String a - } deriving (Alternative, Functor, Applicative, Monad, MonadPlus) type T = RWST Content () CachedFonts Error data Page = Page { contents :: [Text] , source :: ObjectId } -instance MonadFail Error where - fail = Error . Left - getFontDictionary :: Dictionary -> T Dictionary getFontDictionary pageDict = ((pageDict // ["Resources", "Font"]) >>= getDictionary)