Commit Graph

23 Commits

Author SHA1 Message Date
Jacek Sieka 6c97f11c7c
results: work around nim codegen bug (#192)
https://github.com/nim-lang/Nim/issues/22049
2023-06-08 17:24:19 +02:00
Jacek Sieka 13e55ed27a
results: collections integration (#179)
This set of helpers allows treating Result and Opt as collections of 0
or 1 item, allowing iterating over them and checking "membership" - such
integration is useful in generic code which can then be generalised to
handle more complex cases - the integration is most useful with Opt.

One design tradeoff here is the "explicitness" of `items` vs `values`
for `Result` - technically error and value are "equal" and therefore we
shouldn't give preference to the value, but there exists a convenience
argument to treat the value as the "default" and therefore define
`items` / `contains` for `Result` as well - this PR chooses the more
conservative and explicit approach - a more liberal version can easily
be added later should motivating examples emerge.
2023-06-07 16:45:53 +02:00
Jacek Sieka 266e9002f3
results: Add Opt/Result converters (#177)
Add `optError`, `optValue`, to convert back and forth between Opt and Result
These conversions end up being more common than others since "trivial"  success/fail-style API often use Opt while combining such API into combined operations tends to prefer richer error reporting.
2023-05-11 16:34:41 +03:00
Jacek Sieka 9b985e8ea8
results: Add `isOkOr`, `isErrOr` (#176)
These two helpers complete `valueOr` and `errorOr` to cover `void` cases
where no value should be returned or `Result[void, E]` /
`Result[T, void]` is being used - they can be used for a convient
early-return style in side-effectful proc:s:

```nim
v.update().isOkOr:
  echo "update failed: ", error
```
2023-04-20 13:08:54 +02:00
jangko faabd40c30
reduce compiler warnings 2023-02-14 21:35:54 +07:00
Jacek Sieka ac602b5086
results: print `none` for empty Opt instances (#164)
* results: print `none` for empty Opt instances

* fix test too
2023-01-16 09:01:31 +01:00
Jacek Sieka 8a1b6ab4dc
results: work around void member codegen issue (#150) 2022-11-11 14:26:59 +01:00
tersec f173efc500
stylecheck: nnkArglist -> nnkArgList (#127)
* stylecheck: nnkArglist -> nnkArgList

* --stylecheck:error iff >= 1.6.0
2022-07-18 11:02:40 +00:00
Jacek Sieka dad28a269f
result: add `Opt.some` / `Opt.none` (#117) 2022-06-17 13:45:22 +03:00
Jacek Sieka 9d0f1167ca
results: `[]` for void (#103)
mostly for consistency.. but it's used in nim-eth tests
2022-01-13 22:02:44 +01:00
Jacek Sieka 6ad35b876f result: expose value/error in errorOr/valueOr
* fix error type as well
2022-01-11 16:39:12 +02:00
Jacek Sieka d2ae2889e8
`Result` refresh (#96)
* `Result` refresh

* add full support for `Result[T, void]` (aka `Opt[T]` aka `Option[T]`)
* expand tests
* add `flatten`, `filter` of `Option` fame
* add `tryError` that raises a regular exception when result holds a
value
* fix `$` to print `ok`/`err` in lower-case, like the functions that
created the result
* add `orErr` that collapses all errors to a single value of a
potentially different type - useful when translating errors between
layers
* `capture` should work with `CatchableError`
* remove `Defect`-dependent tests

* Update stew/results.nim

* avoid redundant error message when converting error to string
* avoid multiple evaluation in `valueOr`
* add `unsafeError` to match `unsafeGet`
* let `valueOr` evaluate a block
* add `errorOr` to mirror `valueOr`
2022-01-10 00:22:09 +02:00
Jacek Sieka 3c91b8694e
add void tests (#84) 2021-07-07 10:12:20 +02:00
Mamy Ratsimbazafy 6d3e6a21ca
results.`==` with void, Reboot #73 addressing comments (#74) 2021-01-26 15:06:17 +01:00
Mamy Ratsimbazafy b4b3841a85
Revert "Allow comparing Result[void, E] (#71)" (#73)
This reverts commit 068412ff4e.
2021-01-26 14:20:09 +01:00
Mamy Ratsimbazafy 068412ff4e
Allow comparing Result[void, E] (#71) 2021-01-26 14:12:08 +01:00
Jacek Sieka e15c1ae012
results: prevent dangling cstring pointers in result (#63) 2020-12-09 17:21:12 +01:00
Jacek Sieka ec2f52b0ce
results: make `?` work with void (#50) 2020-07-13 18:09:45 +02:00
Jacek Sieka f125f6e8d1 space 2020-05-08 18:54:36 +03:00
Jacek Sieka 2d9226464d heterogenous `or`
Useful for translating `error` results
2020-05-08 18:54:36 +03:00
Jacek Sieka ff755bbf75
change get/[] to always raise Defect and tryGet to do eh bridge mode (#30)
* also sprinkle mixin randomly across the codebase
2020-04-16 18:23:12 +02:00
Jacek Sieka 805ef4f1b2
result: cleanups
* fix defect raising without `$`
* doc updates
* better `?` that doesn't cause raises effect (it should, because of
FieldError but...)
2020-04-11 16:22:37 +02:00
Jacek Sieka b06a5b6e32
result -> results (#27) 2020-04-07 11:43:07 +02:00