added checks + fixed auto ids

This commit is contained in:
2025-04-19 00:10:57 +02:00
parent 358de4825d
commit 5616544707
2 changed files with 31 additions and 5 deletions

View File

@@ -11,26 +11,40 @@
#let circuit(body, length: 2em) = {
let next-id = 0
let elements = (:)
let ids = ()
for e in body {
if type(e) == dictionary and "id" in e {
ids.push(e.id)
}
}
for element in body {
let internal = type(element) == dictionary and "id" in element
let eid = if internal {element.id} else {none}
if eid == none {
while str(next-id) in elements {
let eid = if internal {element.id} else {auto}
if eid == auto {
while str(next-id) in ids {
next-id += 1
}
eid = str(next-id)
ids.push(eid)
if internal {
element.id = eid
}
next-id += 1
}
if eid in elements {
panic("An element with the id '" + eid + "' already exists. Please use a different id")
}
elements.insert(eid, element)
}
for element in elements.values() {
if type(element) == dictionary and "pre-process" in element {
elements = (element.pre-process)(elements, element)
assert(
type(elements) == dictionary,
message: "The `pre-process` method of element '" + element.id + "' did not return the elements dictionary"
)
}
}