@@ -24,6 +24,7 @@ import Data.Functor ((<&>))
24
24
import Data.Functor.Identity
25
25
import Data.IntSet qualified as IS
26
26
import Data.List.NonEmpty qualified as NE
27
+ import Data.Map.Strict (Map )
27
28
import Data.Map.Strict qualified as Map
28
29
import Data.Maybe
29
30
import Data.Text qualified as T
@@ -34,9 +35,9 @@ import System.Exit
34
35
import System.IO
35
36
import Text.Regex.TDFA
36
37
37
- type CDDL = CTreeRoot' Identity MonoRef
38
- type Rule = Node MonoRef
39
- type ResolvedRule = CTree MonoRef
38
+ type CDDL = Map Name Rule
39
+ type Rule = Node MonoReferenced
40
+ type ResolvedRule = CTree MonoReferenced
40
41
41
42
data CBORTermResult = CBORTermResult Term CDDLResult
42
43
deriving (Show )
@@ -130,13 +131,13 @@ validateCBOR bs rule cddl =
130
131
131
132
validateCBOR' ::
132
133
BS. ByteString -> Name -> CDDL -> CBORTermResult
133
- validateCBOR' bs rule cddl@ ( CTreeRoot tree) =
134
+ validateCBOR' bs rule cddl =
134
135
case deserialiseFromBytes decodeTerm (BSL. fromStrict bs) of
135
136
Left e -> error $ show e
136
137
Right (rest, term) ->
137
138
if BSL. null rest
138
- then runReader (validateTerm term (runIdentity $ tree Map. ! rule)) cddl
139
- else runReader (validateTerm (TBytes bs) (runIdentity $ tree Map. ! rule)) cddl
139
+ then runReader (validateTerm term (cddl Map. ! rule)) cddl
140
+ else runReader (validateTerm (TBytes bs) (cddl Map. ! rule)) cddl
140
141
141
142
--------------------------------------------------------------------------------
142
143
-- Terms
@@ -218,12 +219,12 @@ validateInteger i rule =
218
219
-- a = <big number>
219
220
Literal (Value (VBignum i') _) -> pure $ check $ i == i'
220
221
-- a = foo .ctrl bar
221
- Control op tgt ctrl -> ctrlDispatch (validateInteger i) op tgt ctrl (controlInteger i)
222
+ Control op tgt ctrl -> ctrlDispatch (validateInteger i) op ( MIt tgt) ( MIt ctrl) (controlInteger i)
222
223
-- a = foo / bar
223
- Choice opts -> validateChoice (validateInteger i) opts
224
+ Choice opts -> validateChoice (validateInteger i) ( MIt <$> opts)
224
225
-- a = x..y
225
226
Range low high bound ->
226
- ((,) <$> getRule low <*> getRule high)
227
+ ((,) <$> getRule ( MIt low) <*> getRule ( MIt high) )
227
228
<&> check . \ case
228
229
(Literal (Value (VUInt (fromIntegral -> n)) _), Literal (Value (VUInt (fromIntegral -> m)) _)) -> n <= i && range bound i m
229
230
(Literal (Value (VNInt (fromIntegral -> n)) _), Literal (Value (VUInt (fromIntegral -> m)) _)) -> - n <= i && range bound i m
@@ -235,14 +236,14 @@ validateInteger i rule =
235
236
(Literal (Value (VNInt (fromIntegral -> n)) _), Literal (Value (VBignum m) _)) -> (- n) <= i && range bound i m
236
237
-- a = &(x, y, z)
237
238
Enum g ->
238
- getRule g >>= \ case
239
+ getRule ( MIt g) >>= \ case
239
240
Group g' -> validateInteger i (MIt (Choice (NE. fromList g'))) <&> replaceRule
240
241
-- a = x: y
241
242
-- Note KV cannot appear on its own, but we will use this when validating
242
243
-- lists.
243
- KV _ v _ -> validateInteger i v <&> replaceRule
244
- Tag 2 (MIt ( Postlude PTBytes ) ) -> pure Valid
245
- Tag 3 (MIt ( Postlude PTBytes ) ) -> pure Valid
244
+ KV _ v _ -> validateInteger i ( MIt v) <&> replaceRule
245
+ Tag 2 (Postlude PTBytes ) -> pure Valid
246
+ Tag 3 (Postlude PTBytes ) -> pure Valid
246
247
_ -> pure UnapplicableRule
247
248
248
249
-- | Controls for an Integer
@@ -256,9 +257,9 @@ controlInteger i Bits ctrl = do
256
257
indices <-
257
258
getRule ctrl >>= \ case
258
259
Literal (Value (VUInt i') _) -> pure [i']
259
- Choice nodes -> getIndicesOfChoice nodes
260
- Range ff tt incl -> getIndicesOfRange ff tt incl
261
- Enum g -> getIndicesOfEnum g
260
+ Choice nodes -> getIndicesOfChoice ( MIt <$> nodes)
261
+ Range ff tt incl -> getIndicesOfRange ( MIt ff) ( MIt tt) incl
262
+ Enum g -> getIndicesOfEnum $ MIt g
262
263
pure $ boolCtrl $ go (IS. fromList (map fromIntegral indices)) i 0
263
264
where
264
265
go _ 0 _ = True
@@ -325,12 +326,12 @@ validateHalf f rule =
325
326
-- a = 0.5
326
327
Literal (Value (VFloat16 f') _) -> pure $ check $ f == f'
327
328
-- a = foo / bar
328
- Choice opts -> validateChoice (validateHalf f) opts
329
+ Choice opts -> validateChoice (validateHalf f) ( MIt <$> opts)
329
330
-- a = foo .ctrl bar
330
- Control op tgt ctrl -> ctrlDispatch (validateHalf f) op tgt ctrl (controlHalf f)
331
+ Control op tgt ctrl -> ctrlDispatch (validateHalf f) op ( MIt tgt) ( MIt ctrl) (controlHalf f)
331
332
-- a = x..y
332
333
Range low high bound ->
333
- ((,) <$> getRule low <*> getRule high)
334
+ ((,) <$> getRule ( MIt low) <*> getRule ( MIt high) )
334
335
<&> check . \ case
335
336
(Literal (Value (VFloat16 n) _), Literal (Value (VFloat16 m) _)) -> n <= f && range bound f m
336
337
_ -> pure UnapplicableRule
@@ -363,13 +364,13 @@ validateFloat f rule =
363
364
-- TODO: it is unclear if smaller floats should also validate
364
365
Literal (Value (VFloat32 f') _) -> pure $ check $ f == f'
365
366
-- a = foo / bar
366
- Choice opts -> validateChoice (validateFloat f) opts
367
+ Choice opts -> validateChoice (validateFloat f) ( MIt <$> opts)
367
368
-- a = foo .ctrl bar
368
- Control op tgt ctrl -> ctrlDispatch (validateFloat f) op tgt ctrl (controlFloat f)
369
+ Control op tgt ctrl -> ctrlDispatch (validateFloat f) op ( MIt tgt) ( MIt ctrl) (controlFloat f)
369
370
-- a = x..y
370
371
-- TODO it is unclear if this should mix floating point types too
371
372
Range low high bound ->
372
- ((,) <$> getRule low <*> getRule high)
373
+ ((,) <$> getRule ( MIt low) <*> getRule ( MIt high) )
373
374
<&> check . \ case
374
375
(Literal (Value (VFloat16 n) _), Literal (Value (VFloat16 m) _)) -> n <= f && range bound f m
375
376
(Literal (Value (VFloat32 n) _), Literal (Value (VFloat32 m) _)) -> n <= f && range bound f m
@@ -405,13 +406,13 @@ validateDouble f rule =
405
406
-- TODO: it is unclear if smaller floats should also validate
406
407
Literal (Value (VFloat64 f') _) -> pure $ check $ f == f'
407
408
-- a = foo / bar
408
- Choice opts -> validateChoice (validateDouble f) opts
409
+ Choice opts -> validateChoice (validateDouble f) ( MIt <$> opts)
409
410
-- a = foo .ctrl bar
410
- Control op tgt ctrl -> ctrlDispatch (validateDouble f) op tgt ctrl (controlDouble f)
411
+ Control op tgt ctrl -> ctrlDispatch (validateDouble f) op ( MIt tgt) ( MIt ctrl) (controlDouble f)
411
412
-- a = x..y
412
413
-- TODO it is unclear if this should mix floating point types too
413
414
Range low high bound ->
414
- ((,) <$> getRule low <*> getRule high)
415
+ ((,) <$> getRule ( MIt low) <*> getRule ( MIt high) )
415
416
<&> check . \ case
416
417
(Literal (Value (VFloat16 (float2Double -> n)) _), Literal (Value (VFloat16 (float2Double -> m)) _)) -> n <= f && range bound f m
417
418
(Literal (Value (VFloat32 (float2Double -> n)) _), Literal (Value (VFloat32 (float2Double -> m)) _)) -> n <= f && range bound f m
@@ -453,9 +454,9 @@ validateBool b rule =
453
454
-- a = true
454
455
Literal (Value (VBool b') _) -> pure $ check $ b == b'
455
456
-- a = foo .ctrl bar
456
- Control op tgt ctrl -> ctrlDispatch (validateBool b) op tgt ctrl (controlBool b)
457
+ Control op tgt ctrl -> ctrlDispatch (validateBool b) op ( MIt tgt) ( MIt ctrl) (controlBool b)
457
458
-- a = foo / bar
458
- Choice opts -> validateChoice (validateBool b) opts
459
+ Choice opts -> validateChoice (validateBool b) $ MIt <$> opts
459
460
_ -> pure UnapplicableRule
460
461
461
462
-- | Controls for `Bool`
@@ -486,7 +487,7 @@ validateSimple 23 rule =
486
487
-- a = undefined
487
488
Postlude PTUndefined -> pure Valid
488
489
-- a = foo / bar
489
- Choice opts -> validateChoice (validateSimple 23 ) opts
490
+ Choice opts -> validateChoice (validateSimple 23 ) $ MIt <$> opts
490
491
_ -> pure UnapplicableRule
491
492
validateSimple n _ = error $ " Found simple different to 23! please report this somewhere! Found: " <> show n
492
493
@@ -503,7 +504,7 @@ validateNull rule =
503
504
Postlude PTAny -> pure Valid
504
505
-- a = nil
505
506
Postlude PTNil -> pure Valid
506
- Choice opts -> validateChoice validateNull opts
507
+ Choice opts -> validateChoice validateNull $ MIt <$> opts
507
508
_ -> pure UnapplicableRule
508
509
509
510
--------------------------------------------------------------------------------
@@ -525,9 +526,9 @@ validateBytes bs rule =
525
526
-- a = h'123456'
526
527
Literal (Value (VBytes bs') _) -> pure $ check $ bs == bs'
527
528
-- a = foo .ctrl bar
528
- Control op tgt ctrl -> ctrlDispatch (validateBytes bs) op tgt ctrl (controlBytes bs)
529
+ Control op tgt ctrl -> ctrlDispatch (validateBytes bs) op ( MIt tgt) ( MIt ctrl) (controlBytes bs)
529
530
-- a = foo / bar
530
- Choice opts -> validateChoice (validateBytes bs) opts
531
+ Choice opts -> validateChoice (validateBytes bs) ( MIt <$> opts)
531
532
_ -> pure UnapplicableRule
532
533
533
534
-- | Controls for byte strings
@@ -543,7 +544,7 @@ controlBytes bs Size ctrl =
543
544
Literal (Value (VUInt (fromIntegral -> sz)) _) -> pure $ boolCtrl $ BS. length bs == sz
544
545
Range low high bound ->
545
546
let i = BS. length bs
546
- in ((,) <$> getRule low <*> getRule high)
547
+ in ((,) <$> getRule ( MIt low) <*> getRule ( MIt high) )
547
548
<&> boolCtrl . \ case
548
549
(Literal (Value (VUInt (fromIntegral -> n)) _), Literal (Value (VUInt (fromIntegral -> m)) _)) -> n <= i && range bound i m
549
550
(Literal (Value (VNInt (fromIntegral -> n)) _), Literal (Value (VUInt (fromIntegral -> m)) _)) -> - n <= i && range bound i m
@@ -553,9 +554,9 @@ controlBytes bs Bits ctrl = do
553
554
indices <-
554
555
getRule ctrl >>= \ case
555
556
Literal (Value (VUInt i') _) -> pure [i']
556
- Choice nodes -> getIndicesOfChoice nodes
557
- Range ff tt incl -> getIndicesOfRange ff tt incl
558
- Enum g -> getIndicesOfEnum g
557
+ Choice nodes -> getIndicesOfChoice $ MIt <$> nodes
558
+ Range ff tt incl -> getIndicesOfRange ( MIt ff) ( MIt tt) incl
559
+ Enum g -> getIndicesOfEnum $ MIt g
559
560
pure $ boolCtrl $ bitsControlCheck (map fromIntegral indices)
560
561
where
561
562
bitsControlCheck :: [Int ] -> Bool
@@ -578,7 +579,7 @@ controlBytes bs Cbor ctrl =
578
579
controlBytes bs Cborseq ctrl =
579
580
case deserialiseFromBytes decodeTerm (BSL. fromStrict (BS. snoc (BS. cons 0x9f bs) 0xff )) of
580
581
Right (BSL. null -> True , TListI terms) ->
581
- validateTerm (TList terms) (MIt (Array [MIt ( Occur ctrl OIZeroOrMore ) ])) >>= \ case
582
+ validateTerm (TList terms) (MIt (Array [Occur ctrl OIZeroOrMore ])) >>= \ case
582
583
CBORTermResult _ (Valid _) -> pure $ Right ()
583
584
CBORTermResult _ err -> error $ show err
584
585
0 commit comments