nim-codex/codex/utils/addrutils.nim
Jaremy Creechley e47b38af11
Improving proc/func formatting consistency (#454)
* Fixes/workarounds for nimsuggest failures in codex.nim.

* remove rng prefix - it appears to work now

* format new's to be more consistent

* making proc formatting a bit more consistent
2023-06-22 08:11:18 -07:00

42 lines
918 B
Nim

## 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 pkg/upraises
push: {.upraises: [].}
import std/strutils
import std/options
import pkg/libp2p
import pkg/stew/shims/net
func remapAddr*(
address: MultiAddress,
ip: Option[ValidIpAddress] = ValidIpAddress.none,
port: Option[Port] = Port.none
): MultiAddress =
## Remap addresses to new IP and/or Port
##
var
parts = ($address).split("/")
parts[2] = if ip.isSome:
$ip.get
else:
parts[2]
parts[4] = if port.isSome:
$port.get
else:
parts[4]
MultiAddress.init(parts.join("/"))
.expect("Should construct multiaddress")