Skip to content

Commit

Permalink
Move to using leading commas in pretty-printed CDDL
Browse files Browse the repository at this point in the history
  • Loading branch information
nc6 committed Sep 11, 2024
1 parent 79d6a78 commit 7c434d2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@

* `collectFrom` now adds a first definition in the generated Huddle referencing
the top level elements, to be compatible with the CDDL spec.

## 0.3.2.0 -- 2024-09-11

* Leading rather than trailing commas in the pretty printer.
2 changes: 1 addition & 1 deletion cuddle.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 3.4
name: cuddle
version: 0.3.1.0
version: 0.3.2.0
synopsis: CDDL Generator and test utilities

-- description:
Expand Down
30 changes: 20 additions & 10 deletions src/Codec/CBOR/Cuddle/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ instance Pretty Type1 where
instance Pretty Type2 where
pretty (T2Value v) = pretty v
pretty (T2Name n mg) = pretty n <> pretty mg
pretty (T2Group g) = align $ enclose "(" ")" $ pretty g
pretty (T2Map g) = align $ enclose "{" "}" $ pretty g
pretty (T2Array g) = brackets $ pretty g
pretty (T2Group g) = enclose "( " ")" . align $ pretty g
pretty (T2Map g) = prettyGroup AsMap g
pretty (T2Array g) = prettyGroup AsArray g
pretty (T2Unwrapped n mg) = "~" <+> pretty n <> pretty mg
pretty (T2Enum g) = "&" <+> enclose "(" ")" (pretty g)
pretty (T2Enum g) = "&" <+> prettyGroup AsGroup g
pretty (T2EnumRef g mg) = "&" <+> pretty g <> pretty mg
pretty (T2Tag minor t) = "#6" <> min' <> enclose "(" ")" (pretty t)
where
Expand All @@ -90,12 +90,22 @@ instance Pretty OccurrenceIndicator where
pretty OIOneOrMore = "+"
pretty (OIBounded ml mh) = pretty ml <> "*" <> pretty mh

instance Pretty Group where
pretty (Group (NE.toList -> xs)) =
align . vsep . punctuate " // " $ fmap prettyGrpChoice xs
where
prettyGrpChoice = sep . punctuate "," . fmap pretty
-- | Control how to render a group
data GroupRender =
AsMap
| AsArray
| AsGroup

prettyGroup :: GroupRender -> Group -> Doc ann
prettyGroup gr (Group (NE.toList -> xs)) =
align $ encloseSep lp rp " // " (fmap prettyGrpChoice xs)
where
prettyGrpChoice = encloseSep mempty mempty ", " . fmap pretty
(lp, rp) = case gr of
AsMap -> ("{", "}")
AsArray -> ("[", "]")
AsGroup -> ("(",")")

instance Pretty GroupEntry where
pretty (GEType moi mmk t) =
hsep $
Expand All @@ -106,7 +116,7 @@ instance Pretty GroupEntry where
<> pretty mga
pretty (GEGroup moi g) =
hsep $
catMaybes [fmap pretty moi, Just $ enclose "(" ")" (pretty g)]
catMaybes [fmap pretty moi, Just $ prettyGroup AsGroup g]

instance Pretty MemberKey where
pretty (MKType t1) = pretty t1 <+> "=>"
Expand Down

0 comments on commit 7c434d2

Please sign in to comment.