-
Notifications
You must be signed in to change notification settings - Fork 108
/
Encoder.scala
166 lines (144 loc) · 6.34 KB
/
Encoder.scala
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* Copyright (c) 2013, Scodec
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package scodec
import scala.annotation.unchecked.uncheckedVariance
import scodec.bits.BitVector
/** Supports encoding a value of type `A` to a `BitVector`.
*/
trait Encoder[-A]:
self =>
/** Attempts to encode the specified value in to a bit vector.
*
* @param value value to encode
* @return error or binary encoding of the value
*/
def encode(value: A): Attempt[BitVector]
/** Provides a bound on the size of successfully encoded values.
*/
def sizeBound: SizeBound
/** Converts this encoder to an `Encoder[B]` using the supplied `B => A`.
*/
def contramap[B](f: B => A): Encoder[B] = new Encoder[B]:
def sizeBound = self.sizeBound
def encode(b: B) = self.encode(f(b))
/** Converts this encoder to an `Encoder[B]` using the supplied partial
* function from `B` to `A`. The encoding will fail for any `B` that
* `f` maps to `None`.
*/
def pcontramap[B](f: B => Option[A]): Encoder[B] = new Encoder[B]:
def sizeBound = self.sizeBound
def encode(b: B): Attempt[BitVector] =
f(b).map(self.encode).getOrElse(Attempt.failure(Err(s"widening failed: $b")))
/** Converts this encoder to an `Encoder[B]` using the supplied `B => Attempt[A]`.
*/
def econtramap[B](f: B => Attempt[A]): Encoder[B] = new Encoder[B]:
def sizeBound = self.sizeBound
def encode(b: B) = f(b).flatMap(self.encode)
/** Converts this encoder to a new encoder that compacts the generated bit vector before returning it
*/
def compact: Encoder[A] = new Encoder[A]:
def sizeBound = self.sizeBound
def encode(a: A) = self.encode(a).map(_.compact)
/** Gets this as an `Encoder`.
*/
def asEncoder: Encoder[A] = this
/** Converts this to a codec that fails decoding with an error.
*/
def encodeOnly: Codec[A @uncheckedVariance] = new Codec[A]:
def sizeBound = self.sizeBound
def encode(a: A) = self.encode(a)
def decode(bits: BitVector) = Attempt.failure(Err("decoding not supported"))
/** Encodes all elements of the specified sequence and concatenates the results, or returns the first encountered error.
*/
def encodeAll(as: Iterable[A]): Attempt[BitVector] =
val buf = new collection.mutable.ArrayBuffer[BitVector](as.size)
var failure: Err | Null = null
as.foreach { a =>
if failure == null then
encode(a) match
case Attempt.Successful(aa) => buf += aa
case Attempt.Failure(err) => failure = err.pushContext(buf.size.toString)
}
if failure == null then
def merge(offset: Int, size: Int): BitVector = size match
case 0 => BitVector.empty
case 1 => buf(offset)
case _ =>
val half = size / 2
merge(offset, half) ++ merge(offset + half, half + (if size % 2 == 0 then 0 else 1))
Attempt.successful(merge(0, buf.size))
else
Attempt.failure(
failure.nn
) // FIXME .nn shouldn't be necessary here b/c failure is checked for null above
/** Provides functions for working with encoders.
*/
trait EncoderFunctions:
/** Encodes the specified value using the given `Encoder[A]`.
*/
final def encode[A](a: A)(using encA: Encoder[A]): Attempt[BitVector] =
encA.encode(a)
/** Encodes the specified values, one after the other, to a bit vector using the specified encoders.
*/
final def encodeBoth[A, B](encA: Encoder[A], encB: Encoder[B])(a: A, b: B): Attempt[BitVector] =
for
encodedA <- encA.encode(a)
encodedB <- encB.encode(b)
yield encodedA ++ encodedB
/** Creates an encoder that encodes with each of the specified encoders, returning
* the first successful result.
*/
final def choiceEncoder[A](encoders: Encoder[A]*): Encoder[A] = new Encoder[A]:
def sizeBound = SizeBound.choice(encoders.map(_.sizeBound))
def encode(a: A) =
@annotation.tailrec
def go(rem: List[Encoder[A]], errs: List[Err]): Attempt[BitVector] = rem match
case Nil => Attempt.failure(Err(errs.reverse))
case hd :: tl =>
hd.encode(a) match
case res @ Attempt.Successful(_) => res
case Attempt.Failure(err) => go(tl, err :: errs)
if encoders.isEmpty then Attempt.failure(Err("no encoders provided"))
else go(encoders.toList, Nil)
/** Companion for [[Encoder]].
*/
object Encoder extends EncoderFunctions:
inline def apply[A](using e: Encoder[A]): Encoder[A] = e
/** Creates an encoder from the specified function.
*/
def apply[A](f: A => Attempt[BitVector]): Encoder[A] = new Encoder[A]:
def sizeBound = SizeBound.unknown
def encode(value: A) = f(value)
given Transform[Encoder] with
extension [A, B](fa: Encoder[A])
def exmap(f: A => Attempt[B], g: B => Attempt[A]): Encoder[B] =
fa.econtramap(g)
extension [A](encoder: Encoder[A])
def as[B](using iso: Iso[A, B]): Encoder[B] = encoder.contramap(iso.from)