{-# LANGUAGE RankNTypes #-}
module Waargonaut.Encode.Builder.CommaSep (commaSeparatedBuilder) where
import Data.Monoid ((<>))
import Waargonaut.Types.CommaSep (Comma, CommaSeparated (..),
Elem (..), Elems (..))
import Waargonaut.Encode.Builder.Types (Builder (..))
commaBuilder :: Builder t b -> b
commaBuilder b = fromChar b ','
{-# INLINE commaBuilder #-}
commaTrailingBuilder
:: ( Monoid b
, Foldable f
)
=> Builder t b
-> (Builder t b -> ws -> b)
-> f (Comma, ws)
-> b
commaTrailingBuilder bldr wsB =
foldMap ((commaBuilder bldr <>) . (wsB bldr) . snd)
commaSeparatedBuilder
:: forall ws a t b. Monoid b
=> Builder t b
-> Char
-> Char
-> (Builder t b -> ws -> b)
-> (Builder t b -> a -> b)
-> CommaSeparated ws a
-> b
commaSeparatedBuilder bldr op fin wsB aB (CommaSeparated lws sepElems) =
fromChar bldr op <> wsB bldr lws <> maybe mempty buildElems sepElems <> fromChar bldr fin
where
elemBuilder (Elem e eTrailing) =
aB bldr e <> commaTrailingBuilder bldr wsB eTrailing
buildElems (Elems es elst) =
foldMap elemBuilder es <> elemBuilder elst