Skip to content

Commit 7c434d2

Browse files
committed
Move to using leading commas in pretty-printed CDDL
1 parent 79d6a78 commit 7c434d2

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@
1818

1919
* `collectFrom` now adds a first definition in the generated Huddle referencing
2020
the top level elements, to be compatible with the CDDL spec.
21+
22+
## 0.3.2.0 -- 2024-09-11
23+
24+
* Leading rather than trailing commas in the pretty printer.

cuddle.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cabal-version: 3.4
22
name: cuddle
3-
version: 0.3.1.0
3+
version: 0.3.2.0
44
synopsis: CDDL Generator and test utilities
55

66
-- description:

src/Codec/CBOR/Cuddle/Pretty.hs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ instance Pretty Type1 where
6767
instance Pretty Type2 where
6868
pretty (T2Value v) = pretty v
6969
pretty (T2Name n mg) = pretty n <> pretty mg
70-
pretty (T2Group g) = align $ enclose "(" ")" $ pretty g
71-
pretty (T2Map g) = align $ enclose "{" "}" $ pretty g
72-
pretty (T2Array g) = brackets $ pretty g
70+
pretty (T2Group g) = enclose "( " ")" . align $ pretty g
71+
pretty (T2Map g) = prettyGroup AsMap g
72+
pretty (T2Array g) = prettyGroup AsArray g
7373
pretty (T2Unwrapped n mg) = "~" <+> pretty n <> pretty mg
74-
pretty (T2Enum g) = "&" <+> enclose "(" ")" (pretty g)
74+
pretty (T2Enum g) = "&" <+> prettyGroup AsGroup g
7575
pretty (T2EnumRef g mg) = "&" <+> pretty g <> pretty mg
7676
pretty (T2Tag minor t) = "#6" <> min' <> enclose "(" ")" (pretty t)
7777
where
@@ -90,12 +90,22 @@ instance Pretty OccurrenceIndicator where
9090
pretty OIOneOrMore = "+"
9191
pretty (OIBounded ml mh) = pretty ml <> "*" <> pretty mh
9292

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

99+
prettyGroup :: GroupRender -> Group -> Doc ann
100+
prettyGroup gr (Group (NE.toList -> xs)) =
101+
align $ encloseSep lp rp " // " (fmap prettyGrpChoice xs)
102+
where
103+
prettyGrpChoice = encloseSep mempty mempty ", " . fmap pretty
104+
(lp, rp) = case gr of
105+
AsMap -> ("{", "}")
106+
AsArray -> ("[", "]")
107+
AsGroup -> ("(",")")
108+
99109
instance Pretty GroupEntry where
100110
pretty (GEType moi mmk t) =
101111
hsep $
@@ -106,7 +116,7 @@ instance Pretty GroupEntry where
106116
<> pretty mga
107117
pretty (GEGroup moi g) =
108118
hsep $
109-
catMaybes [fmap pretty moi, Just $ enclose "(" ")" (pretty g)]
119+
catMaybes [fmap pretty moi, Just $ prettyGroup AsGroup g]
110120

111121
instance Pretty MemberKey where
112122
pretty (MKType t1) = pretty t1 <+> "=>"

0 commit comments

Comments
 (0)