Primitives.scala 3.35 KB
Newer Older
1
package scodec.protocols.protobuf.v3
Luciano Joublanc's avatar
Luciano Joublanc committed
2 3 4

import scodec.Codec

5
/** Codecs for the primitive elements in protobuffers. These are not 'full'
6 7 8 9 10 11
  * codecs as they don't include framing information, i.e. the index +
  * wire-type information.
  *
  * The API doc has been mostly copied from
  * [[https://developers.google.com/protocol-buffers/docs/proto3#scalar this]]
  * table.
12 13
  *
  * @see [[https://developers.google.com/protocol-buffers/docs/encoding]] for
14
  *      the specification.
15
  */
16
trait Primitives { self =>
Luciano Joublanc's avatar
Luciano Joublanc committed
17

18
  /** The type used to represent unsinged 32-bit integers. */
Luciano Joublanc's avatar
Luciano Joublanc committed
19 20
  type UInt

21
  /** The type used to represent unsinged 64-bit integers. */
Luciano Joublanc's avatar
Luciano Joublanc committed
22 23
  type ULong

24 25 26 27 28 29 30 31 32 33 34
  /** Witness that [[UInt]] is a `scala.math.Integral`.
    * This guarantees portability across platforms. A typical implementation will
    * have an `implicit instance: Integral[UInt]` in the companion of `UInt`.
    * @example {{{
    * scala> import JVMPrimitives.{UInt, ULong}
    * scala> import Integral.Implicits._
    *
    * scala> UInt(1) + UInt(2)
    * res1: scodec.protocols.protobuf.v3.JVMPrimitives.UInt = UInt(3)
    * }}}
    */
35 36
  def uintIsIntegral: Integral[UInt]

37 38 39
  /** Witness that [[ULong]] is a `scala.math.Integral`.
    * @see [[uintIsIntegral]] for further explanation
    */
40 41
  def ulongIsIntegral: Integral[ULong]

42
  /** IEEE 754 double precision binary. */
43
  def double: PartialCodec.Aux[Double, 1]
Luciano Joublanc's avatar
Luciano Joublanc committed
44

45
  /** IEEE 754 single precision binary. */
46
  def float: PartialCodec.Aux[Float, 5]
Luciano Joublanc's avatar
Luciano Joublanc committed
47

48 49 50
  /** Uses variable-length encoding. Inefficient for encoding negative numbers
    * – if your field is likely to have negative values, use [[sint32]] instead.
    */
51
  def int32: PartialCodec.Aux[Int, 0]
Luciano Joublanc's avatar
Luciano Joublanc committed
52

53 54 55
  /** Uses variable-length encoding. Inefficient for encoding negative numbers
    * – if your field is likely to have negative values, use [[sint64]] instead.
    */
56
  def int64: PartialCodec.Aux[Long, 0]
Luciano Joublanc's avatar
Luciano Joublanc committed
57

58
  /** Uses variable-length encoding. */
59
  def uint32: PartialCodec.Aux[UInt, 0]
Luciano Joublanc's avatar
Luciano Joublanc committed
60

61
  /** Uses variable-length encoding. */
62
  def uint64: PartialCodec.Aux[ULong, 0]
Luciano Joublanc's avatar
Luciano Joublanc committed
63

64 65 66
  /** Uses variable-length encoding. Signed `int` value. These more efficiently
    * encode negative numbers than regular [[int32]]s.
    */
67
  def sint32: PartialCodec.Aux[Int, 0]
Luciano Joublanc's avatar
Luciano Joublanc committed
68

69 70 71
  /** Uses variable-length encoding. Signed `int` value. These more efficiently
    * encode negative numbers than regular [[int64]]s.
    */
72
  def sint64: PartialCodec.Aux[Long, 0]
Luciano Joublanc's avatar
Luciano Joublanc committed
73

74 75 76
  /** Always four bytes. More efficient than [[uint32]] if values are often
    * greater than 228.
    */
77
  def fixed32: PartialCodec.Aux[UInt, 5]
Luciano Joublanc's avatar
Luciano Joublanc committed
78

79 80
  /** Always eight bytes. More efficient than [[uint64]] if values are often
    * greater than 256. */
81
  def fixed64: PartialCodec.Aux[ULong, 1]
Luciano Joublanc's avatar
Luciano Joublanc committed
82

83
  /** Always four bytes. */
84
  def sfixed32: PartialCodec.Aux[Int, 5]
Luciano Joublanc's avatar
Luciano Joublanc committed
85

86
  /** Always eight bytes. */
87
  def sfixed64: PartialCodec.Aux[Long, 1]
Luciano Joublanc's avatar
Luciano Joublanc committed
88

89
  /** Always one byte. */
90
  def bool: PartialCodec.Aux[Boolean, 0]
Luciano Joublanc's avatar
Luciano Joublanc committed
91

92 93 94 95
  /** A string must always contain UTF-8 encoded or 7-bit ASCII text, and
    * cannot be longer than 232.
    * @param charset Default to UTF-8.
    */
96 97
  def string(
      implicit charset: java.nio.charset.Charset
98
  ): PartialCodec.Aux[String, 2]
Luciano Joublanc's avatar
Luciano Joublanc committed
99

100
  /** May contain any arbitrary sequence of bytes no longer than 232. */
101
  def bytes: PartialCodec.Aux[scodec.bits.ByteVector, 2]
Luciano Joublanc's avatar
Luciano Joublanc committed
102

103
  protected[protobuf] def lengthDelimited[A] =
104
    scodec.codecs.variableSizeBytes(self.int32.force, _: Codec[A], 0)
Luciano Joublanc's avatar
Luciano Joublanc committed
105
}