From 980461c982b284cb511b9f544e25187c527516f3 Mon Sep 17 00:00:00 2001 From: Dustin Brody Date: Wed, 12 Jan 2022 11:48:41 +0000 Subject: [PATCH] use enum for ExecutePayloadResponse.status --- web3/conversions.nim | 24 +++++++++++++++++++++--- web3/engine_api_types.nim | 2 +- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/web3/conversions.nim b/web3/conversions.nim index 2e5a9d5..f2a6560 100644 --- a/web3/conversions.nim +++ b/web3/conversions.nim @@ -1,7 +1,8 @@ import - json, options, stint, strutils, strformat, typetraits, - stew/byteutils, json_serialization, faststreams/textio, - ethtypes, ethhexstrings + std/[json, options, strutils, strformat, tables, typetraits], + stint, stew/byteutils, json_serialization, faststreams/textio, + ethtypes, ethhexstrings, + ./engine_api_types from json_rpc/rpcserver import expect @@ -64,6 +65,23 @@ proc fromJson*(n: JsonNode, argName: string, result: var Quantity) {.inline.} = n.kind.expect(JString, argName) result = Quantity(parseHexInt(n.getStr)) +func getEnumStringTable(enumType: typedesc): Table[string, enumType] {.compileTime.} = + var res: Table[string, enumType] + # Not intended for enums with ordinal holes or repeated stringification + # strings. + for value in enumType: + res[$value] = value + res + +proc fromJson*(n: JsonNode, argName: string, result: var PayloadExecutionStatus) {.inline.} = + n.kind.expect(JString, argName) + const enumStrings = static: getEnumStringTable(type(result)) + try: + enumStrings[n.getStr] + except KeyError: + raise newException( + ValueError, "Parameter \"" & argName & "\" value invalid: " & n.getStr) + proc `%`*(v: Quantity): JsonNode = result = %encodeQuantity(v.uint64) diff --git a/web3/engine_api_types.nim b/web3/engine_api_types.nim index c5d5eae..2992c6b 100644 --- a/web3/engine_api_types.nim +++ b/web3/engine_api_types.nim @@ -22,7 +22,7 @@ type PayloadID* = FixedBytes[8] ExecutePayloadResponse* = object - status*: string + status*: PayloadExecutionStatus latestValidHash*: Option[BlockHash] message*: Option[string]