From b4756b13ae59700df157315e74fca8b582399bcb Mon Sep 17 00:00:00 2001 From: Emery Hemingway Date: Thu, 27 Oct 2022 15:29:45 -0500 Subject: [PATCH] isRecord: add variant with a label --- preserves.nimble | 2 +- src/preserves.nim | 22 ++++++++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/preserves.nimble b/preserves.nimble index 4a04bd4..9e42c43 100644 --- a/preserves.nimble +++ b/preserves.nimble @@ -1,6 +1,6 @@ # Package -version = "20221025" +version = "20221027" author = "Emery Hemingway" description = "data model and serialization format" license = "Unlicense" diff --git a/src/preserves.nim b/src/preserves.nim index c8aff5e..66ab8d7 100644 --- a/src/preserves.nim +++ b/src/preserves.nim @@ -391,9 +391,23 @@ func isSymbol*(pr: Preserve; sym: string|Symbol): bool {.inline.} = ## Check if ``pr`` is a Preserves symbol of ``sym``. (pr.kind == pkSymbol) and (pr.symbol == Symbol(sym)) +proc label*(pr: Preserve): Preserve {.inline.} = + ## Return the label of record value. + pr.record[pr.record.high] + +proc arity*(pr: Preserve): int {.inline.} = + ## Return the number of fields in record `pr`. + pred(pr.record.len) + func isRecord*(pr: Preserve): bool {.inline.} = (pr.kind == pkRecord) and (pr.record.len > 0) ## Check if ``pr`` is a Preserves record. +func isRecord*(pr: Preserve; label: string): bool {.inline.} = + ## Check if ``pr`` is a Preserves record with the given label symbol. + pr.kind == pkRecord and + pr.record.len > 0 and + pr.label.isSymbol(label) + proc isSequence*(pr: Preserve): bool {.inline.} = pr.kind == pkSequence ## Check if ``pr`` is a Preserves sequence. @@ -408,14 +422,6 @@ func isEmbedded*[E](pr: Preserve[E]): bool {.inline.} = when E is void: pr.embedded # embedded Preserves else: pr.kind == pkEmbedded # embedded Nim -proc label*(pr: Preserve): Preserve {.inline.} = - ## Return the label of record value. - pr.record[pr.record.high] - -proc arity*(pr: Preserve): int {.inline.} = - ## Return the number of fields in record `pr`. - pred(pr.record.len) - proc fields*(pr: Preserve): seq[Preserve] {.inline.} = ## Return the fields of a record value. pr.record[0..pr.record.high.pred]