Skip to content

Commit

Permalink
2.1.2: fix another symbol hack
Browse files Browse the repository at this point in the history
  • Loading branch information
disruptek committed Oct 3, 2021
1 parent 9465fa4 commit 8f5f214
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
12 changes: 8 additions & 4 deletions badresults.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,26 @@ type
of true:
v: T

func newResultError[E](e: E; s: string): ResultError[E] {.inline, nimcall.} =
## capturing ResultError...
ResultError[E](error: e, msg: s)

macro toException*[E](err: E): ResultError[E] =
err.expectKind nnkCheckedFieldExpr
# err is `self.e`, a checked field expr
let e = err[0] # unwrap checked-field to get dot expr
let T = getTypeImpl e[1] # unwrap dot expr to get the error type
let re = bindSym"newResultError"
quote:
when compiles($`e`):
ResultError[`T`](error: `e`, msg: "Result isErr: " & $`e`)
`re`(`e`, "Result isErr: " & $`e`)
else:
ResultError[`T`](error: `e`)
`re`(`e`, "Result isErr; no `$` in scope.")

macro raiseResultError[T, E](self: Result[T, E]): untyped =
quote:
when `E` is ref Exception:
if `self`.e.isNil: # for example Result.default()!
raise ResultError[void](msg: "Result isErr: (no exception)")
raise ResultError[void](msg: "Result isErr; no exception.")
else:
raise `self`.e
else:
Expand Down
2 changes: 1 addition & 1 deletion badresults.nimble
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = "2.1.1"
version = "2.1.2"
author = "disruptek"
description = "a simpler and less fascist fork of nim-result"
license = "MIT"
Expand Down

0 comments on commit 8f5f214

Please sign in to comment.