2022-11-14 23:42:57 +00:00
|
|
|
## Nim-Codex
|
|
|
|
## Copyright (c) 2022 Status Research & Development GmbH
|
|
|
|
## Licensed under either of
|
|
|
|
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
## at your option.
|
|
|
|
## This file may not be copied, modified, or distributed except according to
|
|
|
|
## those terms.
|
|
|
|
|
|
|
|
import std/sugar
|
|
|
|
|
|
|
|
import pkg/presto
|
|
|
|
import pkg/chronos
|
|
|
|
import pkg/libp2p
|
|
|
|
import pkg/stew/base10
|
|
|
|
import pkg/stew/byteutils
|
|
|
|
import pkg/stew/results
|
|
|
|
import pkg/stint
|
|
|
|
|
|
|
|
import ../sales
|
|
|
|
import ../purchasing
|
2023-06-21 05:46:18 +00:00
|
|
|
import ../utils/stintutils
|
2022-11-14 23:42:57 +00:00
|
|
|
|
|
|
|
proc encodeString*(cid: type Cid): Result[string, cstring] =
|
|
|
|
ok($cid)
|
|
|
|
|
|
|
|
proc decodeString*(T: type Cid, value: string): Result[Cid, cstring] =
|
|
|
|
Cid
|
|
|
|
.init(value)
|
|
|
|
.mapErr do(e: CidError) -> cstring:
|
|
|
|
case e
|
|
|
|
of CidError.Incorrect: "Incorrect Cid".cstring
|
|
|
|
of CidError.Unsupported: "Unsupported Cid".cstring
|
|
|
|
of CidError.Overrun: "Overrun Cid".cstring
|
|
|
|
else: "Error parsing Cid".cstring
|
|
|
|
|
|
|
|
proc encodeString*(peerId: PeerId): Result[string, cstring] =
|
|
|
|
ok($peerId)
|
|
|
|
|
|
|
|
proc decodeString*(T: type PeerId, value: string): Result[PeerId, cstring] =
|
2023-03-10 07:02:54 +00:00
|
|
|
PeerId.init(value)
|
2022-11-14 23:42:57 +00:00
|
|
|
|
|
|
|
proc encodeString*(address: MultiAddress): Result[string, cstring] =
|
|
|
|
ok($address)
|
|
|
|
|
|
|
|
proc decodeString*(T: type MultiAddress, value: string): Result[MultiAddress, cstring] =
|
|
|
|
MultiAddress
|
|
|
|
.init(value)
|
|
|
|
.mapErr do(e: string) -> cstring: cstring(e)
|
|
|
|
|
|
|
|
proc decodeString*(T: type SomeUnsignedInt, value: string): Result[T, cstring] =
|
|
|
|
Base10.decode(T, value)
|
|
|
|
|
|
|
|
proc encodeString*(value: SomeUnsignedInt): Result[string, cstring] =
|
|
|
|
ok(Base10.toString(value))
|
|
|
|
|
|
|
|
proc decodeString*(T: type Duration, value: string): Result[T, cstring] =
|
|
|
|
let v = ? Base10.decode(uint32, value)
|
|
|
|
ok(v.minutes)
|
|
|
|
|
|
|
|
proc encodeString*(value: Duration): Result[string, cstring] =
|
|
|
|
ok($value)
|
|
|
|
|
|
|
|
proc decodeString*(T: type bool, value: string): Result[T, cstring] =
|
|
|
|
try:
|
|
|
|
ok(value.parseBool())
|
|
|
|
except CatchableError as exc:
|
|
|
|
let s: cstring = exc.msg
|
|
|
|
err(s) # err(exc.msg) won't compile
|
|
|
|
|
|
|
|
proc encodeString*(value: bool): Result[string, cstring] =
|
|
|
|
ok($value)
|
|
|
|
|
|
|
|
proc decodeString*(_: type UInt256, value: string): Result[UInt256, cstring] =
|
|
|
|
try:
|
2023-06-21 05:46:18 +00:00
|
|
|
ok UInt256.fromDecimal(value)
|
2022-11-14 23:42:57 +00:00
|
|
|
except ValueError as e:
|
|
|
|
err e.msg.cstring
|
|
|
|
|
|
|
|
proc decodeString*(_: type array[32, byte],
|
|
|
|
value: string): Result[array[32, byte], cstring] =
|
|
|
|
try:
|
|
|
|
ok array[32, byte].fromHex(value)
|
|
|
|
except ValueError as e:
|
|
|
|
err e.msg.cstring
|
|
|
|
|
2024-03-21 10:53:45 +00:00
|
|
|
proc decodeString*[T: PurchaseId | RequestId | Nonce | SlotId | AvailabilityId](_: type T,
|
2022-11-14 23:42:57 +00:00
|
|
|
value: string): Result[T, cstring] =
|
|
|
|
array[32, byte].decodeString(value).map(id => T(id))
|
|
|
|
|
|
|
|
proc decodeString*(t: typedesc[string],
|
|
|
|
value: string): Result[string, cstring] =
|
|
|
|
ok(value)
|
|
|
|
|
|
|
|
proc encodeString*(value: string): RestResult[string] =
|
|
|
|
ok(value)
|