{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleInstances #-}
module Data.Svfactor.Print.Options (
PrintOptions (..)
, HasPrintOptions (..)
, defaultPrintOptions
, utf8PrintOptions
, utf8LazyPrintOptions
, textPrintOptions
, stringPrintOptions
) where
import Control.Lens
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as LBS
import Data.ByteString.Builder (Builder)
import qualified Data.ByteString.Builder as Builder
import Data.Text (Text)
import qualified Data.Text.Encoding as Text
import Data.Svfactor.Text.Escape (Escaper', escapeUtf8, escapeUtf8Lazy, escapeText, escapeString)
data PrintOptions a =
PrintOptions {
_build :: a -> Builder
, _escape :: Escaper' a
}
class HasPrintOptions c a | c -> a where
printOptions :: Lens' c (PrintOptions a)
build :: Lens' c (a -> Builder)
{-# INLINE build #-}
escape :: Lens' c (Escaper' a)
{-# INLINE escape #-}
build = printOptions . build
escape = printOptions . escape
instance HasPrintOptions (PrintOptions a) a where
printOptions = id
{-# INLINE printOptions #-}
build f (PrintOptions x1 x2) = fmap (\ y -> PrintOptions y x2) (f x1)
{-# INLINE build #-}
escape f (PrintOptions x1 x2) = fmap (PrintOptions x1) (f x2)
{-# INLINE escape #-}
defaultPrintOptions :: PrintOptions BS.ByteString
defaultPrintOptions = utf8PrintOptions
utf8PrintOptions :: PrintOptions BS.ByteString
utf8PrintOptions = PrintOptions Builder.byteString escapeUtf8
utf8LazyPrintOptions :: PrintOptions LBS.ByteString
utf8LazyPrintOptions = PrintOptions Builder.lazyByteString escapeUtf8Lazy
textPrintOptions :: PrintOptions Text
textPrintOptions = PrintOptions (Builder.byteString . Text.encodeUtf8) escapeText
stringPrintOptions :: PrintOptions String
stringPrintOptions = PrintOptions Builder.stringUtf8 escapeString