Rename to op
This commit is contained in:
@@ -6,8 +6,6 @@ description = "Generic result type for operations that can fail."
|
|||||||
license = "GPL-2.0"
|
license = "GPL-2.0"
|
||||||
srcDir = "src"
|
srcDir = "src"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Dependencies
|
# Dependencies
|
||||||
|
|
||||||
requires "nim >= 1.2.4"
|
requires "nim >= 1.2.4"
|
||||||
17
src/oi.nim
17
src/oi.nim
@@ -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
17
src/op.nim
Normal 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)
|
||||||
|
|
||||||
@@ -1,18 +1,18 @@
|
|||||||
# To run these tests, simply execute `nimble test`.
|
# To run these tests, simply execute `nimble test`.
|
||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
import oi
|
import op
|
||||||
|
|
||||||
test "Check OK":
|
test "Check OK":
|
||||||
let test = ok 1
|
let test = ok 1
|
||||||
check test.isOk == true
|
check test.isOk == true
|
||||||
|
|
||||||
test "Check fail":
|
test "Check fail":
|
||||||
let test = oi.fail[int] "no data here"
|
let test = op.fail[int] "no data here"
|
||||||
check test.isOk == false
|
check test.isOk == false
|
||||||
|
|
||||||
test "Check proc results":
|
test "Check proc results":
|
||||||
proc createValue: OI[string] =
|
proc createValue: OP[string] =
|
||||||
let myString = "This is test code!"
|
let myString = "This is test code!"
|
||||||
ok myString
|
ok myString
|
||||||
let data = createValue()
|
let data = createValue()
|
||||||
@@ -20,7 +20,7 @@ test "Check proc results":
|
|||||||
check data.val == "This is test code!"
|
check data.val == "This is test code!"
|
||||||
|
|
||||||
test "Check failing proc":
|
test "Check failing proc":
|
||||||
proc destinedToFail(): OI[int] =
|
proc destinedToFail(): OP[int] =
|
||||||
result.fail "no data found"
|
result.fail "no data found"
|
||||||
|
|
||||||
let data = destinedToFail()
|
let data = destinedToFail()
|
||||||
@@ -28,7 +28,7 @@ test "Check failing proc":
|
|||||||
check data.error == "no data found"
|
check data.error == "no data found"
|
||||||
|
|
||||||
test "Check changing result":
|
test "Check changing result":
|
||||||
proc checker(): OI[int] =
|
proc checker(): OP[int] =
|
||||||
result = ok 42
|
result = ok 42
|
||||||
# something happend here
|
# something happend here
|
||||||
result = result.fail "data got corrupted"
|
result = result.fail "data got corrupted"
|
||||||
|
|||||||
Reference in New Issue
Block a user