module Data.Sv.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 BL
import Data.ByteString.Builder (Builder)
import qualified Data.ByteString.Builder as Builder
import Data.Text (Text)
import qualified Data.Text.Encoding as Text
import 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)
escape :: Lens' c (Escaper' a)
build = printOptions . build
escape = printOptions . escape
instance HasPrintOptions (PrintOptions a) a where
printOptions = id
build f (PrintOptions x1 x2) = fmap (\ y -> PrintOptions y x2) (f x1)
escape f (PrintOptions x1 x2) = fmap (PrintOptions x1) (f x2)
defaultPrintOptions :: PrintOptions BS.ByteString
defaultPrintOptions = utf8PrintOptions
utf8PrintOptions :: PrintOptions BS.ByteString
utf8PrintOptions = PrintOptions Builder.byteString escapeUtf8
utf8LazyPrintOptions :: PrintOptions BL.ByteString
utf8LazyPrintOptions = PrintOptions Builder.lazyByteString escapeUtf8Lazy
textPrintOptions :: PrintOptions Text
textPrintOptions = PrintOptions (Builder.byteString . Text.encodeUtf8) escapeText
stringPrintOptions :: PrintOptions String
stringPrintOptions = PrintOptions Builder.stringUtf8 escapeString