File tree Expand file tree Collapse file tree 3 files changed +27
-4
lines changed Expand file tree Collapse file tree 3 files changed +27
-4
lines changed Original file line number Diff line number Diff line change @@ -22,11 +22,12 @@ import System.Exit (exitFailure, exitSuccess)
22
22
import System.IO (hPutStrLn , stderr )
23
23
import System.Random (getStdGen )
24
24
import Text.Megaparsec (ParseErrorBundle , Parsec , errorBundlePretty , runParser )
25
+ import Codec.CBOR.Cuddle.CDDL (sortCDDL )
25
26
26
27
data Opts = Opts Command String
27
28
28
29
data Command
29
- = Format
30
+ = Format FormatOpts
30
31
| Validate
31
32
| GenerateCBOR GenOpts
32
33
@@ -67,14 +68,25 @@ pGenOpts =
67
68
<> value AsCBOR
68
69
)
69
70
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
+
70
82
opts :: Parser Opts
71
83
opts =
72
84
Opts
73
85
<$> subparser
74
86
( command
75
87
" format"
76
88
( info
77
- (pure Format )
89
+ ( Format <$> pFormatOpts )
78
90
( progDesc " Format the provided CDDL file"
79
91
)
80
92
)
@@ -114,7 +126,8 @@ run (Opts cmd cddlFile) = do
114
126
putStrLnErr $ errorBundlePretty err
115
127
exitFailure
116
128
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
118
131
Validate -> case fullResolveCDDL res of
119
132
Left err -> putStrLnErr (show err) >> exitFailure
120
133
Right _ -> exitSuccess
Original file line number Diff line number Diff line change 1
1
cabal-version : 3.4
2
2
name : cuddle
3
- version : 0.2.0.1
3
+ version : 0.2.1.0
4
4
synopsis : CDDL Generator and test utilities
5
5
6
6
-- description:
Original file line number Diff line number Diff line change @@ -13,9 +13,16 @@ import GHC.Generics (Generic)
13
13
newtype CDDL = CDDL (NE. NonEmpty (WithComments Rule ))
14
14
deriving (Eq , Generic , Show )
15
15
16
+ -- | Sort the CDDL Rules on the basis of their names
17
+ sortCDDL :: CDDL -> CDDL
18
+ sortCDDL (CDDL xs) = CDDL $ NE. sort xs
19
+
16
20
data WithComments a = WithComments a (Maybe Comment )
17
21
deriving (Eq , Show , Generic )
18
22
23
+ instance Ord a => Ord (WithComments a ) where
24
+ compare (WithComments a1 _) (WithComments a2 _) = compare a1 a2
25
+
19
26
stripComment :: WithComments a -> a
20
27
stripComment (WithComments a _) = a
21
28
@@ -114,6 +121,9 @@ newtype GenericArg = GenericArg (NE.NonEmpty Type1)
114
121
data Rule = Rule Name (Maybe GenericParam ) Assign TypeOrGroup
115
122
deriving (Eq , Generic , Show )
116
123
124
+ instance Ord Rule where
125
+ compare (Rule n1 _ _ _) (Rule n2 _ _ _) = compare n1 n2
126
+
117
127
-- |
118
128
-- A range operator can be used to join two type expressions that stand
119
129
-- for either two integer values or two floating-point values; it
You can’t perform that action at this time.
0 commit comments