Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions dhall/src/Dhall/Marshal/Decode.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ module Dhall.Marshal.Decode
, string
, lazyText
, strictText
-- ** Time
, timeOfDay
, day
, timeZone
-- ** Containers
, maybe
, pair
Expand Down Expand Up @@ -164,6 +168,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
Expand Down Expand Up @@ -893,6 +898,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"
Expand Down
22 changes: 22 additions & 0 deletions dhall/src/Dhall/Marshal/Encode.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down