{-|
Module      : Toml.Value
Description : Semantic TOML values
Copyright   : (c) Eric Mertens, 2023
License     : ISC
Maintainer  : emertens@gmail.com

This module provides the type for the semantics of a TOML file.
All dotted keys are resolved in this representation. Each table
is a Map with a single level of keys.

-}
module Toml.Value (
    Value(..),
    Table,
    ) where

import Data.Data (Data)
import Data.Map (Map)
import Data.Time (Day, LocalTime, TimeOfDay, ZonedTime(zonedTimeToLocalTime, zonedTimeZone), timeZoneMinutes)
import GHC.Generics (Generic)

-- | Representation of a TOML key-value table.
type Table = Map String Value

-- | Semantic TOML value with all table assignments resolved.
data Value
    = Integer   Integer
    | Float     Double
    | Array     [Value]
    | Table     Table
    | Bool      Bool
    | String    String
    | TimeOfDay TimeOfDay
    | ZonedTime ZonedTime
    | LocalTime LocalTime
    | Day       Day
    deriving (
        Show {- ^ Default instance -},
        Read {- ^ Default instance -},
        Data {- ^ Default instance -},
        Generic {- ^ Default instance -})

-- | Nearly default instance except 'ZonedTime' doesn't have an
-- 'Eq' instance. 'ZonedTime' values are equal if their times and
-- timezones are both equal.

instance Eq Value where
    Integer   Integer
x == :: Value -> Value -> Bool
== Integer   Integer
y = Integer
x Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
y
    Float     Double
x == Float     Double
y = Double
x Double -> Double -> Bool
forall a. Eq a => a -> a -> Bool
== Double
y
    Array     [Value]
x == Array     [Value]
y = [Value]
x [Value] -> [Value] -> Bool
forall a. Eq a => a -> a -> Bool
== [Value]
y
    Table     Table
x == Table     Table
y = Table
x Table -> Table -> Bool
forall a. Eq a => a -> a -> Bool
== Table
y
    Bool      Bool
x == Bool      Bool
y = Bool
x Bool -> Bool -> Bool
forall a. Eq a => a -> a -> Bool
== Bool
y
    String    String
x == String    String
y = String
x String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
y
    TimeOfDay TimeOfDay
x == TimeOfDay TimeOfDay
y = TimeOfDay
x TimeOfDay -> TimeOfDay -> Bool
forall a. Eq a => a -> a -> Bool
== TimeOfDay
y
    LocalTime LocalTime
x == LocalTime LocalTime
y = LocalTime
x LocalTime -> LocalTime -> Bool
forall a. Eq a => a -> a -> Bool
== LocalTime
y
    Day       Day
x == Day       Day
y = Day
x Day -> Day -> Bool
forall a. Eq a => a -> a -> Bool
== Day
y
    ZonedTime ZonedTime
x == ZonedTime ZonedTime
y = ZonedTime -> (LocalTime, Int)
projectZT ZonedTime
x (LocalTime, Int) -> (LocalTime, Int) -> Bool
forall a. Eq a => a -> a -> Bool
== ZonedTime -> (LocalTime, Int)
projectZT ZonedTime
y
    Value
_           == Value
_           = Bool
False

-- Extract the relevant parts to build an Eq instance
projectZT :: ZonedTime -> (LocalTime, Int)
projectZT :: ZonedTime -> (LocalTime, Int)
projectZT ZonedTime
x = (ZonedTime -> LocalTime
zonedTimeToLocalTime ZonedTime
x, TimeZone -> Int
timeZoneMinutes (ZonedTime -> TimeZone
zonedTimeZone ZonedTime
x))