Skip to content

Commit 38016d6

Browse files
committed
Add tag traces
1 parent 1b92e54 commit 38016d6

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/Codec/CBOR/Cuddle/CBOR/Validator.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -706,8 +706,8 @@ validateTagged cddl tag term rule =
706706
Tag tag' rule' ->
707707
-- If the tag does not match, this is a direct fail
708708
if tag == tag'
709-
then validateTerm cddl term rule'
710-
else evidence . UnapplicableRule $ mapIndex rule
709+
then mapTrace (TagTrace tag) $ validateTerm cddl term rule'
710+
else evidence $ InvalidTag tag
711711
Choice opts -> validateChoice (validateTagged cddl tag term) opts
712712
_ -> unapplicable rule
713713

src/Codec/CBOR/Cuddle/CBOR/Validator/Trace.hs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import Data.List.NonEmpty (NonEmpty)
4747
import Data.Text (Text)
4848
import Data.Text qualified as T
4949
import Data.Type.Equality (TestEquality (..), (:~:) (..))
50+
import Data.Word (Word64)
5051
import Prettyprinter (
5152
Doc,
5253
Pretty (..),
@@ -176,6 +177,8 @@ data ValidationTrace (v :: Validity) where
176177
ChoiceBranch :: Int -> ValidationTrace IsValid -> ValidationTrace IsValid
177178
ListTrace :: ListValidationTrace v -> ValidationTrace v
178179
MapTrace :: MapValidationTrace v -> ValidationTrace v
180+
TagTrace :: Word64 -> ValidationTrace v -> ValidationTrace v
181+
InvalidTag :: Word64 -> ValidationTrace IsInvalid
179182

180183
deriving instance Show (ValidationTrace v)
181184

@@ -259,9 +262,11 @@ instance IsValidationTrace ValidationTrace where
259262
UnapplicableRule {} -> SInvalid
260263
CustomFailure {} -> SInvalid
261264
UnsatisfiedControl {} -> SInvalid
265+
InvalidTag {} -> SInvalid
262266
ReferenceRule _ x -> traceValidity x
263267
ListTrace x -> traceValidity x
264268
MapTrace x -> traceValidity x
269+
TagTrace _ x -> traceValidity x
265270

266271
measureProgress = \case
267272
TerminalRule {} -> 1
@@ -270,9 +275,11 @@ instance IsValidationTrace ValidationTrace where
270275
UnapplicableRule {} -> 0
271276
CustomFailure {} -> 0
272277
UnsatisfiedControl {} -> 0
273-
(ReferenceRule _ x) -> succ $ measureProgress x
274-
(ListTrace x) -> measureProgress x
275-
(MapTrace x) -> measureProgress x
278+
InvalidTag {} -> 0
279+
ReferenceRule _ x -> succ $ measureProgress x
280+
ListTrace x -> measureProgress x
281+
MapTrace x -> measureProgress x
282+
TagTrace _ x -> succ $ measureProgress x
276283

277284
instance IsValidationTrace ListValidationTrace where
278285
traceValidity = \case
@@ -453,6 +460,13 @@ prettyValidationTrace opts = \case
453460
[ "map"
454461
, nestContainer $ prettyMapValidationResult opts m
455462
]
463+
TagTrace t x ->
464+
vsep
465+
[ "tag:" <+> annotate (color Green) ("#6." <> pretty t)
466+
, nestContainer $ prettyValidationTrace opts x
467+
]
468+
InvalidTag t ->
469+
"expected tag #6." <> pretty t
456470

457471
showValidationTrace :: ValidationTrace v -> String
458472
showValidationTrace =

0 commit comments

Comments
 (0)