Skip to content

Commit 9b15cf7

Browse files
committed
Support sorting CDDL
This is useful to compare generated CDDL files.
1 parent 90f5739 commit 9b15cf7

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

bin/Main.hs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ import System.Exit (exitFailure, exitSuccess)
2222
import System.IO (hPutStrLn, stderr)
2323
import System.Random (getStdGen)
2424
import Text.Megaparsec (ParseErrorBundle, Parsec, errorBundlePretty, runParser)
25+
import Codec.CBOR.Cuddle.CDDL (sortCDDL)
2526

2627
data Opts = Opts Command String
2728

2829
data Command
29-
= Format
30+
= Format FormatOpts
3031
| Validate
3132
| GenerateCBOR GenOpts
3233

@@ -67,14 +68,25 @@ pGenOpts =
6768
<> value AsCBOR
6869
)
6970

71+
newtype FormatOpts = FormatOpts
72+
{sort :: Bool}
73+
74+
pFormatOpts :: Parser FormatOpts
75+
pFormatOpts =
76+
FormatOpts
77+
<$> switch
78+
( long "sort-rules"
79+
<> help "Sort the CDDL rule definitions before printing."
80+
)
81+
7082
opts :: Parser Opts
7183
opts =
7284
Opts
7385
<$> subparser
7486
( command
7587
"format"
7688
( info
77-
(pure Format)
89+
( Format <$> pFormatOpts)
7890
( progDesc "Format the provided CDDL file"
7991
)
8092
)
@@ -114,7 +126,8 @@ run (Opts cmd cddlFile) = do
114126
putStrLnErr $ errorBundlePretty err
115127
exitFailure
116128
Right res -> case cmd of
117-
Format -> putDocW 80 $ pretty res
129+
Format fOpts -> let defs = if sort fOpts then sortCDDL res else res in
130+
putDocW 80 $ pretty defs
118131
Validate -> case fullResolveCDDL res of
119132
Left err -> putStrLnErr (show err) >> exitFailure
120133
Right _ -> exitSuccess

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.2.0.1
3+
version: 0.2.1.0
44
synopsis: CDDL Generator and test utilities
55

66
-- description:

src/Codec/CBOR/Cuddle/CDDL.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,16 @@ import GHC.Generics (Generic)
1313
newtype CDDL = CDDL (NE.NonEmpty (WithComments Rule))
1414
deriving (Eq, Generic, Show)
1515

16+
-- | Sort the CDDL Rules on the basis of their names
17+
sortCDDL :: CDDL -> CDDL
18+
sortCDDL (CDDL xs) = CDDL $ NE.sort xs
19+
1620
data WithComments a = WithComments a (Maybe Comment)
1721
deriving (Eq, Show, Generic)
1822

23+
instance Ord a => Ord (WithComments a) where
24+
compare (WithComments a1 _) (WithComments a2 _) = compare a1 a2
25+
1926
stripComment :: WithComments a -> a
2027
stripComment (WithComments a _) = a
2128

@@ -114,6 +121,9 @@ newtype GenericArg = GenericArg (NE.NonEmpty Type1)
114121
data Rule = Rule Name (Maybe GenericParam) Assign TypeOrGroup
115122
deriving (Eq, Generic, Show)
116123

124+
instance Ord Rule where
125+
compare (Rule n1 _ _ _) (Rule n2 _ _ _) = compare n1 n2
126+
117127
-- |
118128
-- A range operator can be used to join two type expressions that stand
119129
-- for either two integer values or two floating-point values; it

0 commit comments

Comments
 (0)