mirror of
https://github.com/logos-storage/questionable.git
synced 2026-02-17 04:13:27 +00:00
Avoid wrapping option in option and result in result
This commit is contained in:
parent
d82581244c
commit
fa56587bcd
@ -21,12 +21,24 @@ template `->?`*[T,U](option: ?T, expression: U): ?U =
|
||||
else:
|
||||
U.none
|
||||
|
||||
template `->?`*[T,U](option: ?T, expression: ?U): ?U =
|
||||
if option.isSome:
|
||||
expression
|
||||
else:
|
||||
U.none
|
||||
|
||||
template `->?`*[T,U,V](options: (?T, ?U), expression: V): ?V =
|
||||
if options[0].isSome and options[1].isSome:
|
||||
expression.some
|
||||
else:
|
||||
V.none
|
||||
|
||||
template `->?`*[T,U,V](options: (?T, ?U), expression: ?V): ?V =
|
||||
if options[0].isSome and options[1].isSome:
|
||||
expression
|
||||
else:
|
||||
V.none
|
||||
|
||||
template `=?`*[T](name: untyped{nkIdent}, expression: ?T): bool =
|
||||
let option = expression
|
||||
template name: T {.used.} = option.unsafeGet()
|
||||
|
||||
@ -48,6 +48,12 @@ template `->?`*[T,U](value: ?!T, expression: U): ?!U =
|
||||
else:
|
||||
expression.success
|
||||
|
||||
template `->?`*[T,U](value: ?!T, expression: ?!U): ?!U =
|
||||
if value.isFailure:
|
||||
U.failure(value.error)
|
||||
else:
|
||||
expression
|
||||
|
||||
template `->?`*[T,U,V](values: (?!T, ?!U), expression: V): ?!V =
|
||||
if values[0].isFailure:
|
||||
V.failure(values[0].error)
|
||||
@ -56,6 +62,14 @@ template `->?`*[T,U,V](values: (?!T, ?!U), expression: V): ?!V =
|
||||
else:
|
||||
expression.success
|
||||
|
||||
template `->?`*[T,U,V](values: (?!T, ?!U), expression: ?!V): ?!V =
|
||||
if values[0].isFailure:
|
||||
V.failure(values[0].error)
|
||||
elif values[1].isFailure:
|
||||
V.failure(values[1].error)
|
||||
else:
|
||||
expression
|
||||
|
||||
template `|?`*[T,E](value: Result[T,E], fallback: T): T =
|
||||
value.valueOr(fallback)
|
||||
|
||||
|
||||
@ -192,6 +192,23 @@ suite "optionals":
|
||||
check 40.some >= 42 == false.some
|
||||
check 40.some > 42 == false.some
|
||||
|
||||
test ".? avoids wrapping option in option":
|
||||
let a = 41.some
|
||||
|
||||
proc b(x: int): ?int =
|
||||
some x + 1
|
||||
|
||||
check a.?b == 42.some
|
||||
|
||||
test "lifted operators avoid wrapping option in option":
|
||||
let a = 40.some
|
||||
let b = 2.some
|
||||
|
||||
func `&`(x, y: int): ?int =
|
||||
some x + y
|
||||
|
||||
check (a & b) == 42.some
|
||||
|
||||
test "examples from readme work":
|
||||
|
||||
var x: ?int
|
||||
|
||||
@ -175,6 +175,23 @@ suite "result":
|
||||
check fails().isFailure
|
||||
check fails().error.msg == "some error"
|
||||
|
||||
test ".? avoids wrapping result in result":
|
||||
let a = 41.success
|
||||
|
||||
proc b(x: int): ?!int =
|
||||
success x + 1
|
||||
|
||||
check a.?b == 42.success
|
||||
|
||||
test "lifted operators avoid wrapping result in result":
|
||||
let a = 40.success
|
||||
let b = 2.success
|
||||
|
||||
func `&`(x, y: int): ?!int =
|
||||
success x + y
|
||||
|
||||
check (a & b) == 42.success
|
||||
|
||||
test "examples from readme work":
|
||||
|
||||
proc works: ?!seq[int] =
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user