diff options
Diffstat (limited to 'src/utils/twtstr.nim')
-rw-r--r-- | src/utils/twtstr.nim | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/src/utils/twtstr.nim b/src/utils/twtstr.nim index 80b6be65..1916fff5 100644 --- a/src/utils/twtstr.nim +++ b/src/utils/twtstr.nim @@ -713,3 +713,19 @@ proc makeCRLF*(s: string): string = result &= '\n' else: result &= s[i] + +func strictParseEnum*[T: enum](s: string): Opt[T] = + # cmp when len is small enough, otherwise hashmap + when {T.low..T.high}.len <= 4: + for e in T.low .. T.high: + if $e == s: + return ok(e) + else: + const tab = (func(): Table[string, T] = + result = initTable[string, T]() + for e in T.low .. T.high: + result[$e] = e + )() + if s in tab: + return ok(tab[s]) + return err() |