mirror of
https://github.com/logos-storage/questionable.git
synced 2026-01-13 11:13:09 +00:00
25 lines
608 B
Nim
25 lines
608 B
Nim
import std/options
|
|
|
|
var captures {.global, compileTime.}: int
|
|
var errorVariable: ptr ref CatchableError
|
|
|
|
template captureBindError*(error: var ref CatchableError, expression): auto =
|
|
let previousErrorVariable = errorVariable
|
|
errorVariable = addr error
|
|
|
|
static: inc captures
|
|
let evaluated = expression
|
|
static: dec captures
|
|
|
|
errorVariable = previousErrorVariable
|
|
|
|
evaluated
|
|
|
|
func error[T](option: Option[T]): ref CatchableError =
|
|
newException(ValueError, "Option is set to `none`")
|
|
|
|
template bindFailed*(expression) =
|
|
when captures > 0:
|
|
mixin error
|
|
errorVariable[] = expression.error
|