{-# LANGUAGE GADTs #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
module Fmt.Internal.Core where
#if __GLASGOW_HASKELL__ < 804
import Data.Monoid ((<>))
#endif
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.IO as TL
import qualified Data.Text.Lazy.Encoding as TL
import Data.Text.Lazy.Builder hiding (fromString)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BSL
import qualified Data.ByteString.Builder as BB
import Formatting.Buildable (Buildable(..))
class FromBuilder a where
fromBuilder :: Builder -> a
instance FromBuilder Builder where
fromBuilder = id
{-# INLINE fromBuilder #-}
instance (a ~ Char) => FromBuilder [a] where
fromBuilder = TL.unpack . toLazyText
{-# INLINE fromBuilder #-}
instance FromBuilder T.Text where
fromBuilder = TL.toStrict . toLazyText
{-# INLINE fromBuilder #-}
instance FromBuilder TL.Text where
fromBuilder = toLazyText
{-# INLINE fromBuilder #-}
instance FromBuilder BS.ByteString where
fromBuilder = T.encodeUtf8 . TL.toStrict . toLazyText
{-# INLINE fromBuilder #-}
instance FromBuilder BSL.ByteString where
fromBuilder = TL.encodeUtf8 . toLazyText
{-# INLINE fromBuilder #-}
instance FromBuilder BB.Builder where
fromBuilder = TL.encodeUtf8Builder . toLazyText
{-# INLINE fromBuilder #-}
instance (a ~ ()) => FromBuilder (IO a) where
fromBuilder = TL.putStr . toLazyText
{-# INLINE fromBuilder #-}
(+|) :: (FromBuilder b) => Builder -> Builder -> b
(+|) str rest = fromBuilder (str <> rest)
(|+) :: (Buildable a, FromBuilder b) => a -> Builder -> b
(|+) a rest = fromBuilder (build a <> rest)
infixr 1 +|
infixr 1 |+
(+||) :: (FromBuilder b) => Builder -> Builder -> b
(+||) str rest = str +| rest
{-# INLINE (+||) #-}
(||+) :: (Show a, FromBuilder b) => a -> Builder -> b
(||+) a rest = show a |+ rest
{-# INLINE (||+) #-}
infixr 1 +||
infixr 1 ||+
(|++|) :: (Buildable a, FromBuilder b) => a -> Builder -> b
(|++|) a rest = fromBuilder (build a <> rest)
{-# INLINE (|++|) #-}
(||++||) :: (Show a, FromBuilder b) => a -> Builder -> b
(||++||) a rest = show a |+ rest
{-# INLINE (||++||) #-}
(||++|) :: (Buildable a, FromBuilder b) => a -> Builder -> b
(||++|) a rest = a |++| rest
{-# INLINE (||++|) #-}
(|++||) :: (Show a, FromBuilder b) => a -> Builder -> b
(|++||) a rest = a ||++|| rest
{-# INLINE (|++||) #-}
infixr 1 |++|
infixr 1 ||++||
infixr 1 ||++|
infixr 1 |++||
fmt :: FromBuilder b => Builder -> b
fmt = fromBuilder
{-# INLINE fmt #-}
fmtLn :: FromBuilder b => Builder -> b
fmtLn = fromBuilder . (<> "\n")
{-# INLINE fmtLn #-}
pretty :: (Buildable a, FromBuilder b) => a -> b
pretty = fmt . build
{-# INLINE pretty #-}
prettyLn :: (Buildable a, FromBuilder b) => a -> b
prettyLn = fmtLn . build
{-# INLINE prettyLn #-}