Skip to content

Commit

Permalink
support IpAddress in contexts ValidIpAddress is supported (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
tersec committed Oct 27, 2023
1 parent 07b598f commit 7568f1b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
6 changes: 6 additions & 0 deletions confutils/toml/std/net.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import
export
net, toml_serialization

proc readValue*(r: var TomlReader, val: var IpAddress)
{.raises: [SerializationError, IOError, Defect].} =
val = try: parseIpAddress(r.readValue(string))
except ValueError as err:
r.lex.raiseUnexpectedValue("IP address")

proc readValue*(r: var TomlReader, val: var ValidIpAddress)
{.raises: [SerializationError, IOError, Defect].} =
val = try: ValidIpAddress.init(r.readValue(string))
Expand Down
20 changes: 17 additions & 3 deletions tests/test_config_file.nim
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,15 @@ type
name: "rpc-port" }: Port

rpcAddress* {.
defaultValue: defaultAdminListenAddress(config)
defaultValue: ValidIpAddress.init(defaultAdminListenAddress(config))
desc: "Address of the server to connect to for RPC - for the validator duties in the pull model"
name: "rpc-address" }: ValidIpAddress

restAddress* {.
defaultValue: defaultAdminListenAddress(config)
desc: "Address of the server to connect to for RPC - for the validator duties in the pull model"
name: "rest-address" }: IpAddress

retryDelay* {.
defaultValue: 10
desc: "Delay in seconds between retries after unsuccessful attempts to connect to a beacon node"
Expand Down Expand Up @@ -127,8 +132,8 @@ func parseCmdArg*(T: type GraffitiBytes, input: string): T
func completeCmdArg*(T: type GraffitiBytes, input: string): seq[string] =
@[]

func defaultAdminListenAddress*(conf: TestConf): ValidIpAddress =
(static ValidIpAddress.init("127.0.0.1"))
func defaultAdminListenAddress*(conf: TestConf): IpAddress =
(static parseIpAddress("127.0.0.1"))

const
defaultEth2TcpPort* = 9000
Expand All @@ -148,6 +153,12 @@ proc readValue(r: var TomlReader,
type T = type value
value = T r.parseAsString()

proc readValue(r: var TomlReader, value: var IpAddress) =
try:
value = parseIpAddress(r.parseAsString())
except ValueError as ex:
raise newException(SerializationError, ex.msg)

proc readValue(r: var TomlReader, value: var ValidIpAddress) =
try:
value = ValidIpAddress.init(r.parseAsString())
Expand All @@ -168,6 +179,9 @@ proc readValue(r: var WinregReader,
type T = type value
value = r.readValue(string).T

proc readValue(r: var WinregReader, value: var IpAddress) {.used.} =
value = parseIpAddress(r.readValue(string))

proc readValue(r: var WinregReader, value: var ValidIpAddress) {.used.} =
value = ValidIpAddress.init(r.readValue(string))

Expand Down

0 comments on commit 7568f1b

Please sign in to comment.