about summary refs log tree commit diff stats
path: root/src/utils
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-05-16 13:17:41 +0200
committerbptato <nincsnevem662@gmail.com>2023-05-16 13:18:47 +0200
commit4e0fd8c7ef2ad2f61c1ac0572e02b92b1c42b688 (patch)
tree83adafc3a2046bb8af09d7c57340dc9374eebbd6 /src/utils
parent951d587f7edf3544d30ba039530a1d19b7e9db78 (diff)
downloadchawan-4e0fd8c7ef2ad2f61c1ac0572e02b92b1c42b688.tar.gz
Refactor config, add charset opts
Only document-charset supported for now.
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/opt.nim34
-rw-r--r--src/utils/twtstr.nim6
2 files changed, 40 insertions, 0 deletions
diff --git a/src/utils/opt.nim b/src/utils/opt.nim
new file mode 100644
index 00000000..21d79af2
--- /dev/null
+++ b/src/utils/opt.nim
@@ -0,0 +1,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
diff --git a/src/utils/twtstr.nim b/src/utils/twtstr.nim
index 027b26d1..d67e1ab7 100644
--- a/src/utils/twtstr.nim
+++ b/src/utils/twtstr.nim
@@ -116,6 +116,12 @@ func toScreamingSnakeCase*(str: string): string = # input is camel case
     else:
       result &= c.toUpperAscii()
 
+func snakeToKebabCase*(str: string): string =
+  result = str
+  for c in result.mitems:
+    if c == '_':
+      c = '-'
+
 func isAscii*(r: Rune): bool =
   return cast[uint32](r) < 128