{-# LANGUAGE CPP, FlexibleInstances, OverlappingInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# OPTIONS_HADDOCK not-home #-}

module Text.StringTemplate.Instances() where
import Text.StringTemplate.Classes

import qualified Data.Map as M
import Numeric
import qualified Data.ByteString.Char8 as B
import qualified Data.ByteString.Lazy.Char8 as LB
import Data.Ratio
import Data.Array
import Data.Maybe
import qualified Data.Foldable as F
import Data.Time
import Data.Void
import qualified Data.Text as T
import qualified Data.Text.Lazy as LT

#if !MIN_VERSION_time(1,5,0)
import System.Locale (defaultTimeLocale)
#endif


{--------------------------------------------------------------------
  Additional instances for items that may be set as StringTemplate
  attributes. The code should provide examples of how to proceed.
--------------------------------------------------------------------}

--Basics
instance ToSElem () where
    toSElem :: forall b. Stringable b => () -> SElem b
toSElem ()
_ = forall a. String -> SElem a
STR String
""

instance ToSElem Void where
    toSElem :: forall b. Stringable b => Void -> SElem b
toSElem = forall a. Void -> a
absurd

instance ToSElem Char where
    toSElem :: forall b. Stringable b => Char -> SElem b
toSElem = forall a. String -> SElem a
STR forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. a -> [a] -> [a]
:[])
    toSElemList :: forall b. Stringable b => String -> SElem b
toSElemList = forall a. String -> SElem a
STR

instance ToSElem LB.ByteString where
    toSElem :: forall b. Stringable b => ByteString -> SElem b
toSElem = forall a. ByteString -> SElem a
BS

instance ToSElem B.ByteString where
    toSElem :: forall b. Stringable b => ByteString -> SElem b
toSElem = forall a. ByteString -> SElem a
BS forall b c a. (b -> c) -> (a -> b) -> a -> c
. [ByteString] -> ByteString
LB.fromChunks forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. a -> [a] -> [a]
:[])

instance ToSElem LT.Text where
    toSElem :: forall b. Stringable b => Text -> SElem b
toSElem = forall a. Text -> SElem a
TXT

instance ToSElem T.Text where
    toSElem :: forall b. Stringable b => Text -> SElem b
toSElem = forall a. Text -> SElem a
TXT forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
LT.fromStrict

instance ToSElem Bool where
    toSElem :: forall b. Stringable b => Bool -> SElem b
toSElem Bool
True = forall a. String -> SElem a
STR String
""
    toSElem Bool
_ = forall a. SElem a
SNull

instance (ToSElem a) => ToSElem (Maybe a) where
    toSElem :: forall b. Stringable b => Maybe a -> SElem b
toSElem (Just a
x) = forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem a
x
    toSElem Maybe a
_ = forall a. SElem a
SNull

instance (ToSElem a) => ToSElem (M.Map String a) where
    toSElem :: forall b. Stringable b => Map String a -> SElem b
toSElem = forall a. SMap a -> SElem a
SM forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem

instance (ToSElem a) => ToSElem [a] where
    toSElem :: forall b. Stringable b => [a] -> SElem b
toSElem = forall a b. (ToSElem a, Stringable b) => [a] -> SElem b
toSElemList

instance (ToSElem a, Ix i) => ToSElem (Array i a) where
   toSElem :: forall b. Stringable b => Array i a -> SElem b
toSElem = forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall i e. Array i e -> [e]
elems

instance (ToSElem a, F.Foldable t) => ToSElem (t a) where
    toSElem :: forall b. Stringable b => t a -> SElem b
toSElem = forall a b. (ToSElem a, Stringable b) => [a] -> SElem b
toSElemList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t a -> [a]
F.toList

--Numbers
instance StringTemplateShows Float where
    stringTemplateShow :: Float -> String
stringTemplateShow = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a. RealFloat a => a -> ShowS
showFloat String
""
    stringTemplateFormattedShow :: String -> Float -> String
stringTemplateFormattedShow = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a b c. (a -> b -> c) -> b -> a -> c
flip [] forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. RealFloat a => Maybe Int -> a -> ShowS
showGFloat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> Maybe a
listToMaybe forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Read a => ReadS a
reads
instance ToSElem Float where
    toSElem :: forall b. Stringable b => Float -> SElem b
toSElem = forall a b. (StringTemplateShows a, Stringable b) => a -> SElem b
stShowsToSE

instance StringTemplateShows Double where
    stringTemplateShow :: Double -> String
stringTemplateShow = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a. RealFloat a => a -> ShowS
showFloat String
""
    stringTemplateFormattedShow :: String -> Double -> String
stringTemplateFormattedShow = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a b c. (a -> b -> c) -> b -> a -> c
flip [] forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. RealFloat a => Maybe Int -> a -> ShowS
showGFloat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> Maybe a
listToMaybe forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Read a => ReadS a
reads
instance ToSElem Double where
    toSElem :: forall b. Stringable b => Double -> SElem b
toSElem = forall a b. (StringTemplateShows a, Stringable b) => a -> SElem b
stShowsToSE

instance ToSElem Int where
    toSElem :: forall b. Stringable b => Int -> SElem b
toSElem = forall a. String -> SElem a
STR forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show

instance ToSElem Integer where
    toSElem :: forall b. Stringable b => Integer -> SElem b
toSElem = forall a. String -> SElem a
STR forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show

instance (Integral a, Show a) => ToSElem (Ratio a) where
    toSElem :: forall b. Stringable b => Ratio a -> SElem b
toSElem = forall a. String -> SElem a
STR forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show

--Dates and Times

instance StringTemplateShows Day where
    stringTemplateShow :: Day -> String
stringTemplateShow = forall a. Show a => a -> String
show
    stringTemplateFormattedShow :: String -> Day -> String
stringTemplateFormattedShow = forall t. FormatTime t => TimeLocale -> String -> t -> String
formatTime TimeLocale
defaultTimeLocale
instance ToSElem Day where
    toSElem :: forall b. Stringable b => Day -> SElem b
toSElem = forall a b. (StringTemplateShows a, Stringable b) => a -> SElem b
stShowsToSE

instance StringTemplateShows LocalTime where
    stringTemplateShow :: LocalTime -> String
stringTemplateShow = forall a. Show a => a -> String
show
    stringTemplateFormattedShow :: String -> LocalTime -> String
stringTemplateFormattedShow = forall t. FormatTime t => TimeLocale -> String -> t -> String
formatTime TimeLocale
defaultTimeLocale
instance ToSElem LocalTime where
    toSElem :: forall b. Stringable b => LocalTime -> SElem b
toSElem = forall a b. (StringTemplateShows a, Stringable b) => a -> SElem b
stShowsToSE

instance StringTemplateShows TimeOfDay where
    stringTemplateShow :: TimeOfDay -> String
stringTemplateShow = forall a. Show a => a -> String
show
    stringTemplateFormattedShow :: String -> TimeOfDay -> String
stringTemplateFormattedShow = forall t. FormatTime t => TimeLocale -> String -> t -> String
formatTime TimeLocale
defaultTimeLocale
instance ToSElem TimeOfDay where
    toSElem :: forall b. Stringable b => TimeOfDay -> SElem b
toSElem = forall a b. (StringTemplateShows a, Stringable b) => a -> SElem b
stShowsToSE

instance StringTemplateShows UTCTime where
    stringTemplateShow :: UTCTime -> String
stringTemplateShow = forall a. Show a => a -> String
show
    stringTemplateFormattedShow :: String -> UTCTime -> String
stringTemplateFormattedShow = forall t. FormatTime t => TimeLocale -> String -> t -> String
formatTime TimeLocale
defaultTimeLocale
instance ToSElem UTCTime where
    toSElem :: forall b. Stringable b => UTCTime -> SElem b
toSElem = forall a b. (StringTemplateShows a, Stringable b) => a -> SElem b
stShowsToSE

instance StringTemplateShows TimeZone where
    stringTemplateShow :: TimeZone -> String
stringTemplateShow = forall a. Show a => a -> String
show
    stringTemplateFormattedShow :: String -> TimeZone -> String
stringTemplateFormattedShow = forall t. FormatTime t => TimeLocale -> String -> t -> String
formatTime TimeLocale
defaultTimeLocale
instance ToSElem TimeZone where
    toSElem :: forall b. Stringable b => TimeZone -> SElem b
toSElem = forall a b. (StringTemplateShows a, Stringable b) => a -> SElem b
stShowsToSE

instance StringTemplateShows ZonedTime where
    stringTemplateShow :: ZonedTime -> String
stringTemplateShow = forall a. Show a => a -> String
show
    stringTemplateFormattedShow :: String -> ZonedTime -> String
stringTemplateFormattedShow = forall t. FormatTime t => TimeLocale -> String -> t -> String
formatTime TimeLocale
defaultTimeLocale
instance ToSElem ZonedTime where
    toSElem :: forall b. Stringable b => ZonedTime -> SElem b
toSElem = forall a b. (StringTemplateShows a, Stringable b) => a -> SElem b
stShowsToSE

t2map :: [SElem a] -> SElem a
t2map :: forall a. [SElem a] -> SElem a
t2map = forall a. SMap a -> SElem a
SM forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k a. Ord k => [(k, a)] -> Map k a
M.fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. [a] -> [b] -> [(a, b)]
zip (forall a b. (a -> b) -> [a] -> [b]
map forall a. Show a => a -> String
show [(Int
0::Int)..])

instance (ToSElem a, ToSElem b) => ToSElem (a, b) where
   toSElem :: forall b. Stringable b => (a, b) -> SElem b
toSElem (a
a,b
b) = forall a. [SElem a] -> SElem a
t2map [forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem a
a, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem b
b]
instance (ToSElem a, ToSElem b, ToSElem c) => ToSElem (a, b, c) where
   toSElem :: forall b. Stringable b => (a, b, c) -> SElem b
toSElem (a
a,b
b,c
c) = forall a. [SElem a] -> SElem a
t2map [forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem a
a, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem b
b, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem c
c]
instance (ToSElem a, ToSElem b, ToSElem c, ToSElem d) => ToSElem (a, b, c, d) where
   toSElem :: forall b. Stringable b => (a, b, c, d) -> SElem b
toSElem (a
a,b
b,c
c,d
d) = forall a. [SElem a] -> SElem a
t2map [forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem a
a, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem b
b, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem c
c, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem d
d]
instance (ToSElem a, ToSElem b, ToSElem c, ToSElem d, ToSElem e) => ToSElem (a, b, c, d, e) where
   toSElem :: forall b. Stringable b => (a, b, c, d, e) -> SElem b
toSElem (a
a,b
b,c
c,d
d,e
e) = forall a. [SElem a] -> SElem a
t2map [forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem a
a, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem b
b, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem c
c, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem d
d, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem e
e]
instance (ToSElem a, ToSElem b, ToSElem c, ToSElem d, ToSElem e, ToSElem f) => ToSElem (a, b, c, d, e, f) where
   toSElem :: forall b. Stringable b => (a, b, c, d, e, f) -> SElem b
toSElem (a
a,b
b,c
c,d
d,e
e, f
f) = forall a. [SElem a] -> SElem a
t2map [forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem a
a, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem b
b, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem c
c, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem d
d, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem e
e, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem f
f]
instance (ToSElem a, ToSElem b, ToSElem c, ToSElem d, ToSElem e, ToSElem f, ToSElem g) => ToSElem (a, b, c, d, e, f, g) where
   toSElem :: forall b. Stringable b => (a, b, c, d, e, f, g) -> SElem b
toSElem (a
a,b
b,c
c,d
d,e
e,f
f,g
g) = forall a. [SElem a] -> SElem a
t2map [forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem a
a, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem b
b, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem c
c, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem d
d, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem e
e, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem f
f, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem g
g]
instance (ToSElem a, ToSElem b, ToSElem c, ToSElem d, ToSElem e, ToSElem f, ToSElem g, ToSElem h) => ToSElem (a, b, c, d, e, f, g, h) where
   toSElem :: forall b. Stringable b => (a, b, c, d, e, f, g, h) -> SElem b
toSElem (a
a,b
b,c
c,d
d,e
e,f
f,g
g,h
h) = forall a. [SElem a] -> SElem a
t2map [forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem a
a, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem b
b, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem c
c, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem d
d, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem e
e, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem f
f, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem g
g, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem h
h]
instance (ToSElem a, ToSElem b, ToSElem c, ToSElem d, ToSElem e, ToSElem f, ToSElem g, ToSElem h, ToSElem i) => ToSElem (a, b, c, d, e, f, g, h, i) where
   toSElem :: forall b. Stringable b => (a, b, c, d, e, f, g, h, i) -> SElem b
toSElem (a
a,b
b,c
c,d
d,e
e,f
f,g
g,h
h,i
i) = forall a. [SElem a] -> SElem a
t2map [forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem a
a, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem b
b, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem c
c, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem d
d, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem e
e, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem f
f, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem g
g, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem h
h, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem i
i]
instance (ToSElem a, ToSElem b, ToSElem c, ToSElem d, ToSElem e, ToSElem f, ToSElem g, ToSElem h, ToSElem i, ToSElem j) => ToSElem (a, b, c, d, e, f, g, h, i, j) where
   toSElem :: forall b. Stringable b => (a, b, c, d, e, f, g, h, i, j) -> SElem b
toSElem (a
a,b
b,c
c,d
d,e
e,f
f,g
g,h
h,i
i,j
j) = forall a. [SElem a] -> SElem a
t2map [forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem a
a, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem b
b, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem c
c, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem d
d, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem e
e, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem f
f, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem g
g, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem h
h, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem i
i, forall a b. (ToSElem a, Stringable b) => a -> SElem b
toSElem j
j]