about summary refs log tree commit diff stats
path: root/src/utils/opt.nim
blob: 21d79af24ec7bac50a3a5b5d041cb80467c18db1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Inspired by nim-results.

type
  Result*[T, E] = object
    val: T
    has: bool
    when not (E is void):
      ex: E

  Opt*[T] = Result[T, void]

template ok*[T, E](t: type Result[T, E], x: T): Result[T, E] =
  Result[T, E](val: x, has: true)

template ok*[T](x: T): auto =
  ok(typeof(result), x)

template ok*[T, E](res: var Result[T, E], x: T): Result[T, E] =
  res.val = x
  res.has = true

template err*[T, E](t: type Result[T, E], e: E): Result[T, E] =
  Result[T, E](ex: e)

template err*[E](e: E): auto =
  err(typeof(result), e)

template err*[T, E](res: var Result[T, E], e: E) =
  res.ex = e

template isOk*(res: Result): bool = res.has
template isErr*(res: Result): bool = not res.has
template get*[T, E](res: Result[T, E]): T = res.val
template error*[T, E](res: Result[T, E]): E = res.ex