1- {-# LANGUAGE ApplicativeDo #-}
2- {-# LANGUAGE PatternSynonyms #-}
3- {-# LANGUAGE RecordWildCards #-}
1+ {-# LANGUAGE ApplicativeDo #-}
2+ {-# LANGUAGE PatternSynonyms #-}
3+ {-# LANGUAGE RecordWildCards #-}
4+ {-# LANGUAGE OverloadedLists #-}
5+ {-# LANGUAGE OverloadedStrings #-}
6+ {-# LANGUAGE ViewPatterns #-}
47
58{-| This module exports the `dhallToToml` function for translating a
69 Dhall syntax tree to a TOML syntax tree (`TOML`) for the @tomland@
8184> [r.nested]
8285> c = 3
8386
87+ … and @Prelude.Map.Type@ also translates to a TOML table:
88+
89+ > $ dhall-to-toml <<< '[ { mapKey = "foo", mapValue = 1 } ]'
90+ > foo = 1
91+
8492 Dhall unions translate to the wrapped value, or a string if the alternative is empty:
8593
8694> $ dhall-to-toml <<< '{ u = < A | B >.A }'
@@ -248,9 +256,21 @@ pattern UnionApp x <- Core.App (Core.Field (Core.Union _) _) x
248256assertRecordLit
249257 :: Expr Void Void
250258 -> Either CompileError (Map Text (Core. RecordField Void Void ))
251- assertRecordLit (Core. RecordLit r) = Right r
252- assertRecordLit (UnionApp x) = assertRecordLit x
253- assertRecordLit e = Left $ NotARecord e
259+ assertRecordLit (Core. RecordLit r) =
260+ Right r
261+ assertRecordLit (UnionApp x) =
262+ assertRecordLit x
263+ assertRecordLit (Core. ListLit _ expressions)
264+ | Just keyValues <- traverse toKeyValue (toList expressions) =
265+ Right (Map. fromList keyValues)
266+ where
267+ toKeyValue
268+ (Core. RecordLit [ (" mapKey" , Core. recordFieldValue -> Core. TextLit (Core. Chunks [] key)), (" mapValue" , value) ]) =
269+ Just (key, value)
270+ toKeyValue _ =
271+ Nothing
272+ assertRecordLit e =
273+ Left (NotARecord e)
254274
255275toTomlTable :: Map Text (Core. RecordField Void Void ) -> Either CompileError TOML
256276toTomlTable r = foldM (toTomlRecordFold [] ) (mempty :: TOML ) (Map. toList r)
@@ -292,24 +312,6 @@ toToml toml pieces expr = case expr of
292312 Core. App Core. None _ ->
293313 return toml
294314
295- Core. ListLit _ a -> case toList a of
296- -- TODO: unions need to be handled here as well, it's a bit tricky
297- -- because they also have to be probed for being a "simple"
298- -- array of table
299- union@ (UnionApp (Core. RecordLit _)) : unions -> do
300- insertTables (union :| unions)
301-
302- record@ (Core. RecordLit _) : records -> do
303- insertTables (record :| records)
304-
305- -- inline array
306- expressions -> do
307- anyValues <- mapM toAnyValue expressions
308-
309- case AnyValue. toMArray anyValues of
310- Left _ -> Left (HeterogeneousArray expr)
311- Right array -> insertPrim array
312-
313315 Core. RecordLit r -> do
314316 let (inline, nested) =
315317 Map. partition (isInline . Core. recordFieldValue) r
@@ -331,6 +333,28 @@ toToml toml pieces expr = case expr of
331333 else do
332334 newPairs <- foldM (toTomlRecordFold [] ) mempty pairs
333335 return (TOML. insertTable key newPairs toml)
336+
337+ _ | Right keyValues <- assertRecordLit expr ->
338+ toToml toml pieces (Core. RecordLit keyValues)
339+
340+ Core. ListLit _ a -> case toList a of
341+ -- TODO: unions need to be handled here as well, it's a bit tricky
342+ -- because they also have to be probed for being a "simple"
343+ -- array of table
344+ union@ (UnionApp (Core. RecordLit _)) : unions -> do
345+ insertTables (union :| unions)
346+
347+ record@ (Core. RecordLit _) : records -> do
348+ insertTables (record :| records)
349+
350+ -- inline array
351+ expressions -> do
352+ anyValues <- mapM toAnyValue expressions
353+
354+ case AnyValue. toMArray anyValues of
355+ Left _ -> Left (HeterogeneousArray expr)
356+ Right array -> insertPrim array
357+
334358 _ ->
335359 Left (Unsupported expr)
336360 where
0 commit comments