2021-06-18 10:30:24 +00:00
|
|
|
## 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")
|
|
|
|
|
2021-07-15 18:25:52 +00:00
|
|
|
let namespacedTopic= NamespacedTopic(application: parts[1],
|
2021-06-18 10:30:24 +00:00
|
|
|
version: parts[2],
|
|
|
|
topicName: parts[3],
|
2021-07-15 18:25:52 +00:00
|
|
|
encoding: parts[4])
|
|
|
|
|
|
|
|
return ok(namespacedTopic)
|
2021-06-18 10:30:24 +00:00
|
|
|
|
|
|
|
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
|