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


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


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


--------------------------------------------------------------------------------
data Item a = Item
    { forall a. Item a -> Identifier
itemIdentifier :: Identifier
    , forall a. Item a -> a
itemBody       :: a
    } deriving (Int -> Item a -> ShowS
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, forall a b. a -> Item b -> Item a
forall a b. (a -> b) -> Item a -> Item b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> Item b -> Item a
$c<$ :: forall a b. a -> Item b -> Item a
fmap :: forall a b. (a -> b) -> Item a -> Item b
$cfmap :: forall a b. (a -> b) -> Item a -> Item b
Functor, forall a. Eq a => a -> Item a -> Bool
forall a. Num a => Item a -> a
forall a. Ord a => Item a -> a
forall m. Monoid m => Item m -> m
forall a. Item a -> Bool
forall a. Item a -> Int
forall a. Item a -> [a]
forall a. (a -> a -> a) -> Item a -> a
forall m a. Monoid m => (a -> m) -> Item a -> m
forall b a. (b -> a -> b) -> b -> Item a -> b
forall a b. (a -> b -> b) -> b -> Item a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: forall a. Num a => Item a -> a
$cproduct :: forall a. Num a => Item a -> a
sum :: forall a. Num a => Item a -> a
$csum :: forall a. Num a => Item a -> a
minimum :: forall a. Ord a => Item a -> a
$cminimum :: forall a. Ord a => Item a -> a
maximum :: forall a. Ord a => Item a -> a
$cmaximum :: forall a. Ord a => Item a -> a
elem :: forall a. Eq a => a -> Item a -> Bool
$celem :: forall a. Eq a => a -> Item a -> Bool
length :: forall a. Item a -> Int
$clength :: forall a. Item a -> Int
null :: forall a. Item a -> Bool
$cnull :: forall a. Item a -> Bool
toList :: forall a. Item a -> [a]
$ctoList :: forall a. Item a -> [a]
foldl1 :: forall a. (a -> a -> a) -> Item a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> Item a -> a
foldr1 :: forall a. (a -> a -> a) -> Item a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> Item a -> a
foldl' :: forall b a. (b -> a -> b) -> b -> Item a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> Item a -> b
foldl :: forall b a. (b -> a -> b) -> b -> Item a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> Item a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> Item a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> Item a -> b
foldr :: forall a b. (a -> b -> b) -> b -> Item a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> Item a -> b
foldMap' :: forall m a. Monoid m => (a -> m) -> Item a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> Item a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> Item a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> Item a -> m
fold :: forall m. Monoid m => Item m -> m
$cfold :: forall m. Monoid m => Item m -> m
Foldable, Functor Item
Foldable Item
forall (t :: * -> *).
Functor t
-> Foldable t
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a. Monad m => Item (m a) -> m (Item a)
forall (f :: * -> *) a. Applicative f => Item (f a) -> f (Item a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Item a -> m (Item b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Item a -> f (Item b)
sequence :: forall (m :: * -> *) a. Monad m => Item (m a) -> m (Item a)
$csequence :: forall (m :: * -> *) a. Monad m => Item (m a) -> m (Item a)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Item a -> m (Item b)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Item a -> m (Item b)
sequenceA :: forall (f :: * -> *) a. Applicative f => Item (f a) -> f (Item a)
$csequenceA :: forall (f :: * -> *) a. Applicative f => Item (f a) -> f (Item a)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Item a -> f (Item b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Item a -> f (Item b)
Traversable)

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


--------------------------------------------------------------------------------
itemSetBody :: a -> Item b -> Item a
itemSetBody :: forall a b. a -> Item b -> Item a
itemSetBody a
x (Item Identifier
i b
_) = 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 :: forall a b. (a -> Compiler b) -> Item a -> Compiler (Item b)
withItemBody = forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse