Skip to content

Commit cae610f

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

File tree

4 files changed

+450
-81
lines changed

4 files changed

+450
-81
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: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ import Network.TypedProtocol.Codec
2525
import Network.TypedProtocol.Core
2626

2727

28-
-- | Construct a 'Codec' for a CBOR based serialisation format, using strict
28+
type DeserialiseFailure = CBOR.DeserialiseFailure
29+
30+
-- | Construct a 'CodecF' for a CBOR based serialisation format, using strict
2931
-- 'BS.ByteString's.
3032
--
31-
-- This is an adaptor between the @cborg@ library and the 'Codec' abstraction.
33+
-- This is an adaptor between the @cborg@ library and the 'CodecF' abstraction.
3234
--
3335
-- It takes encode and decode functions for the protocol messages that use the
3436
-- CBOR library encoder and decoder.
@@ -38,7 +40,7 @@ import Network.TypedProtocol.Core
3840
-- natively produces chunks).
3941
--
4042
mkCodecCborStrictBS
41-
:: forall ps m. MonadST m
43+
:: forall ps m f. MonadST m
4244

4345
=> (forall (st :: ps) (st' :: ps).
4446
StateTokenI st
@@ -49,10 +51,10 @@ mkCodecCborStrictBS
4951
-> (forall (st :: ps) s.
5052
ActiveState st
5153
=> StateToken st
52-
-> CBOR.Decoder s (SomeMessage st))
54+
-> CBOR.Decoder s (f st))
5355
-- ^ cbor decoder
5456

55-
-> Codec ps CBOR.DeserialiseFailure m BS.ByteString
57+
-> CodecF ps DeserialiseFailure m f BS.ByteString
5658
mkCodecCborStrictBS cborMsgEncode cborMsgDecode =
5759
Codec {
5860
encode = \msg -> convertCborEncoder cborMsgEncode msg,
@@ -65,11 +67,12 @@ mkCodecCborStrictBS cborMsgEncode cborMsgDecode =
6567
. cborEncode
6668

6769
convertCborDecoder
68-
:: (forall s. CBOR.Decoder s a)
69-
-> m (DecodeStep BS.ByteString CBOR.DeserialiseFailure m a)
70+
:: (forall s. CBOR.Decoder s (f a))
71+
-> m (DecodeStep BS.ByteString DeserialiseFailure m (f a))
7072
convertCborDecoder cborDecode =
7173
convertCborDecoderBS cborDecode stToIO
7274

75+
7376
convertCborDecoderBS
7477
:: forall s m a. Functor m
7578
=> CBOR.Decoder s a
@@ -89,16 +92,16 @@ convertCborDecoderBS cborDecode liftST =
8992
go (CBOR.Partial k) = DecodePartial (fmap go . liftST . k)
9093

9194

92-
-- | Construct a 'Codec' for a CBOR based serialisation format, using lazy
95+
-- | Construct a 'CodecF' for a CBOR based serialisation format, using lazy
9396
-- 'BS.ByteString's.
9497
--
95-
-- This is an adaptor between the @cborg@ library and the 'Codec' abstraction.
98+
-- This is an adaptor between the @cborg@ library and the 'CodecF' abstraction.
9699
--
97100
-- It takes encode and decode functions for the protocol messages that use the
98101
-- CBOR library encoder and decoder.
99102
--
100103
mkCodecCborLazyBS
101-
:: forall ps m. MonadST m
104+
:: forall ps m f. MonadST m
102105

103106
=> (forall (st :: ps) (st' :: ps).
104107
StateTokenI st
@@ -109,10 +112,10 @@ mkCodecCborLazyBS
109112
-> (forall (st :: ps) s.
110113
ActiveState st
111114
=> StateToken st
112-
-> CBOR.Decoder s (SomeMessage st))
115+
-> CBOR.Decoder s (f st))
113116
-- ^ cbor decoder
114117

115-
-> Codec ps CBOR.DeserialiseFailure m LBS.ByteString
118+
-> CodecF ps CBOR.DeserialiseFailure m f LBS.ByteString
116119
mkCodecCborLazyBS cborMsgEncode cborMsgDecode =
117120
Codec {
118121
encode = \msg -> convertCborEncoder cborMsgEncode msg,
@@ -126,11 +129,12 @@ mkCodecCborLazyBS cborMsgEncode cborMsgDecode =
126129
. cborEncode
127130

128131
convertCborDecoder
129-
:: (forall s. CBOR.Decoder s a)
130-
-> m (DecodeStep LBS.ByteString CBOR.DeserialiseFailure m a)
132+
:: (forall s. CBOR.Decoder s (f a))
133+
-> m (DecodeStep LBS.ByteString CBOR.DeserialiseFailure m (f a))
131134
convertCborDecoder cborDecode =
132135
convertCborDecoderLBS cborDecode stToIO
133136

137+
134138
convertCborDecoderLBS
135139
:: forall s m a. Monad m
136140
=> CBOR.Decoder s a

0 commit comments

Comments
 (0)