forked from chpatrick/codec
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodec.cabal
More file actions
144 lines (141 loc) · 4.96 KB
/
codec.cabal
File metadata and controls
144 lines (141 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
name: codec
version: 0.1
license: BSD3
synopsis: First-class record construction and bidirectional serialization
description:
= Data.Codec
#data.codec#
.
Tired of writing complementary @parseJSON@\/@toJSON@, @peek@\/@poke@ or
Binary @get@\/@put@ functions?
.
@codec@ provides easy bidirectional serialization of plain Haskell
records in any Applicative context. All you need to do is provide a
de\/serializer for every record field in any order you like, and you get
a de\/serializer for the whole structure. The type system ensures that
you provide every record exactly once. It also includes a library for
general record construction in an Applicative context, of which creating
codecs is just one application.
.
JSON!
.
> userCodec :: JSONCodec User
> userCodec = obj "user object" $
> User
> $>> f_username >-< "user"
> >>> f_userEmail >-< "email"
> >>> f_userLanguages >-< "languages"
> >>> f_userReferrer >-< opt "referrer"
>
> instance FromJSON User where
> parseJSON = parseVal userCodec
>
> instance ToJSON User where
> toJSON = produceVal userCodec
.
Bit fields!
.
> ipv4Codec :: BinaryCodec IPv4
> ipv4Codec = toBytes $
> IPv4
> $>> f_version >-< word8 4
> >>> f_ihl >-< word8 4
> >>> f_dscp >-< word8 6
> >>> f_ecn >-< word8 2
> >>> f_totalLength >-< word16be 16
> >>> f_identification >-< word16be 16
> >>> f_flags >-< word8 3
> >>> f_fragmentOffset >-< word16be 13
> >>> f_timeToLive >-< word8 8
> >>> f_protocol >-< word8 8
> >>> f_headerChecksum >-< word16be 16
> >>> f_sourceIP >-< word32be 32
> >>> f_destIP >-< word32be 32
>
> instance Binary IPv4 where
> get = parse ipv4Codec
> put = produce ipv4Codec
.
Storable!
.
> timeSpecCodec :: ForeignCodec TimeSpec
> timeSpecCodec =
> TimeSpec
> $>> f_seconds >-< field (#offset struct timespec, tv_sec) cInt
> >>> f_nanoseconds >-< field (#offset struct timespec, tv_nsec) cInt
>
> instance Storable TimeSpec where
> peek = peekWith timeSpecCodec
> poke = pokeWith timeSpecCodec
> ...
.
All of these examples use the same types and logic for constructing
Codecs, and it\'s very easy to create Codecs for any
parsing\/serialization library.
.
See "Data.Codec" for an introduction.
author: Patrick Chilton
maintainer: chpatrick@gmail.com
-- copyright:
category: Data
build-type: Simple
-- extra-source-files:
cabal-version: >=1.10
library
exposed-modules: Data.Codec,
Data.Codec.Testing,
Control.Lens.Codec,
Data.Aeson.Codec,
Data.Binary.Codec,
Data.Binary.Bits.Codec,
Foreign.Codec
-- other-modules:
default-extensions: TemplateHaskell,
MultiParamTypeClasses,
FlexibleContexts,
FlexibleInstances,
LambdaCase,
FunctionalDependencies,
DeriveFunctor,
ScopedTypeVariables
build-depends: base >=4.6,
bytestring >=0.10,
binary >=0.7,
binary-bits >=0.5,
template-haskell >=2.8,
mtl >= 2.2.1,
aeson >= 0.8.0.2,
text >= 1.2.0.4,
unordered-containers >= 0.2.5.1,
data-default-class >= 0.0.1,
transformers >= 0.4.2.0
-- hs-source-dirs:
default-language: Haskell2010
ghc-options: -Wall -O2 -fno-warn-orphans
test-suite Examples
default-language: Haskell2010
type: exitcode-stdio-1.0
main-is: TestExamples.hs
other-modules: Examples.Foreign,
Examples.IP,
Examples.JSON,
Examples.Multi,
Examples.Tar
build-depends: base >=4.6,
bytestring >=0.10,
binary >=0.7,
binary-bits >=0.5,
template-haskell >=2.8,
mtl >= 2.2.1,
aeson >= 0.8.0.2,
text >= 1.2.0.4,
unordered-containers >= 0.2.5.1,
data-default-class >= 0.0.1,
transformers >= 0.4.2.0
default-extensions: TemplateHaskell,
MultiParamTypeClasses,
FlexibleInstances,
DeriveFunctor,
FunctionalDependencies,
LambdaCase,
ScopedTypeVariables