Skip to content

Commit 78d6c6d

Browse files
committed
Generalised Codec to provide AnnotatedCodec
1 parent 3b65eae commit 78d6c6d

File tree

4 files changed

+450
-82
lines changed

4 files changed

+450
-82
lines changed

typed-protocols/CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
# Revision history for typed-protocols
22

3+
## next release
4+
5+
### Breaking changes
6+
7+
* Annotated codecs which allow to retain original bytes received from the network.
8+
The `Codec` type evolved into a new `CodecF` data type, and two type aliases
9+
`AnnotatedCodec`, `Codec`.
10+
11+
### Non-breaking changes
12+
13+
## 1.0.0.0
14+
15+
* Hackage release.
16+
317
## 0.3.0.0
418

519
* `AnyMessageWithAgency` pattern synonym is exported as a constructor of `AnyMessage`.

typed-protocols/cborg/Network/TypedProtocol/Codec/CBOR.hs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ import Network.TypedProtocol.Codec
2525
import Network.TypedProtocol.Core
2626

2727

28-
-- | Construct a 'Codec' for a CBOR based serialisation format, using strict
28+
-- | Construct a 'CodecF' for a CBOR based serialisation format, using strict
2929
-- 'BS.ByteString's.
3030
--
31-
-- This is an adaptor between the @cborg@ library and the 'Codec' abstraction.
31+
-- This is an adaptor between the @cborg@ library and the 'CodecF' abstraction.
3232
--
3333
-- It takes encode and decode functions for the protocol messages that use the
3434
-- CBOR library encoder and decoder.
@@ -38,7 +38,7 @@ import Network.TypedProtocol.Core
3838
-- natively produces chunks).
3939
--
4040
mkCodecCborStrictBS
41-
:: forall ps m. MonadST m
41+
:: forall ps m f. MonadST m
4242

4343
=> (forall (st :: ps) (st' :: ps).
4444
StateTokenI st
@@ -49,10 +49,10 @@ mkCodecCborStrictBS
4949
-> (forall (st :: ps) s.
5050
ActiveState st
5151
=> StateToken st
52-
-> CBOR.Decoder s (SomeMessage st))
52+
-> CBOR.Decoder s (f st))
5353
-- ^ cbor decoder
5454

55-
-> Codec ps CBOR.DeserialiseFailure m BS.ByteString
55+
-> CodecF ps CBOR.DeserialiseFailure m f BS.ByteString
5656
mkCodecCborStrictBS cborMsgEncode cborMsgDecode =
5757
Codec {
5858
encode = \msg -> convertCborEncoder cborMsgEncode msg,
@@ -65,11 +65,12 @@ mkCodecCborStrictBS cborMsgEncode cborMsgDecode =
6565
. cborEncode
6666

6767
convertCborDecoder
68-
:: (forall s. CBOR.Decoder s a)
69-
-> m (DecodeStep BS.ByteString CBOR.DeserialiseFailure m a)
68+
:: (forall s. CBOR.Decoder s (f a))
69+
-> m (DecodeStep BS.ByteString CBOR.DeserialiseFailure m (f a))
7070
convertCborDecoder cborDecode =
7171
convertCborDecoderBS cborDecode stToIO
7272

73+
7374
convertCborDecoderBS
7475
:: forall s m a. Functor m
7576
=> CBOR.Decoder s a
@@ -89,16 +90,16 @@ convertCborDecoderBS cborDecode liftST =
8990
go (CBOR.Partial k) = DecodePartial (fmap go . liftST . k)
9091

9192

92-
-- | Construct a 'Codec' for a CBOR based serialisation format, using lazy
93+
-- | Construct a 'CodecF' for a CBOR based serialisation format, using lazy
9394
-- 'BS.ByteString's.
9495
--
95-
-- This is an adaptor between the @cborg@ library and the 'Codec' abstraction.
96+
-- This is an adaptor between the @cborg@ library and the 'CodecF' abstraction.
9697
--
9798
-- It takes encode and decode functions for the protocol messages that use the
9899
-- CBOR library encoder and decoder.
99100
--
100101
mkCodecCborLazyBS
101-
:: forall ps m. MonadST m
102+
:: forall ps m f. MonadST m
102103

103104
=> (forall (st :: ps) (st' :: ps).
104105
StateTokenI st
@@ -109,10 +110,10 @@ mkCodecCborLazyBS
109110
-> (forall (st :: ps) s.
110111
ActiveState st
111112
=> StateToken st
112-
-> CBOR.Decoder s (SomeMessage st))
113+
-> CBOR.Decoder s (f st))
113114
-- ^ cbor decoder
114115

115-
-> Codec ps CBOR.DeserialiseFailure m LBS.ByteString
116+
-> CodecF ps CBOR.DeserialiseFailure m f LBS.ByteString
116117
mkCodecCborLazyBS cborMsgEncode cborMsgDecode =
117118
Codec {
118119
encode = \msg -> convertCborEncoder cborMsgEncode msg,
@@ -126,11 +127,12 @@ mkCodecCborLazyBS cborMsgEncode cborMsgDecode =
126127
. cborEncode
127128

128129
convertCborDecoder
129-
:: (forall s. CBOR.Decoder s a)
130-
-> m (DecodeStep LBS.ByteString CBOR.DeserialiseFailure m a)
130+
:: (forall s. CBOR.Decoder s (f a))
131+
-> m (DecodeStep LBS.ByteString CBOR.DeserialiseFailure m (f a))
131132
convertCborDecoder cborDecode =
132133
convertCborDecoderLBS cborDecode stToIO
133134

135+
134136
convertCborDecoderLBS
135137
:: forall s m a. Monad m
136138
=> CBOR.Decoder s a

0 commit comments

Comments
 (0)