diff --git a/src/circuit.typ b/src/circuit.typ index bdf6184..2af8565 100644 --- a/src/circuit.typ +++ b/src/circuit.typ @@ -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" + ) } } diff --git a/src/elements/element.typ b/src/elements/element.typ index 2dc0bb6..d83c4b5 100644 --- a/src/elements/element.typ +++ b/src/elements/element.typ @@ -147,7 +147,19 @@ // Workaround because CeTZ needs to have all draw functions in the body let func = {} - (func, bounds) = draw-shape(elmt, bounds) + let res = draw-shape(elmt, bounds) + assert( + type(res) == array and res.len() == 2, + message: "The drawing function of element '" + elmt.id + "' did not return a function and new bounds" + ) + (func, bounds) = res + if type(func) == function { + func = (func,) + } + assert( + type(bounds) == dictionary, + message: "The drawing function of element '" + elmt.id + "' did not return the correct bounds dictionary" + ) func if elmt.name != none { @@ -208,7 +220,7 @@ ports-margins: (:), fill: none, stroke: black + 1pt, - id: "", + id: auto, auto-ports: true, ports-y: (:), debug: (