From 0976774b988a665184db05188cdbce7b50e955a2 Mon Sep 17 00:00:00 2001 From: Yu Vitaqua fer Chronos Date: Thu, 12 Oct 2023 22:43:02 +0100 Subject: [PATCH] Version bump, add version in docs --- nulid.nimble | 4 +++- src/nulid.nim | 19 ++++++++++++------- src/nulid/private/constants.nim | 4 ++++ src/nulid/private/dochelpers.nim | 8 ++++++++ tests/test1.nim | 4 +--- 5 files changed, 28 insertions(+), 11 deletions(-) create mode 100644 src/nulid/private/constants.nim create mode 100644 src/nulid/private/dochelpers.nim diff --git a/nulid.nimble b/nulid.nimble index 0a2efdd..8dffa6a 100644 --- a/nulid.nimble +++ b/nulid.nimble @@ -1,6 +1,8 @@ +import src/nulid/private/constants + # Package -version = "0.2.3" +version = NulidVersion author = "Yu Vitaqua fer Chronos" description = "An implementation of ULID!" license = "CC0" diff --git a/src/nulid.nim b/src/nulid.nim index 2332993..5670732 100644 --- a/src/nulid.nim +++ b/src/nulid.nim @@ -8,11 +8,16 @@ import pkg/[ nint128 ] +import ./nulid/private/[ + dochelpers, + constants +] + import ./nulid/private/stew/endians2 -const insecureRandom = defined(nulidInsecureRandom) or defined(js) # No sysrand on the JS backend +fmtCmnt "NULID Version: {NulidVersion}" -when insecureRandom: +when InsecureRandom: import std/random else: @@ -23,8 +28,8 @@ const HighUint80 = u128("1208925819614629174706176") type NULID* = object ## An object representing a ULID. - timestamp*: int64 = 0 - randomness*: UInt128 = 0.u128 + timestamp*: int64 + randomness*: UInt128 NULIDGenerator* = ref object ## A NULID generator object, contains details needed to follow the spec. @@ -33,14 +38,14 @@ type lastTime: int64 # Timestamp of last ULID random: UInt128 # A random number - when insecureRandom: + when InsecureRandom: rand: Rand # Random generator when using insecure random proc initNulidGenerator*(): NULIDGenerator = ## Initialises a `NULIDGenerator` for use. result = NULIDGenerator(lastTime: 0, random: 0.u128) - when insecureRandom: + when InsecureRandom: result.rand = initRand() # Discouraged to use it but it's fine for single-threaded apps really @@ -56,7 +61,7 @@ func toArray[T](oa: openArray[T], size: static Slice[int]): array[size.len, T] = proc randomBits(n: NULIDGenerator): UInt128 = var arr: array[16, byte] - when insecureRandom: + when InsecureRandom: var rnd: array[10, byte] rnd[0..7] = cast[array[8, byte]](n.rand.next()) diff --git a/src/nulid/private/constants.nim b/src/nulid/private/constants.nim new file mode 100644 index 0000000..26c80ee --- /dev/null +++ b/src/nulid/private/constants.nim @@ -0,0 +1,4 @@ +# No sysrand on the JS backend nor VM +const InsecureRandom* = defined(nulidInsecureRandom) or defined(js) or defined(nimvm) + +const NulidVersion* = "0.2.4" \ No newline at end of file diff --git a/src/nulid/private/dochelpers.nim b/src/nulid/private/dochelpers.nim new file mode 100644 index 0000000..d528eea --- /dev/null +++ b/src/nulid/private/dochelpers.nim @@ -0,0 +1,8 @@ +import std/[ + strformat, + macros +] + +macro ct(s: static string) = newCommentStmtNode(s) + +template fmtCmnt*(s: static string): untyped = ct(&s) \ No newline at end of file diff --git a/tests/test1.nim b/tests/test1.nim index 0e772e9..1333c58 100644 --- a/tests/test1.nim +++ b/tests/test1.nim @@ -11,11 +11,9 @@ import pkg/nint128 import nulid -let gen = initNulidGenerator() - test "NULID Generation": for _ in 0..5: - let nulid = gen.nulidSync() + let nulid = nulidSync() echo nulid test "NULID Parsing":