## Waku content topics definition and namespacing utils ## ## See 23/WAKU2-TOPICS RFC: https://rfc.vac.dev/spec/23/ when (NimMajor, NimMinor) < (1, 4): {.push raises: [Defect].} else: {.push raises: [].} import std/options, std/strutils, stew/results import ./parsing export parsing ## Content topic type ContentTopic* = string const DefaultContentTopic* = ContentTopic("/waku/2/default-content/proto") ## Namespaced content topic type NsContentTopic* = object generation*: Option[int] application*: string version*: string name*: string encoding*: string proc init*(T: type NsContentTopic, generation: Option[int], application: string, version: string, name: string, encoding: string): T = NsContentTopic( generation: generation, application: application, version: version, name: name, encoding: encoding ) # Serialization proc `$`*(topic: NsContentTopic): string = ## Returns a string representation of a namespaced topic ## in the format `////` ## Autosharding adds 1 optional prefix `/ var formatted = "" if topic.generation.isSome(): formatted = formatted & "/" & $topic.generation.get() formatted & "/" & topic.application & "/" & topic.version & "/" & topic.name & "/" & topic.encoding # Deserialization proc parse*(T: type NsContentTopic, topic: ContentTopic|string): ParsingResult[NsContentTopic] = ## Splits a namespaced topic string into its constituent parts. ## The topic string has to be in the format `////` ## Autosharding adds 1 optional prefix `/ if not topic.startsWith("/"): return err(ParsingError.invalidFormat("topic must start with slash")) let parts = topic[1..