------------------------------------------------------------------------------
-- | Defines the 'RenderHeader' type class, with the 'renderHeader' method.
-- 'renderHeader' can be used to render basic header values (acting as
-- identity on 'ByteString's), but it will also work on lists of quality
-- values, which provides the necessary interface for rendering the full
-- possibilities of Accept headers.
module Network.HTTP.Media.RenderHeader
    ( RenderHeader (..)
    ) where

import           Data.ByteString (ByteString, intercalate)


------------------------------------------------------------------------------
-- | A class for header values, so they may be rendered to their 'ByteString'
-- representation. Lists of header values and quality-marked header values
-- will render appropriately.
class RenderHeader h where

    -- | Render a header value to a UTF-8 'ByteString'.
    renderHeader :: h -> ByteString

instance RenderHeader ByteString where
    renderHeader :: ByteString -> ByteString
renderHeader = ByteString -> ByteString
forall a. a -> a
id

instance RenderHeader h => RenderHeader [h] where
    renderHeader :: [h] -> ByteString
renderHeader = ByteString -> [ByteString] -> ByteString
intercalate ByteString
"," ([ByteString] -> ByteString)
-> ([h] -> [ByteString]) -> [h] -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (h -> ByteString) -> [h] -> [ByteString]
forall a b. (a -> b) -> [a] -> [b]
map h -> ByteString
forall h. RenderHeader h => h -> ByteString
renderHeader