Provide conversion to string `$` for Results

This commit is contained in:
Mark Spanbroek 2022-01-12 10:49:30 +01:00 committed by markspanbroek
parent 58c10fb333
commit 0d7ce8efde
2 changed files with 10 additions and 0 deletions

View File

@ -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)

View File

@ -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