Skip to content

Commit 20f226f

Browse files
committed
Add 'IsSize' instance for 'Word'
This resolves issue #33
1 parent 7874af0 commit 20f226f

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/Codec/CBOR/Cuddle/Huddle.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,10 @@ class IsSize a where
431431
sizeAsCDDL :: a -> C.Type2
432432
sizeAsString :: a -> String
433433

434+
instance IsSize Word where
435+
sizeAsCDDL = C.T2Value . C.VUInt . fromIntegral
436+
sizeAsString = show
437+
434438
instance IsSize Word64 where
435439
sizeAsCDDL = C.T2Value . C.VUInt
436440
sizeAsString = show

test/Test/Codec/CBOR/Cuddle/Huddle.hs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ huddleSpec = describe "huddle" $ do
2222
mapSpec
2323
nestedSpec
2424
genericSpec
25+
constraintSpec
2526

2627
basicAssign :: Spec
2728
basicAssign = describe "basic assignment" $ do
@@ -111,6 +112,12 @@ genericSpec =
111112
toSortedCDDL (collectFrom ["mymap" =:= dict VUInt VText])
112113
`shouldMatchParseCDDL` "dict<a0, b0> = {* a0 => b0}\n mymap = dict<uint, text>"
113114

115+
constraintSpec :: Spec
116+
constraintSpec =
117+
describe "Constraints" $
118+
it "Size can take a Word" $
119+
toSortedCDDL (collectFrom ["sz" =:= VUInt `sized` (2 :: Word)])
120+
`shouldMatchParseCDDL` "sz = uint .size 2"
114121
--------------------------------------------------------------------------------
115122
-- Helper functions
116123
--------------------------------------------------------------------------------

0 commit comments

Comments
 (0)