mirror of
https://github.com/logos-storage/questionable.git
synced 2026-01-16 12:43:11 +00:00
Cleanup
This commit is contained in:
parent
8fbf39a60f
commit
f4ada730b8
@ -1,16 +1,12 @@
|
|||||||
import std/options
|
import std/options
|
||||||
import std/macros
|
import std/macros
|
||||||
|
|
||||||
proc option[T](option: Option[T]): Option[T] =
|
template questionableUnpack*(expression: Option): untyped =
|
||||||
option
|
## Used internally
|
||||||
|
|
||||||
proc placeholder(T: type): T =
|
|
||||||
discard
|
|
||||||
|
|
||||||
template unpack*(expression: Option): untyped =
|
|
||||||
let option = expression
|
let option = expression
|
||||||
type T = typeof(option.unsafeGet())
|
type T = typeof(option.unsafeGet())
|
||||||
let res = if option.isSome: option.unsafeGet() else: placeholder(T)
|
let res = if option.isSome: option.unsafeGet() else: default(T)
|
||||||
(res, option.isSome)
|
(res, option.isSome)
|
||||||
|
|
||||||
macro `=?`*(name, expression): bool =
|
macro `=?`*(name, expression): bool =
|
||||||
@ -21,13 +17,13 @@ macro `=?`*(name, expression): bool =
|
|||||||
name.expectKind({nnkIdent, nnkVarTy})
|
name.expectKind({nnkIdent, nnkVarTy})
|
||||||
if name.kind == nnkIdent:
|
if name.kind == nnkIdent:
|
||||||
quote do:
|
quote do:
|
||||||
mixin unpack
|
mixin questionableUnpack
|
||||||
let (`name`, isOk) = unpack(`expression`)
|
let (`name`, isOk) = questionableUnpack(`expression`)
|
||||||
isOk
|
isOk
|
||||||
|
|
||||||
else:
|
else:
|
||||||
let name = name[0]
|
let name = name[0]
|
||||||
quote do:
|
quote do:
|
||||||
mixin unpack
|
mixin questionableUnpack
|
||||||
var (`name`, isOk) = unpack(`expression`)
|
var (`name`, isOk) = questionableUnpack(`expression`)
|
||||||
isOk
|
isOk
|
||||||
|
|||||||
@ -99,13 +99,6 @@ template `|?`*[T,E](value: Result[T,E], fallback: T): T =
|
|||||||
|
|
||||||
value.valueOr(fallback)
|
value.valueOr(fallback)
|
||||||
|
|
||||||
template unpack*(expression: Result): untyped =
|
|
||||||
let res = expression
|
|
||||||
when declared(internalWithoutError):
|
|
||||||
if res.isFailure:
|
|
||||||
internalWithoutError = res.error
|
|
||||||
unpack(res.option)
|
|
||||||
|
|
||||||
proc option*[T,E](value: Result[T,E]): ?T =
|
proc option*[T,E](value: Result[T,E]): ?T =
|
||||||
## Converts a Result into an Option.
|
## Converts a Result into an Option.
|
||||||
|
|
||||||
@ -117,6 +110,15 @@ proc option*[T,E](value: Result[T,E]): ?T =
|
|||||||
else:
|
else:
|
||||||
T.none
|
T.none
|
||||||
|
|
||||||
|
template questionableUnpack*(expression: Result): untyped =
|
||||||
|
## Used internally
|
||||||
|
|
||||||
|
let res = expression
|
||||||
|
when declared(internalWithoutError):
|
||||||
|
if res.isFailure:
|
||||||
|
internalWithoutError = res.error
|
||||||
|
questionableUnpack(res.option)
|
||||||
|
|
||||||
Result.liftUnary(`-`)
|
Result.liftUnary(`-`)
|
||||||
Result.liftUnary(`+`)
|
Result.liftUnary(`+`)
|
||||||
Result.liftUnary(`@`)
|
Result.liftUnary(`@`)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user