Allow failure() to be called with string argument
This commit is contained in:
parent
21927f3c88
commit
74c5089d49
|
@ -6,6 +6,8 @@ include ./errorban
|
||||||
|
|
||||||
export resultsbase
|
export resultsbase
|
||||||
|
|
||||||
|
type ResultFailure* = object of CatchableError
|
||||||
|
|
||||||
template `?!`*(T: typed): type Result[T, ref CatchableError] =
|
template `?!`*(T: typed): type Result[T, ref CatchableError] =
|
||||||
Result[T, ref CatchableError]
|
Result[T, ref CatchableError]
|
||||||
|
|
||||||
|
@ -15,6 +17,9 @@ proc success*[T](value: T): ?!T =
|
||||||
proc failure*(T: type, error: ref CatchableError): ?!T =
|
proc failure*(T: type, error: ref CatchableError): ?!T =
|
||||||
err(?!T, error)
|
err(?!T, error)
|
||||||
|
|
||||||
|
proc failure*(T: type, message: string): ?!T =
|
||||||
|
T.failure newException(ResultFailure, message)
|
||||||
|
|
||||||
template `->?`*[T,U](value: ?!T, expression: U): ?!U =
|
template `->?`*[T,U](value: ?!T, expression: U): ?!U =
|
||||||
if value.isErr:
|
if value.isErr:
|
||||||
U.failure(value.error)
|
U.failure(value.error)
|
||||||
|
|
|
@ -82,6 +82,11 @@ suite "result":
|
||||||
check parseInt("42").catch == 42.success
|
check parseInt("42").catch == 42.success
|
||||||
check parseInt("foo").catch.error of ValueError
|
check parseInt("foo").catch.error of ValueError
|
||||||
|
|
||||||
|
test "failure can be called with string argument":
|
||||||
|
let value = int.failure("some failure")
|
||||||
|
check value.error of ResultFailure
|
||||||
|
check value.error.msg == "some failure"
|
||||||
|
|
||||||
test "unary operator `-` works for results":
|
test "unary operator `-` works for results":
|
||||||
check -(-42.success) == 42.success
|
check -(-42.success) == 42.success
|
||||||
check -(int.failure(error)) == int.failure(error)
|
check -(int.failure(error)) == int.failure(error)
|
||||||
|
|
Loading…
Reference in New Issue