{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeOperators #-}
module Toml.Type
(
TOML (..)
, ValueType (..)
, Value (..)
, AnyValue (..)
, UValue (..)
, DateTime (..)
, matchBool
, matchInteger
, matchDouble
, matchText
, matchDate
, matchArray
, showType
, valueType
, typeCheck
) where
import Data.HashMap.Strict (HashMap)
import Data.Text (Text)
import Data.Time (Day, LocalTime, TimeOfDay, ZonedTime, zonedTimeToUTC)
import Data.Type.Equality ((:~:) (..))
import Toml.PrefixTree (Key (..), PrefixMap)
data TOML = TOML
{ tomlPairs :: HashMap Key AnyValue
, tomlTables :: PrefixMap TOML
} deriving (Show, Eq)
data ValueType = TBool | TInt | TFloat | TString | TDate | TArray
deriving (Eq, Show)
showType :: ValueType -> String
showType = drop 1 . show
data Value (t :: ValueType) where
Bool :: Bool -> Value 'TBool
Int :: Integer -> Value 'TInt
Float :: Double -> Value 'TFloat
String :: Text -> Value 'TString
Date :: DateTime -> Value 'TDate
Array :: [Value t] -> Value 'TArray
deriving instance Show (Value t)
instance Eq (Value t) where
(Bool b1) == (Bool b2) = b1 == b2
(Int i1) == (Int i2) = i1 == i2
(Float f1) == (Float f2) = f1 == f2
(String s1) == (String s2) = s1 == s2
(Date d1) == (Date d2) = d1 == d2
(Array a1) == (Array a2) = eqValueList a1 a2
eqValueList :: [Value a] -> [Value b] -> Bool
eqValueList [] [] = True
eqValueList (x:xs) (y:ys) = case sameValue x y of
Right Refl -> x == y && eqValueList xs ys
Left _ -> False
eqValueList _ _ = False
valueType :: Value t -> ValueType
valueType (Bool _) = TBool
valueType (Int _) = TInt
valueType (Float _) = TFloat
valueType (String _) = TString
valueType (Date _) = TDate
valueType (Array _) = TArray
matchBool :: Value f -> Maybe Bool
matchBool (Bool b) = Just b
matchBool _ = Nothing
matchInteger :: Value f -> Maybe Integer
matchInteger (Int n) = Just n
matchInteger _ = Nothing
matchDouble :: Value f -> Maybe Double
matchDouble (Float f) = Just f
matchDouble _ = Nothing
matchText :: Value f -> Maybe Text
matchText (String s) = Just s
matchText _ = Nothing
matchDate :: Value f -> Maybe DateTime
matchDate (Date d) = Just d
matchDate _ = Nothing
matchArray :: (forall t . Value t -> Maybe a) -> Value f -> Maybe [a]
matchArray matchElement (Array a) = mapM matchElement a
matchArray _ _ = Nothing
data UValue
= UBool !Bool
| UInt !Integer
| UFloat !Double
| UString !Text
| UDate !DateTime
| UArray ![UValue]
data AnyValue = forall (t :: ValueType) . AnyValue (Value t)
instance Show AnyValue where
show (AnyValue v) = show v
instance Eq AnyValue where
(AnyValue (Bool b1)) == (AnyValue (Bool b2)) = b1 == b2
(AnyValue (Int i1)) == (AnyValue (Int i2)) = i1 == i2
(AnyValue (Float f1)) == (AnyValue (Float f2)) = f1 == f2
(AnyValue (String s1)) == (AnyValue (String s2)) = s1 == s2
(AnyValue (Date d1)) == (AnyValue (Date d2)) = d1 == d2
(AnyValue (Array a1)) == (AnyValue (Array a2)) = eqValueList a1 a2
_ == _ = False
data DateTime
= Zoned !ZonedTime
| Local !LocalTime
| Day !Day
| Hours !TimeOfDay
deriving (Show)
instance Eq DateTime where
(Zoned a) == (Zoned b) = zonedTimeToUTC a == zonedTimeToUTC b
(Local a) == (Local b) = a == b
(Day a) == (Day b) = a == b
(Hours a) == (Hours b) = a == b
_ == _ = False
data TypeMismatchError = TypeMismatchError
{ typeExpected :: ValueType
, typeActual :: ValueType
} deriving (Eq)
instance Show TypeMismatchError where
show TypeMismatchError{..} = "Expected type '" ++ showType typeExpected
++ "' but actual type: '" ++ showType typeActual ++ "'"
typeCheck :: UValue -> Either TypeMismatchError AnyValue
typeCheck (UBool b) = rightAny $ Bool b
typeCheck (UInt n) = rightAny $ Int n
typeCheck (UFloat f) = rightAny $ Float f
typeCheck (UString s) = rightAny $ String s
typeCheck (UDate d) = rightAny $ Date d
typeCheck (UArray a) = case a of
[] -> rightAny $ Array []
x:xs -> do
AnyValue v <- typeCheck x
AnyValue . Array <$> checkElem v xs
where
checkElem :: Value t -> [UValue] -> Either TypeMismatchError [Value t]
checkElem v [] = Right [v]
checkElem v (x:xs) = do
AnyValue vx <- typeCheck x
Refl <- sameValue v vx
(v :) <$> checkElem vx xs
rightAny :: Value t -> Either l AnyValue
rightAny = Right . AnyValue
sameValue :: Value a -> Value b -> Either TypeMismatchError (a :~: b)
sameValue Bool{} Bool{} = Right Refl
sameValue Int{} Int{} = Right Refl
sameValue Float{} Float{} = Right Refl
sameValue String{} String{} = Right Refl
sameValue Date{} Date{} = Right Refl
sameValue Array{} Array{} = Right Refl
sameValue l r = Left $ TypeMismatchError
{ typeExpected = valueType l
, typeActual = valueType r
}