Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 35ec3cfa49
Fetching contributors…

Cannot retrieve contributors at this time

file 165 lines (128 sloc) 4.078 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
{-# LANGUAGE FlexibleInstances, OverloadedStrings #-}

-- |
-- Module : Data.Text.Buildable
-- Copyright : (c) 2011 MailRank, Inc.
--
-- License : BSD-style
-- Maintainer : bos@mailrank.com
-- Stability : experimental
-- Portability : GHC
--
-- Types that can be rendered to a 'Builder'.

module Data.Text.Buildable
    (
      Buildable(..)
    ) where

import Data.Int (Int8, Int16, Int32, Int64)
import Data.Ratio (Ratio, denominator, numerator)
import Data.Text.Format.Functions ((<>))
import Data.Text.Format.Int (decimal, hexadecimal)
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.Double.Conversion as C
import qualified Data.Text as ST
import qualified Data.Text.Lazy as LT

-- | The class of types that can be rendered to a 'Builder'.
class Buildable p where
    build :: p -> Builder

instance Buildable Builder where
    build = id

instance Buildable LT.Text where
    build = fromLazyText
    {-# INLINE build #-}

instance Buildable ST.Text where
    build = fromText
    {-# INLINE build #-}

instance Buildable Char where
    build = singleton
    {-# INLINE build #-}

instance Buildable [Char] where
    build = fromString
    {-# INLINE build #-}

instance (Integral a) => Buildable (Hex a) where
    build = hexadecimal
    {-# INLINE build #-}

instance Buildable Int8 where
    build = decimal
    {-# INLINE build #-}

instance Buildable Int16 where
    build = decimal
    {-# INLINE build #-}

instance Buildable Int32 where
    build = decimal
    {-# INLINE build #-}

instance Buildable Int where
    build = decimal
    {-# INLINE build #-}

instance Buildable Int64 where
    build = decimal
    {-# INLINE build #-}

instance Buildable Integer where
    build = decimal
    {-# INLINE build #-}

instance Buildable Word8 where
    build = decimal
    {-# INLINE build #-}

instance Buildable Word16 where
    build = decimal
    {-# INLINE build #-}

instance Buildable Word32 where
    build = decimal
    {-# INLINE build #-}

instance Buildable Word where
    build = decimal
    {-# INLINE build #-}

instance Buildable Word64 where
    build = decimal
    {-# INLINE build #-}

instance (Integral a, Buildable a) => Buildable (Ratio a) where
    {-# SPECIALIZE instance Buildable (Ratio Integer) #-}
    build a = build (numerator a) <> singleton '/' <> build (denominator a)

instance Buildable Float where
    build = fromText . C.toShortest . realToFrac
    {-# INLINE build #-}

instance Buildable Double where
    build = fromText . C.toShortest
    {-# INLINE build #-}

instance Buildable DiffTime where
    build = build . Shown
    {-# INLINE build #-}

instance Buildable NominalDiffTime where
    build = build . Shown
    {-# INLINE build #-}

instance Buildable UTCTime where
    build = build . Shown
    {-# INLINE build #-}

instance Buildable UniversalTime where
    build = build . Shown . getModJulianDate
    {-# INLINE build #-}

instance Buildable Day where
    build = fromString . showGregorian
    {-# INLINE build #-}

instance (Show a) => Buildable (Shown a) where
    build = fromString . show . shown
    {-# INLINE build #-}

instance Buildable TimeOfDay where
    build = build . Shown
    {-# INLINE build #-}

instance Buildable TimeZone where
    build = build . Shown
    {-# INLINE build #-}

instance Buildable LocalTime where
    build = build . Shown
    {-# INLINE build #-}

instance Buildable ZonedTime where
    build = build . Shown
    {-# INLINE build #-}

instance Buildable IntPtr where
    build p = fromText "0x" <> hexadecimal p

instance Buildable WordPtr where
    build p = fromText "0x" <> hexadecimal p

instance Buildable (Ptr a) where
    build = build . ptrToWordPtr
Something went wrong with that request. Please try again.