nwaku/waku/v2/utils/namespacing.nim
Sanaz Taheri Boshrooyeh a41b9ac80b
Refactor waku protocols and utils (#667)
* refactors waku_filter

* refactors waku_lightpush

* refactors waku_swap

* refactor namespacing.nim

* refactor peers

* refactors requests

* adds top level {.push raises: [Defect].}

* log scope for rln relay

* cleans up comments

* removes comments

* comments out raise Defect

* defines temp var then pass to constructor

* Update waku/v2/protocol/waku_rln_relay/rln.nim

Co-authored-by: oskarth <ot@oskarthoren.com>

* Update waku/v2/protocol/waku_swap/waku_swap.nim

Co-authored-by: oskarth <ot@oskarthoren.com>

* explains the potential exception in waku_swap

* creates temp var before return

* adjusts spaces

* adds line breaks, temp vars and fixes format

* removes type declaration

* fixes and indentation issue

* adjusts spacing

* adjusts line <80ch

* formating improvement

Co-authored-by: oskarth <ot@oskarthoren.com>
2021-07-15 11:25:52 -07:00

57 lines
1.6 KiB
Nim

## Collection of utilities related to namespaced topics
## Implemented according to the specified Waku v2 Topic Usage Recommendations
## More at https://rfc.vac.dev/spec/23/
{.push raises: [Defect]}
import
std/strutils,
stew/results
type
NamespacedTopic* = object
application*: string
version*: string
topicName*: string
encoding*: string
NamespacingResult*[T] = Result[T, string]
proc fromString*(T: type NamespacedTopic, topic: string): NamespacingResult[NamespacedTopic] =
## Splits a namespaced topic string into its constituent parts.
## The topic string has to be in the format `/<application>/<version>/<topic-name>/<encoding>`
let parts = topic.split('/')
if parts.len != 5:
# Check that we have an expected number of substrings
return err("invalid topic format")
if parts[0] != "":
# Ensures that topic starts with a "/"
return err("invalid topic format")
let namespacedTopic= NamespacedTopic(application: parts[1],
version: parts[2],
topicName: parts[3],
encoding: parts[4])
return ok(namespacedTopic)
proc `$`*(namespacedTopic: NamespacedTopic): string =
## Returns a string representation of a namespaced topic
## in the format `/<application>/<version>/<topic-name>/<encoding>`
var topicStr = newString(0)
topicStr.add("/")
topicStr.add(namespacedTopic.application)
topicStr.add("/")
topicStr.add(namespacedTopic.version)
topicStr.add("/")
topicStr.add(namespacedTopic.topicName)
topicStr.add("/")
topicStr.add(namespacedTopic.encoding)
return topicStr