Provide conversion to string `$` for Results
This commit is contained in:
parent
58c10fb333
commit
0d7ce8efde
|
@ -73,6 +73,12 @@ proc isFailure*[T](value: ?!T): bool =
|
|||
|
||||
value.isErr
|
||||
|
||||
proc `$`*[T](value: ?!T): string =
|
||||
if value.isSuccess:
|
||||
"success(" & $(!value) & ")"
|
||||
else:
|
||||
"failure(\"" & $(value.error.msg) & "\")"
|
||||
|
||||
template `->?`*[T,U](value: ?!T, expression: ?!U): ?!U =
|
||||
if value.isFailure:
|
||||
U.failure(value.error)
|
||||
|
|
|
@ -14,6 +14,10 @@ suite "result":
|
|||
check (?!string is Result[string, ref CatchableError])
|
||||
check (?!seq[bool] is Result[seq[bool], ref CatchableError])
|
||||
|
||||
test "conversion to string $ works for ?!Types":
|
||||
check $42.success == "success(42)"
|
||||
check $(int.failure "some error") == "failure(\"some error\")"
|
||||
|
||||
test "! gets value or raises Defect":
|
||||
check !42.success == 42
|
||||
expect Defect: discard !int.failure error
|
||||
|
|
Loading…
Reference in New Issue