Rename to op

This commit is contained in:
2020-07-01 22:37:19 +02:00
parent 7884ca4a8f
commit 1783232b8e
4 changed files with 22 additions and 24 deletions

View File

@@ -6,8 +6,6 @@ description = "Generic result type for operations that can fail."
license = "GPL-2.0"
srcDir = "src"
# Dependencies
requires "nim >= 1.2.4"

View File

@@ -1,17 +0,0 @@
type
Err = string
OI*[T] = object of RootObj
case isOk*: bool
of true:
val*: T
of false:
error*: string
proc ok*[T](val: T): OI[T] =
OI[T](isOK: true, val: val)
proc fail*(oi: OI, msg: string): OI =
OI(isOK: false, error: msg)
proc fail*[T](msg: string): OI[T] =
OI[T](isOK: false, error: msg)

17
src/op.nim Normal file
View File

@@ -0,0 +1,17 @@
type
OP*[T] = object of RootObj
case isOk*: bool
of true:
val*: T
of false:
error*: string
proc ok*[T](val: T): OP[T] =
OP[T](isOK: true, val: val)
proc fail*(op: OP, msg: string): OP =
OP(isOK: false, error: msg)
proc fail*[T](msg: string): OP[T] =
OP[T](isOK: false, error: msg)

View File

@@ -1,18 +1,18 @@
# To run these tests, simply execute `nimble test`.
import unittest
import oi
import op
test "Check OK":
let test = ok 1
check test.isOk == true
test "Check fail":
let test = oi.fail[int] "no data here"
let test = op.fail[int] "no data here"
check test.isOk == false
test "Check proc results":
proc createValue: OI[string] =
proc createValue: OP[string] =
let myString = "This is test code!"
ok myString
let data = createValue()
@@ -20,7 +20,7 @@ test "Check proc results":
check data.val == "This is test code!"
test "Check failing proc":
proc destinedToFail(): OI[int] =
proc destinedToFail(): OP[int] =
result.fail "no data found"
let data = destinedToFail()
@@ -28,7 +28,7 @@ test "Check failing proc":
check data.error == "no data found"
test "Check changing result":
proc checker(): OI[int] =
proc checker(): OP[int] =
result = ok 42
# something happend here
result = result.fail "data got corrupted"