protobuf_serialization/types

Source   Edit  

Types

PBOption[defaultValue] = object
Optional value type for protobuf fields. Allows distinguishing between "field not set" and "field set to default value". Only works with proto2. Source   Edit  
ProtobufEOFError = object of ProtobufReadError
Error raised when unexpected end of input is encountered. Source   Edit  
ProtobufError = object of SerializationError
Error raised when a protobuf decoding/encoding operation fails. Source   Edit  
ProtobufExt[FieldType; RootType; fieldName] = object
Type marker for protobuf extension types. Used internally for type extensions. Source   Edit  
ProtobufFlags = enum
  VarIntLengthPrefix         ## Flags that control protobuf encoding behavior.
Source   Edit  
ProtobufGroupError = object of ProtobufValueError
Error raised when a group (deprecated protobuf feature) is encountered. Source   Edit  
ProtobufMessageError = object of ProtobufReadError
Error raised when a message structure is invalid. Source   Edit  
ProtobufReader = ref object
  stream*: InputStream
  closeAfter*: bool
Reader for decoding protobuf format to Nim objects. Source   Edit  
ProtobufReadError = object of ProtobufError
Error raised when reading a protobuf message fails. Source   Edit  
ProtobufValueError = object of ProtobufReadError
Error raised when a field value is invalid. Source   Edit  
ProtobufWriter = object
  stream*: OutputStream
  flags*: set[ProtobufFlags]
Writer for encoding Nim objects to protobuf format. Source   Edit  

Procs

proc finish(writer: ProtobufWriter): seq[byte] {....raises: [IOError], raises: [],
    gcsafe, tags: [RootEffect], forbids: [].}
Source   Edit  
func get(opt: PBOption): auto {....raises: [], gcsafe.}
Returns the value if set, otherwise returns the default value. Source   Edit  
func init(opt: var PBOption; val: auto) {....raises: [], gcsafe.}
Source   Edit  
func init(T: type ProtobufReader; stream: InputStream; closeAfter: bool = true): T:type {.
    inline, ...raises: [], gcsafe.}
Source   Edit  
func init(T: type ProtobufWriter; stream: OutputStream;
          flags: static set[ProtobufFlags] = {}): T:type {.inline, ...raises: [],
    gcsafe.}
Source   Edit  
func isNone(opt: PBOption): bool {.inline, ...raises: [], gcsafe.}
Returns true if the optional value is not set. Source   Edit  
func isSome(opt: PBOption): bool {.inline, ...raises: [], gcsafe.}
Returns true if the optional value is set. Source   Edit  
func pbSome[T: PBOption](optType: typedesc[T]; value: auto): T {.inline,
    ...raises: [], gcsafe.}
Creates a PBOption with the given value marked as set. Use this to create optional values that are present. Source   Edit  

Converters

converter toValue(opt: PBOption): auto {.inline, ...raises: [], gcsafe.}
Source   Edit  

Templates

template ext() {.pragma.}
Pragma to mark a field as using a type extension. The field's type must have custom serialization logic defined via computeFieldSize, writeField, and readFieldInto. Source   Edit  
template fieldNumber(num: int) {.pragma.}
Pragma to assign a field number to a protobuf field. Required for all fields. Field numbers must be unique within a message and should never change. Source   Edit  
template fixed() {.pragma.}
Pragma for fixed-width encoding. Always uses 4 or 8 bytes regardless of value. Use for large values or when predictable size matters. Use for fixed32, fixed64, sfixed32, sfixed64. Source   Edit  
template implicit() {.pragma.}
Source   Edit  
template mget(opt: var PBOption): untyped {..}
Returns a mutable reference to the value, marking it as set. Use this to modify the value in place. Source   Edit  
template oneof() {.pragma.}
Pragma to mark a field as a oneof (union type). Only one field in the oneof can be set at a time. Source   Edit  
template packed(v: bool) {.pragma.}
Pragma to enable packed encoding for repeated fields. More efficient for scalar numeric types. All elements are encoded as a single length-delimited field instead of separate entries. Source   Edit  
template pbNone(value: untyped): untyped {..}
Creates a PBOption marked as not set (absent). The parameter specifies the default value type. Source   Edit  
template pbSome(value: untyped): untyped {..}
Creates a PBOption with the given value marked as set. The default value is inferred from the type of the value. Shorthand for pbSome(PBOption[...], value). Source   Edit  
template pint() {.pragma.}
Pragma for "plain int" encoding using standard varint. Efficient for positive values, inefficient for negative values. Use for int32, int64, uint32, uint64. Source   Edit  
template proto(edition = 2023) {.pragma.}
Source   Edit  
template proto2() {.pragma.}
Pragma to mark a type as using Protocol Buffers version 2 syntax. Proto2 supports required fields and explicit optional fields. Source   Edit  
template proto3() {.pragma.}
Pragma to mark a type as using Protocol Buffers version 3 syntax. Proto3 has implicit default values and no required keyword. Source   Edit  
template required() {.pragma.}
Pragma to mark a proto2 field as required. The field must be present in encoded messages. Only available in proto2; proto3 does not support required fields. Source   Edit  
template sint() {.pragma.}
Pragma for "signed int" encoding using zig-zag varint. Efficient for negative values. Use for int32, int64 when negative values are common. Source   Edit  
template valueOr(opt: PBOption; def: untyped): untyped {..}
Returns the value if set, otherwise returns the provided default. Unlike get, this allows you to specify a custom default at call site. Source   Edit