From 1783232b8ef0e1126ec43e5e27f33aac53cb5733 Mon Sep 17 00:00:00 2001 From: luxick Date: Wed, 1 Jul 2020 22:37:19 +0200 Subject: [PATCH] Rename to op --- oi.nimble => op.nimble | 2 -- src/oi.nim | 17 ----------------- src/op.nim | 17 +++++++++++++++++ tests/testResults.nim | 10 +++++----- 4 files changed, 22 insertions(+), 24 deletions(-) rename oi.nimble => op.nimble (99%) delete mode 100644 src/oi.nim create mode 100644 src/op.nim diff --git a/oi.nimble b/op.nimble similarity index 99% rename from oi.nimble rename to op.nimble index 490539a..dc3e40e 100644 --- a/oi.nimble +++ b/op.nimble @@ -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" diff --git a/src/oi.nim b/src/oi.nim deleted file mode 100644 index 0e7539d..0000000 --- a/src/oi.nim +++ /dev/null @@ -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) diff --git a/src/op.nim b/src/op.nim new file mode 100644 index 0000000..c6699ea --- /dev/null +++ b/src/op.nim @@ -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) + diff --git a/tests/testResults.nim b/tests/testResults.nim index 98c7334..3f4537e 100644 --- a/tests/testResults.nim +++ b/tests/testResults.nim @@ -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"