--------------------------------------------------------------------------------
-- | An item is a combination of some content and its 'Identifier'. This way, we
-- can still use the 'Identifier' to access metadata.
{-# LANGUAGE DeriveDataTypeable #-}
module Hakyll.Core.Item
    ( Item (..)
    , itemSetBody
    , withItemBody
    ) where


--------------------------------------------------------------------------------
import           Data.Binary                   (Binary (..))
import           Data.Foldable                 (Foldable (..))
import           Data.Typeable                 (Typeable)
import           Prelude                       hiding (foldr)


--------------------------------------------------------------------------------
import           Hakyll.Core.Compiler.Internal
import           Hakyll.Core.Identifier


--------------------------------------------------------------------------------
data Item a = Item
    { Item a -> Identifier
itemIdentifier :: Identifier
    , Item a -> a
itemBody       :: a
    } deriving (Int -> Item a -> ShowS
[Item a] -> ShowS
Item a -> String
(Int -> Item a -> ShowS)
-> (Item a -> String) -> ([Item a] -> ShowS) -> Show (Item a)
forall a. Show a => Int -> Item a -> ShowS
forall a. Show a => [Item a] -> ShowS
forall a. Show a => Item a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Item a] -> ShowS
$cshowList :: forall a. Show a => [Item a] -> ShowS
show :: Item a -> String
$cshow :: forall a. Show a => Item a -> String
showsPrec :: Int -> Item a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> Item a -> ShowS
Show, Typeable)


--------------------------------------------------------------------------------
instance Functor Item where
    fmap :: (a -> b) -> Item a -> Item b
fmap f :: a -> b
f (Item i :: Identifier
i x :: a
x) = Identifier -> b -> Item b
forall a. Identifier -> a -> Item a
Item Identifier
i (a -> b
f a
x)


--------------------------------------------------------------------------------
instance Foldable Item where
    foldr :: (a -> b -> b) -> b -> Item a -> b
foldr f :: a -> b -> b
f z :: b
z (Item _ x :: a
x) = a -> b -> b
f a
x b
z


--------------------------------------------------------------------------------
instance Traversable Item where
    traverse :: (a -> f b) -> Item a -> f (Item b)
traverse f :: a -> f b
f (Item i :: Identifier
i x :: a
x) = Identifier -> b -> Item b
forall a. Identifier -> a -> Item a
Item Identifier
i (b -> Item b) -> f b -> f (Item b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
x


--------------------------------------------------------------------------------
instance Binary a => Binary (Item a) where
    put :: Item a -> Put
put (Item i :: Identifier
i x :: a
x) = Identifier -> Put
forall t. Binary t => t -> Put
put Identifier
i Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> a -> Put
forall t. Binary t => t -> Put
put a
x
    get :: Get (Item a)
get            = Identifier -> a -> Item a
forall a. Identifier -> a -> Item a
Item (Identifier -> a -> Item a) -> Get Identifier -> Get (a -> Item a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Identifier
forall t. Binary t => Get t
get Get (a -> Item a) -> Get a -> Get (Item a)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get a
forall t. Binary t => Get t
get


--------------------------------------------------------------------------------
itemSetBody :: a -> Item b -> Item a
itemSetBody :: a -> Item b -> Item a
itemSetBody x :: a
x (Item i :: Identifier
i _) = Identifier -> a -> Item a
forall a. Identifier -> a -> Item a
Item Identifier
i a
x


--------------------------------------------------------------------------------
-- | Perform a compiler action on the item body. This is the same as 'traverse',
-- but looks less intimidating.
--
-- > withItemBody = traverse
withItemBody :: (a -> Compiler b) -> Item a -> Compiler (Item b)
withItemBody :: (a -> Compiler b) -> Item a -> Compiler (Item b)
withItemBody = (a -> Compiler b) -> Item a -> Compiler (Item b)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse