3
3
module Main (main ) where
4
4
5
5
import Codec.CBOR.Cuddle.CBOR.Gen (generateCBORTerm )
6
+ import Codec.CBOR.Cuddle.CBOR.Validator
6
7
import Codec.CBOR.Cuddle.CDDL (Name (.. ), sortCDDL )
7
8
import Codec.CBOR.Cuddle.CDDL.Prelude (prependPrelude )
8
9
import Codec.CBOR.Cuddle.CDDL.Resolve (
@@ -28,12 +29,21 @@ import Text.Megaparsec (ParseErrorBundle, Parsec, errorBundlePretty, runParser)
28
29
29
30
data Opts = Opts Command String
30
31
31
- newtype ValidateOpts = ValidateOpts { vNoPrelude :: Bool }
32
-
33
32
data Command
34
33
= Format FormatOpts
35
34
| Validate ValidateOpts
36
35
| GenerateCBOR GenOpts
36
+ | ValidateCBOR ValidateCBOROpts
37
+
38
+ newtype ValidateOpts = ValidateOpts { vNoPrelude :: Bool }
39
+
40
+ pValidateOpts :: Parser ValidateOpts
41
+ pValidateOpts =
42
+ ValidateOpts
43
+ <$> switch
44
+ ( long " no-prelude"
45
+ <> help " Do not include the CDDL prelude."
46
+ )
37
47
38
48
-- | Various formats for outputtting CBOR
39
49
data CBOROutputFormat
@@ -53,6 +63,7 @@ pCBOROutputFormat = eitherReader $ \case
53
63
data GenOpts = GenOpts
54
64
{ itemName :: T. Text
55
65
, outputFormat :: CBOROutputFormat
66
+ , outputTo :: Maybe String
56
67
, gNoPrelude :: Bool
57
68
}
58
69
@@ -72,6 +83,13 @@ pGenOpts =
72
83
<> help " Output format"
73
84
<> value AsCBOR
74
85
)
86
+ <*> optional
87
+ ( strOption
88
+ ( long " out-file"
89
+ <> short ' o'
90
+ <> help " Write to"
91
+ )
92
+ )
75
93
<*> switch
76
94
( long " no-prelude"
77
95
<> help " Do not include the CDDL prelude."
@@ -88,10 +106,27 @@ pFormatOpts =
88
106
<> help " Sort the CDDL rule definitions before printing."
89
107
)
90
108
91
- pValidateOpts :: Parser ValidateOpts
92
- pValidateOpts =
93
- ValidateOpts
94
- <$> switch
109
+ data ValidateCBOROpts = ValidateCBOROpts
110
+ { vcItemName :: T. Text
111
+ , vcInput :: FilePath
112
+ , vcNoPrelude :: Bool
113
+ }
114
+
115
+ pValidateCBOROpts :: Parser ValidateCBOROpts
116
+ pValidateCBOROpts =
117
+ ValidateCBOROpts
118
+ <$> strOption
119
+ ( long " rule"
120
+ <> short ' r'
121
+ <> metavar " RULE"
122
+ <> help " Name of the CDDL rule to validate this file with"
123
+ )
124
+ <*> strOption
125
+ ( long " cbor"
126
+ <> short ' c'
127
+ <> help " CBOR file"
128
+ )
129
+ <*> switch
95
130
( long " no-prelude"
96
131
<> help " Do not include the CDDL prelude."
97
132
)
@@ -118,6 +153,12 @@ opts =
118
153
(GenerateCBOR <$> pGenOpts <**> helper)
119
154
(progDesc " Generate a CBOR term matching the schema" )
120
155
)
156
+ <> command
157
+ " validate-cbor"
158
+ ( info
159
+ (ValidateCBOR <$> pValidateCBOROpts <**> helper)
160
+ (progDesc " Validate a CBOR file against a schema" )
161
+ )
121
162
)
122
163
<*> argument str (metavar " CDDL_FILE" )
123
164
@@ -171,8 +212,21 @@ run (Opts cmd cddlFile) = do
171
212
in case outputFormat gOpts of
172
213
AsTerm -> print term
173
214
AsFlatTerm -> print $ toFlatTerm (encodeTerm term)
174
- AsCBOR -> BSC. putStrLn . Base16. encode . toStrictByteString $ encodeTerm term
215
+ AsCBOR -> case outputTo gOpts of
216
+ Nothing -> BSC. putStrLn . Base16. encode . toStrictByteString $ encodeTerm term
217
+ Just out -> BSC. writeFile out $ toStrictByteString $ encodeTerm term
175
218
AsPrettyCBOR -> putStrLn . prettyHexEnc $ encodeTerm term
219
+ ValidateCBOR vcOpts ->
220
+ let
221
+ res'
222
+ | vcNoPrelude vcOpts = res
223
+ | otherwise = prependPrelude res
224
+ in
225
+ case fullResolveCDDL res' of
226
+ Left err -> putStrLnErr (show err) >> exitFailure
227
+ Right mt -> do
228
+ cbor <- BSC. readFile (vcInput vcOpts)
229
+ validateCBOR cbor (Name (vcItemName vcOpts) mempty ) mt
176
230
177
231
putStrLnErr :: String -> IO ()
178
232
putStrLnErr = hPutStrLn stderr
0 commit comments