Skip to content

Commit 94d6904

Browse files
ch1boSoupstraw
authored andcommitted
Hex-encode the CBOR output
The Haskell specific escaped show instance is less useful and using hex encoding is the most common way to share CBOR encoded bytes (at least in the Cardano ecosystem).
1 parent 6a4b295 commit 94d6904

File tree

3 files changed

+27
-23
lines changed

3 files changed

+27
-23
lines changed

bin/Main.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import Codec.CBOR.FlatTerm (toFlatTerm)
1414
import Codec.CBOR.Pretty (prettyHexEnc)
1515
import Codec.CBOR.Term (encodeTerm)
1616
import Codec.CBOR.Write (toStrictByteString)
17+
import Data.ByteString.Base16 qualified as Base16
18+
import Data.ByteString.Char8 qualified as BSC
1719
import Data.Text qualified as T
1820
import Data.Text.IO qualified as T
1921
import Options.Applicative
@@ -153,7 +155,7 @@ run (Opts cmd cddlFile) = do
153155
in case outputFormat x of
154156
AsTerm -> print term
155157
AsFlatTerm -> print $ toFlatTerm (encodeTerm term)
156-
AsCBOR -> print . toStrictByteString $ encodeTerm term
158+
AsCBOR -> BSC.putStrLn . Base16.encode . toStrictByteString $ encodeTerm term
157159
AsPrettyCBOR -> putStrLn . prettyHexEnc $ encodeTerm term
158160

159161
putStrLnErr :: String -> IO ()

cuddle.cabal

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ library
4747
Codec.CBOR.Cuddle.CDDL
4848
Codec.CBOR.Cuddle.CDDL.CtlOp
4949
Codec.CBOR.Cuddle.CDDL.CTree
50-
Codec.CBOR.Cuddle.CDDL.Prelude
5150
Codec.CBOR.Cuddle.CDDL.Postlude
51+
Codec.CBOR.Cuddle.CDDL.Prelude
5252
Codec.CBOR.Cuddle.CDDL.Resolve
5353
Codec.CBOR.Cuddle.Huddle
5454
Codec.CBOR.Cuddle.Huddle.HuddleM
@@ -109,6 +109,8 @@ executable cuddle
109109
main-is: Main.hs
110110
build-depends:
111111
, base >=4.14.0.0
112+
, base16-bytestring
113+
, bytestring
112114
, cborg
113115
, cuddle
114116
, megaparsec
@@ -121,9 +123,9 @@ test-suite cuddle-test
121123
import: warnings, ghc2021
122124
default-language: Haskell2010
123125
other-modules:
126+
Test.Codec.CBOR.Cuddle.CDDL.Examples
124127
Test.Codec.CBOR.Cuddle.CDDL.Gen
125128
Test.Codec.CBOR.Cuddle.CDDL.Parser
126-
Test.Codec.CBOR.Cuddle.CDDL.Examples
127129
Test.Codec.CBOR.Cuddle.Huddle
128130

129131
-- other-extensions:

test/Test/Codec/CBOR/Cuddle/CDDL/Parser.hs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Codec.CBOR.Cuddle.CDDL.CtlOp qualified as CtlOp
77
import Codec.CBOR.Cuddle.Parser
88
import Codec.CBOR.Cuddle.Parser.Lexer (Parser)
99
import Codec.CBOR.Cuddle.Pretty ()
10-
import Data.List.NonEmpty (NonEmpty(..))
10+
import Data.List.NonEmpty (NonEmpty (..))
1111
import Data.Text qualified as T
1212
import Prettyprinter (Pretty, defaultLayoutOptions, layoutPretty, pretty)
1313
import Prettyprinter.Render.Text (renderStrict)
@@ -230,19 +230,19 @@ type2Spec = describe "type2" $ do
230230
)
231231
]
232232
:| [
233-
[ noComment $
234-
GEType
235-
Nothing
236-
Nothing
237-
( Type0
238-
( Type1
239-
(T2Name (Name "string") Nothing)
240-
Nothing
241-
:| []
242-
)
233+
[ noComment $
234+
GEType
235+
Nothing
236+
Nothing
237+
( Type0
238+
( Type1
239+
(T2Name (Name "string") Nothing)
240+
Nothing
241+
:| []
243242
)
244-
]
245-
]
243+
)
244+
]
245+
]
246246
)
247247
)
248248

@@ -257,13 +257,13 @@ type2Spec = describe "type2" $ do
257257
(Type0 ((:| []) (Type1 (T2Value (VUInt 0)) Nothing)))
258258
]
259259
:| [
260-
[ noComment $
261-
GEType
262-
Nothing
263-
Nothing
264-
(Type0 ((:| []) (Type1 (T2Value (VUInt 1)) Nothing)))
265-
]
266-
]
260+
[ noComment $
261+
GEType
262+
Nothing
263+
Nothing
264+
(Type0 ((:| []) (Type1 (T2Value (VUInt 1)) Nothing)))
265+
]
266+
]
267267
)
268268
)
269269
it "Trailing commas permitted" $

0 commit comments

Comments
 (0)