logos-messaging-nim/waku/factory/conf_builder/rate_limit_conf_builder.nim
fryorcraken 4e527ee045 chore: use type for rate limit config (#3489)
* chore: use type for rate limit config

Use type instead of `seq[string]` for rate limit config earlier.
Enables to fail faster (at config time) if the string is malformated

Also enables using object in some scenarios.

* test: remove import warnings

* improve naming and add tests
2025-07-09 15:57:38 +10:00

30 lines
949 B
Nim

import chronicles, std/[net, options], results
import waku/common/rate_limit/setting
logScope:
topics = "waku conf builder rate limit"
type RateLimitConfBuilder* = object
strValue: Option[seq[string]]
objValue: Option[ProtocolRateLimitSettings]
proc init*(T: type RateLimitConfBuilder): RateLimitConfBuilder =
RateLimitConfBuilder()
proc withRateLimits*(b: var RateLimitConfBuilder, rateLimits: seq[string]) =
b.strValue = some(rateLimits)
proc build*(b: RateLimitConfBuilder): Result[ProtocolRateLimitSettings, string] =
if b.strValue.isSome() and b.objValue.isSome():
return err("Rate limits conf must only be set once on the builder")
if b.objValue.isSome():
return ok(b.objValue.get())
if b.strValue.isSome():
let rateLimits = ProtocolRateLimitSettings.parse(b.strValue.get()).valueOr:
return err("Invalid rate limits settings:" & $error)
return ok(rateLimits)
return ok(DefaultProtocolRateLimit)