on 9939708 help sign in
hakyll/src/Hakyll/Core/Target/Internal.hs
55 lines 1.7 kB history raw
1-- | Internal structure of a Target, not exported outside of the library
2--
3{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4module Hakyll.Core.Target.Internal
5 ( DependencyLookup
6 , TargetEnvironment (..)
7 , TargetM (..)
8 , Target
9 , runTarget
10 ) where
12import Control.Applicative (Applicative)
13import Control.Monad.Trans (MonadIO)
14import Control.Monad.Reader (ReaderT, runReaderT)
16import Hakyll.Core.Identifier
17import Hakyll.Core.ResourceProvider
19-- | A lookup with which we can get dependencies
20--
21type DependencyLookup a = Identifier -> a
23-- | Environment for the target monad
24--
25data TargetEnvironment a = TargetEnvironment
26 { targetIdentifier :: Identifier -- ^ Identifier
27 , targetDependencyLookup :: DependencyLookup a -- ^ Dependency lookup
28 , targetResourceProvider :: ResourceProvider -- ^ To get resources
29 }
31-- | Monad for targets. In this monad, the user can compose targets and describe
32-- how they should be created.
33--
34newtype TargetM a b = TargetM {unTargetM :: ReaderT (TargetEnvironment a) IO b}
35 deriving (Monad, Functor, Applicative, MonadIO)
37-- | Simplification of the 'TargetM' type for concrete cases: the type of the
38-- returned item should equal the type of the dependencies.
39--
40type Target a = TargetM a a
42-- | Run a target, yielding an actual result.
43--
44runTarget :: Target a
45 -> Identifier
46 -> DependencyLookup a
47 -> ResourceProvider
48 -> IO a
49runTarget target id' lookup' provider = runReaderT (unTargetM target) env
50 where
51 env = TargetEnvironment
52 { targetIdentifier = id'
53 , targetDependencyLookup = lookup'
54 , targetResourceProvider = provider
55 }