nwaku/waku/waku_metadata/rpc.nim

77 lines
1.5 KiB
Nim
Raw Normal View History

when (NimMajor, NimMinor) < (1, 4):
{.push raises: [Defect].}
else:
{.push raises: [].}
import
std/options
import
../common/protobuf
type
WakuMetadataRequest* = object
clusterId*: Option[uint32]
shards*: seq[uint32]
type
WakuMetadataResponse* = object
clusterId*: Option[uint32]
shards*: seq[uint32]
proc encode*(rpc: WakuMetadataRequest): ProtoBuffer =
var pb = initProtoBuffer()
pb.write3(1, rpc.clusterId)
for shard in rpc.shards:
pb.write3(2, shard)
pb.finish3()
pb
proc decode*(T: type WakuMetadataRequest, buffer: seq[byte]): ProtoResult[T] =
let pb = initProtoBuffer(buffer)
var rpc = WakuMetadataRequest()
var clusterId: uint64
if not ?pb.getField(1, clusterId):
rpc.clusterId = none(uint32)
else:
rpc.clusterId = some(clusterId.uint32)
var shards: seq[uint64]
if ?pb.getRepeatedField(2, shards):
for shard in shards:
rpc.shards.add(shard.uint32)
ok(rpc)
proc encode*(rpc: WakuMetadataResponse): ProtoBuffer =
var pb = initProtoBuffer()
pb.write3(1, rpc.clusterId)
for shard in rpc.shards:
pb.write3(2, shard)
pb.finish3()
pb
proc decode*(T: type WakuMetadataResponse, buffer: seq[byte]): ProtoResult[T] =
let pb = initProtoBuffer(buffer)
var rpc = WakuMetadataResponse()
var clusterId: uint64
if not ?pb.getField(1, clusterId):
rpc.clusterId = none(uint32)
else:
rpc.clusterId = some(clusterId.uint32)
var shards: seq[uint64]
if ?pb.getRepeatedField(2, shards):
for shard in shards:
rpc.shards.add(shard.uint32)
ok(rpc)