module Formatting.Buildable
(
Buildable(..)
) where
#if MIN_VERSION_base(4,8,0)
import qualified Data.ByteString.Lazy as L
import Data.Void (Void, absurd)
#endif
import Data.Monoid (mempty)
import Data.Int (Int8, Int16, Int32, Int64)
import Data.Fixed (Fixed, HasResolution, showFixed)
import Data.Ratio (Ratio, denominator, numerator)
import qualified Data.Text.Format.Functions as F ((<>))
import Data.Text.Format.Int (decimal, hexadecimal, integer)
import Data.Text.Format.Types (Hex(..), Shown(..))
import Data.Text.Lazy.Builder
import Data.Time.Calendar (Day, showGregorian)
import Data.Time.Clock (DiffTime, NominalDiffTime, UTCTime, UniversalTime)
import Data.Time.Clock (getModJulianDate)
import Data.Time.LocalTime (LocalTime, TimeOfDay, TimeZone, ZonedTime)
import Data.Word (Word, Word8, Word16, Word32, Word64)
import Foreign.Ptr (IntPtr, WordPtr, Ptr, ptrToWordPtr)
import qualified Data.Text as ST
import qualified Data.Text.Lazy as LT
import qualified Data.Text.Encoding as T
import qualified Data.ByteString.Lazy.Builder as L
class Buildable p where
build :: p -> Builder
instance Buildable Builder where
build = id
#if MIN_VERSION_base(4,8,0)
instance Buildable Void where
build = absurd
#endif
instance Buildable LT.Text where
build = fromLazyText
instance Buildable ST.Text where
build = fromText
instance Buildable Char where
build = singleton
instance Buildable [Char] where
build = fromString
instance (Integral a) => Buildable (Hex a) where
build = hexadecimal
instance Buildable Int8 where
build = decimal
instance Buildable Int16 where
build = decimal
instance Buildable Int32 where
build = decimal
instance Buildable Int where
build = decimal
instance Buildable Int64 where
build = decimal
instance Buildable Integer where
build = integer 10
instance (HasResolution a) => Buildable (Fixed a) where
build = build . showFixed False
instance Buildable Word8 where
build = decimal
instance Buildable Word16 where
build = decimal
instance Buildable Word32 where
build = decimal
instance Buildable Word where
build = decimal
instance Buildable Word64 where
build = decimal
instance (Integral a, Buildable a) => Buildable (Ratio a) where
build a = build (numerator a) F.<> singleton '/' F.<> build (denominator a)
instance Buildable Float where
build = fromText . T.decodeUtf8 . L.toStrict . L.toLazyByteString . L.floatDec
;
instance Buildable Double where
build = fromText . T.decodeUtf8 . L.toStrict . L.toLazyByteString . L.doubleDec
instance Buildable DiffTime where
build = build . Shown
instance Buildable NominalDiffTime where
build = build . Shown
instance Buildable UTCTime where
build = build . Shown
instance Buildable UniversalTime where
build = build . Shown . getModJulianDate
instance Buildable Day where
build = fromString . showGregorian
instance (Show a) => Buildable (Shown a) where
build = fromString . show . shown
instance (Buildable a) => Buildable (Maybe a) where
build Nothing = mempty
build (Just v) = build v
instance Buildable TimeOfDay where
build = build . Shown
instance Buildable TimeZone where
build = build . Shown
instance Buildable LocalTime where
build = build . Shown
instance Buildable ZonedTime where
build = build . Shown
instance Buildable IntPtr where
build p = fromText "0x" F.<> hexadecimal p
instance Buildable WordPtr where
build p = fromText "0x" F.<> hexadecimal p
instance Buildable (Ptr a) where
build = build . ptrToWordPtr
instance Buildable Bool where
build True = fromText "True"
build False = fromText "False"