diff --git a/dhall/src/Dhall/Marshal/Decode.hs b/dhall/src/Dhall/Marshal/Decode.hs index b5149c2c4..5b533585b 100644 --- a/dhall/src/Dhall/Marshal/Decode.hs +++ b/dhall/src/Dhall/Marshal/Decode.hs @@ -56,6 +56,10 @@ module Dhall.Marshal.Decode , string , lazyText , strictText + -- ** Time + , timeOfDay + , day + , timeZone -- ** Containers , maybe , pair @@ -168,6 +172,7 @@ import qualified Data.Sequence import qualified Data.Set import qualified Data.Text import qualified Data.Text.Lazy +import qualified Data.Time as Time import qualified Data.Vector import qualified Dhall.Core as Core import qualified Dhall.Map @@ -312,6 +317,15 @@ instance FromDhall a => FromDhall [a] where instance FromDhall a => FromDhall (Vector a) where autoWith opts = vector (autoWith opts) +instance FromDhall Time.TimeOfDay where + autoWith _ = timeOfDay + +instance FromDhall Time.Day where + autoWith _ = day + +instance FromDhall Time.TimeZone where + autoWith _ = timeZone + {-| Note that this instance will throw errors in the presence of duplicates in the list. To ignore duplicates, use `setIgnoringDuplicates`. -} @@ -897,6 +911,45 @@ strictText = Decoder {..} expected = pure Text +{-| Decode `Time.TimeOfDay` + +>>> input timeOfDay "00:00:00" +00:00:00 +-} +timeOfDay :: Decoder Time.TimeOfDay +timeOfDay = Decoder {..} + where + extract (TimeLiteral t _) = pure t + extract expr = typeError expected expr + + expected = pure Time + +{-| Decode `Time.Day` + +>>> input day "2000-01-01" +2000-01-01 +-} +day :: Decoder Time.Day +day = Decoder {..} + where + extract (DateLiteral d) = pure d + extract expr = typeError expected expr + + expected = pure Date + +{-| Decode `Time.TimeZone` + +>>> input timeZone "+00:00" ++0000 +-} +timeZone :: Decoder Time.TimeZone +timeZone = Decoder {..} + where + extract (TimeZoneLiteral z) = pure z + extract expr = typeError expected expr + + expected = pure TimeZone + {-| Decode a `Maybe`. >>> input (maybe natural) "Some 1" diff --git a/dhall/src/Dhall/Marshal/Encode.hs b/dhall/src/Dhall/Marshal/Encode.hs index 4cca05f6b..545ae6f82 100644 --- a/dhall/src/Dhall/Marshal/Encode.hs +++ b/dhall/src/Dhall/Marshal/Encode.hs @@ -82,6 +82,7 @@ import qualified Data.Sequence import qualified Data.Set import qualified Data.Text import qualified Data.Text.Lazy +import qualified Data.Time as Time import qualified Data.Vector import qualified Data.Void import qualified Dhall.Core as Core @@ -311,6 +312,27 @@ instance ToDhall a => ToDhall [a] where instance ToDhall a => ToDhall (Vector a) where injectWith = fmap (contramap Data.Vector.toList) injectWith +instance ToDhall Time.TimeOfDay where + injectWith _ = Encoder {..} + where + embed timeOfDay = TimeLiteral timeOfDay 12 + + declared = Time + +instance ToDhall Time.Day where + injectWith _ = Encoder {..} + where + embed = DateLiteral + + declared = Date + +instance ToDhall Time.TimeZone where + injectWith _ = Encoder {..} + where + embed = TimeZoneLiteral + + declared = TimeZone + {-| Note that the output list will be sorted. >>> let x = Data.Set.fromList ["mom", "hi" :: Text]