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