mirror of
https://github.com/logos-storage/nim-datastore.git
synced 2026-01-02 21:53:05 +00:00
* adding monted store * misc spelling * adding mounted store tests to suite * split out key * relaxed key initialization * always mount and lookup by path * cleaned up and reorged tests * test lookup by path * add re-exports * more re-exports * fix warnings and re-exports
70 lines
1.7 KiB
Nim
70 lines
1.7 KiB
Nim
|
|
import std/hashes
|
|
import std/strformat
|
|
|
|
import pkg/questionable
|
|
import pkg/questionable/results
|
|
import pkg/upraises
|
|
|
|
push: {.upraises: [].}
|
|
|
|
const
|
|
Delimiter* = ":"
|
|
Separator* = "/"
|
|
|
|
type
|
|
Namespace* = object
|
|
field*: string
|
|
value*: string
|
|
|
|
func init*(T: type Namespace, field, value: string): ?!T =
|
|
if value.contains(Delimiter):
|
|
return failure (&"value string must not contain Delimiter '{Delimiter}'")
|
|
.catch.expect("should not fail")
|
|
|
|
if value.contains(Separator):
|
|
return failure (&"value string must not contain Separator {Separator}")
|
|
.catch.expect("should not fail")
|
|
|
|
if field.contains(Delimiter):
|
|
return failure (&"field string must not contain Delimiter {Delimiter}")
|
|
.catch.expect("should not fail")
|
|
|
|
if field.contains(Separator):
|
|
return failure (&"field string must not contain Separator {Separator}")
|
|
.catch.expect("should not fail")
|
|
|
|
success T(field: field, value: value)
|
|
|
|
func init*(T: type Namespace, id: string): ?!T =
|
|
if id.len > 0:
|
|
if id.contains(Separator):
|
|
return failure (&"id string must not contain Separator {Separator}")
|
|
.catch.expect("should not fail")
|
|
|
|
if id.count(Delimiter) > 1:
|
|
return failure (&"id string must not contain more than one {Delimiter}")
|
|
.catch.expect("should not fail")
|
|
|
|
let
|
|
(field, value) = block:
|
|
let parts = id.split(Delimiter)
|
|
if parts.len > 1:
|
|
(parts[0], parts[^1])
|
|
else:
|
|
("", parts[^1])
|
|
|
|
T.init(field.strip, value.strip)
|
|
|
|
func id*(self: Namespace): string =
|
|
if self.field.len > 0:
|
|
self.field & Delimiter & self.value
|
|
else:
|
|
self.value
|
|
|
|
func hash*(namespace: Namespace): Hash =
|
|
hash(namespace.id)
|
|
|
|
func `$`*(namespace: Namespace): string =
|
|
namespace.id
|