module Data.Builder
( TextBuilder
, BlazeBuilder
, Builder (..)
, ToBuilder (..)
, textToBuilder
) where
import Data.Monoid (Monoid)
import qualified Data.Text as T
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Builder as TB
import qualified Data.ByteString as S
import qualified Data.ByteString.Lazy as L
import qualified Blaze.ByteString.Builder as BB
import qualified Blaze.ByteString.Builder.Char.Utf8 as BB
type TextBuilder = TB.Builder
type BlazeBuilder = BB.Builder
class Monoid builder => Builder builder lazy | builder -> lazy, lazy -> builder where
builderToLazy :: builder -> lazy
flushBuilder :: builder
instance Builder TB.Builder TL.Text where
builderToLazy = TB.toLazyText
flushBuilder = TB.flush
instance Builder BB.Builder L.ByteString where
builderToLazy = BB.toLazyByteString
flushBuilder = BB.flush
class ToBuilder value builder where
toBuilder :: value -> builder
instance ToBuilder TB.Builder TB.Builder where
toBuilder = id
instance ToBuilder T.Text TB.Builder where
toBuilder = TB.fromText
instance ToBuilder TL.Text TB.Builder where
toBuilder = TB.fromLazyText
instance ToBuilder Char TB.Builder where
toBuilder = TB.singleton
instance (a ~ Char) => ToBuilder [a] TB.Builder where
toBuilder = TB.fromString
instance ToBuilder BB.Builder BB.Builder where
toBuilder = id
instance ToBuilder T.Text BB.Builder where
toBuilder = BB.fromText
instance ToBuilder TL.Text BB.Builder where
toBuilder = BB.fromLazyText
instance ToBuilder Char BB.Builder where
toBuilder = BB.fromChar
instance (a ~ Char) => ToBuilder [a] BB.Builder where
toBuilder = BB.fromString
instance ToBuilder S.ByteString BB.Builder where
toBuilder = BB.fromByteString
instance ToBuilder L.ByteString BB.Builder where
toBuilder = BB.fromLazyByteString
textToBuilder :: ToBuilder T.Text builder => T.Text -> builder
textToBuilder = toBuilder