{-# LANGUAGE CPP #-}
-- |
--
-- Builder structures for 'Whitespace'
--
module Waargonaut.Encode.Builder.Whitespace
  ( whitespaceBuilder
  , wsBuilder
  , wsRemover
  ) where

#if !MIN_VERSION_base(4,11,0)
import           Data.Monoid                     (Monoid)
#endif

import           Waargonaut.Types.Whitespace     (WS (..), Whitespace (..))

import           Waargonaut.Encode.Builder.Types (Builder (..))

-- | Create a 'Data.ByteString.Builder' from a 'Whitespace'
whitespaceBuilder :: Monoid b => Builder t b -> Whitespace -> b
whitespaceBuilder :: Builder t b -> Whitespace -> b
whitespaceBuilder Builder t b
bldr Whitespace
Space          = Builder t b -> Char -> b
forall t b. Builder t b -> Char -> b
fromChar Builder t b
bldr Char
' '
whitespaceBuilder Builder t b
bldr Whitespace
HorizontalTab  = Builder t b -> Char -> b
forall t b. Builder t b -> Char -> b
fromChar Builder t b
bldr Char
'\t'
whitespaceBuilder Builder t b
bldr Whitespace
LineFeed       = Builder t b -> Char -> b
forall t b. Builder t b -> Char -> b
fromChar Builder t b
bldr Char
'\f'
whitespaceBuilder Builder t b
bldr Whitespace
CarriageReturn = Builder t b -> Char -> b
forall t b. Builder t b -> Char -> b
fromChar Builder t b
bldr Char
'\r'
whitespaceBuilder Builder t b
bldr Whitespace
NewLine        = Builder t b -> Char -> b
forall t b. Builder t b -> Char -> b
fromChar Builder t b
bldr Char
'\n'
{-# INLINE whitespaceBuilder #-}

-- | Reconstitute the given whitespace into its original form.
wsBuilder :: Monoid b => Builder t b -> WS -> b
wsBuilder :: Builder t b -> WS -> b
wsBuilder Builder t b
bldr (WS Vector Whitespace
ws) = (Whitespace -> b) -> Vector Whitespace -> b
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (Builder t b -> Whitespace -> b
forall b t. Monoid b => Builder t b -> Whitespace -> b
whitespaceBuilder Builder t b
bldr) Vector Whitespace
ws
{-# INLINE wsBuilder #-}

-- | Remove any whitespace. Minification for free, yay!
wsRemover :: Monoid b => Builder t b -> WS -> b
wsRemover :: Builder t b -> WS -> b
wsRemover Builder t b
_ = b -> WS -> b
forall a b. a -> b -> a
const b
forall a. Monoid a => a
mempty
{-# INLINE wsRemover #-}