{-# LANGUAGE CPP #-}
module Text.Yate.Types
( Path(..)
, Template(..)
, YateValue(..)
, ToYate(..)
) where
#ifndef NoAeson
import Data.Scientific (toRealFloat)
import qualified Data.Aeson as A
#endif
import qualified Data.HashMap.Strict as M
import qualified Data.Text as T
import qualified Data.Text.Lazy as TL
import qualified Data.Vector as V
data Path
= AbsolutePath [T.Text]
| RelativePath [T.Text]
deriving (Eq)
instance Show Path where
show path = case path of
AbsolutePath names -> T.unpack (T.intercalate "." names)
RelativePath names -> '.' : T.unpack (T.intercalate "." names)
data Template a
= Content TL.Text
| Variable Path
| If Path (Template a) (Template a)
| For T.Text Path (Template a)
| In Path (Template a)
| Parts [Template a]
deriving (Show, Eq)
data YateValue
= String T.Text
| Number Double
| Object (M.HashMap T.Text YateValue)
| List (V.Vector YateValue)
| Bool Bool
| Null
deriving (Show, Eq)
class ToYate a where
toYate :: a -> YateValue
instance ToYate YateValue where
toYate = id
instance ToYate () where
toYate () = Object $ M.empty
instance ToYate a => ToYate (V.Vector a) where
toYate = List . fmap toYate
instance ToYate a => ToYate [a] where
toYate = toYate . V.fromList
instance ToYate T.Text where
toYate = String
instance ToYate Double where
toYate = Number
instance ToYate Bool where
toYate = Bool
instance ToYate a => ToYate (Maybe a) where
toYate = maybe Null toYate
#ifndef NoAeson
instance ToYate A.Value where
toYate x = case x of
A.Object obj -> Object $ fmap toYate obj
A.Array arr -> List $ fmap toYate arr
A.String str -> String str
A.Number num -> Number $ toRealFloat num
A.Bool b -> Bool b
A.Null -> Null
#endif