chore(PeerTable): add stringification operator (#1567)

This commit is contained in:
vladopajic
2025-07-23 12:53:26 +02:00
committed by GitHub
parent e71c7caf82
commit 7bdba4909f
2 changed files with 16 additions and 1 deletions

View File

@@ -9,13 +9,25 @@
{.push raises: [].}
import std/[tables, sets, sequtils]
import std/[tables, sets, sequtils, strutils]
import ./pubsubpeer, ../../peerid
export tables, sets
type PeerTable* = Table[string, HashSet[PubSubPeer]] # topic string to peer map
proc `$`*(table: PeerTable): string =
result.add("PeerTable ")
result.add("topics (" & $table.len & ")")
for topic, peers in table:
result.add(" topic: ")
result.add($topic)
result.add(" peers: ")
result.add("(" & $peers.len & ") [")
result.add(peers.mapIt($it).join(", "))
result.add("]")
proc hasPeerId*(t: PeerTable, topic: string, peerId: PeerId): bool =
if topic in t:
try:

View File

@@ -172,6 +172,9 @@ proc getAgent*(peer: PubSubPeer): string =
else:
"unknown"
proc `$`*(p: PubSubPeer): string =
$p.peerId
func hash*(p: PubSubPeer): Hash =
p.peerId.hash