From 5353932fad33d11e88124e8f1376ac67b2ac895a Mon Sep 17 00:00:00 2001 From: Mc-Zen <52877387+Mc-Zen@users.noreply.github.com> Date: Tue, 17 Sep 2024 11:25:03 +0200 Subject: [PATCH] [refactor] replace type checks against strings with type checks against types (#11) e.g., `var == "string"` becomes `var == str` WIth this change, quill requires Typst 0.8.0 --- src/layout.typ | 9 ++++----- src/length-helpers.typ | 10 +++++----- src/process-args.typ | 10 +++++----- src/quantum-circuit.typ | 16 ++++++++-------- src/utility.typ | 6 +++--- src/verifications.typ | 4 ++-- typst.toml | 1 + 7 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/layout.typ b/src/layout.typ index cdc345d..49dc772 100644 --- a/src/layout.typ +++ b/src/layout.typ @@ -28,7 +28,6 @@ /// Take an alignment or 2d alignment and return a 2d alignment with the possibly /// unspecified axis set to a default value. #let make-2d-alignment(alignment, default-vertical: horizon, default-horizontal: center) = { - if type(alignment) == "2d alignment" { return alignment } let axis = alignment.axis() if axis == none { return alignment } if alignment.axis() == "horizontal" { return alignment + default-vertical } @@ -167,8 +166,8 @@ let cell-width = cell-sizes.at(integral, default: 0pt) return center-coords.at(integral, default: last) + cell-width * (fractional - 0.5) } - if type(cells) in ("integer", "float") { get(cells) } - else if type(cells) == "array" { cells.map(x => get(x)) } + if type(cells) in (int, float) { get(cells) } + else if type(cells) == array { cells.map(x => get(x)) } else { panic("Unsupported coordinate type") } } @@ -182,8 +181,8 @@ let c2 = center-coords.at(integral + 1, default: last) return c1 + (c2 - c1) * (fractional) } - if type(cells) in ("integer", "float") { get(cells) } - else if type(cells) == "array" { cells.map(x => get(x)) } + if type(cells) in (int, float) { get(cells) } + else if type(cells) == array { cells.map(x => get(x)) } else { panic("Unsupported coordinate type") } } diff --git a/src/length-helpers.typ b/src/length-helpers.typ index 8446ab1..26ed0fc 100644 --- a/src/length-helpers.typ +++ b/src/length-helpers.typ @@ -2,12 +2,12 @@ #let convert-em-length(length, em) = { if length.em == 0pt { return length } - if type(length.em) == "float" { return length.abs + length.em * em } + if type(length.em) == float { return length.abs + length.em * em } return length.abs + length.em / 1em * em } -#let get-length(length, container-length) = { - if type(length) == "length" { return length } - if type(length) == "ratio" { return length * container-length} - if type(length) == "relative length" { return length.length + length.ratio * container-length} +#let get-length(len, container-length) = { + if type(len) == length { return len } + if type(len) == ratio { return len * container-length} + if type(len) == relative { return len.length + len.ratio * container-length} } diff --git a/src/process-args.typ b/src/process-args.typ index 2b871e1..74f4288 100644 --- a/src/process-args.typ +++ b/src/process-args.typ @@ -7,10 +7,10 @@ #let process-padding-arg(padding) = { let type = type(padding) - if type == "length" { + if type == length { return (left: padding, top: padding, right: padding, bottom: padding) } - if type == "dictionary" { + if type == dictionary { let rest = padding.at("rest", default: 0pt) let x = padding.at("x", default: rest) let y = padding.at("y", default: rest) @@ -38,9 +38,9 @@ ) = { if labels == none { return () } let type = type(labels) - if type == "dictionary" { labels = (labels,) } - else if type in ("content", "string") { labels = ((content: labels),) } - else if type == "dictionary" { labels = ((content: labels),) } + if type == dictionary { labels = (labels,) } + else if type in (content, str) { labels = ((content: labels),) } + else if type == dictionary { labels = ((content: labels),) } let processed-labels = () for label in labels { let alignment = layout.make-2d-alignment(label.at("pos", default: default-pos)) diff --git a/src/quantum-circuit.typ b/src/quantum-circuit.typ index cdfc822..379ab27 100644 --- a/src/quantum-circuit.typ +++ b/src/quantum-circuit.typ @@ -92,7 +92,7 @@ draw-params.x-gate-size = layout.default-size-hint(gate($X$), draw-params) let items = children.pos().map( x => { - if type(x) in ("content", "string") and x != [\ ] { return gate(x) } + if type(x) in (content, str) and x != [\ ] { return gate(x) } return x }) @@ -324,14 +324,14 @@ let cols = layout.get-cell-coords(center-x-coords, col-widths, item.columns) let annotation = (item.callback)(cols, rows) verifications.verify-annotation-content(annotation) - if type(annotation) == "dictionary" { + if type(annotation) == dictionary { (content, decoration-bounds) = layout.place-with-labels( annotation.content, dx: annotation.at("dx", default: 0pt), dy: annotation.at("dy", default: 0pt), draw-params: draw-params ) - } else if type(annotation) in ("content", "string") { + } else if type(annotation) in (content, str) { layer-below-circuit += place(annotation) } } @@ -353,14 +353,14 @@ if offset == auto { return (dx - width / 2, dy - height / 2) } - assert(type(offset) == "dictionary", message: "Unexpected type `" + type(offset) + "` for parameter `offset`") + assert(type(offset) == dictionary, message: "Unexpected type `" + type(offset) + "` for parameter `offset`") let offset-x = offset.at("x", default: auto) let offset-y = offset.at("y", default: auto) if offset-x == auto { dx -= width / 2} - else if type(offset-x) == "length" { dx -= offset-x } + else if type(offset-x) == length { dx -= offset-x } if offset-y == auto { dy -= height / 2} - else if type(offset-y) == "length" { dy -= offset-y } + else if type(offset-y) == length { dy -= offset-y } return (dx, dy) } @@ -503,10 +503,10 @@ let final-width = scale * (bounds.at(2) - bounds.at(0)) let thebaseline = baseline - if type(thebaseline) in ("content", "string") { + if type(thebaseline) in (content, str) { thebaseline = height/2 - measure(thebaseline, styles).height/2 } - if type(thebaseline) == "fraction" { + if type(thebaseline) == fraction { thebaseline = 100% - layout.get-cell-coords1(center-y-coords, row-heights, thebaseline / 1fr) + bounds.at(1) } box(baseline: thebaseline, diff --git a/src/utility.typ b/src/utility.typ index 4bb4018..e563f5a 100644 --- a/src/utility.typ +++ b/src/utility.typ @@ -1,9 +1,9 @@ #let if-none(a, b) = { if a != none { a } else { b } } #let if-auto(a, b) = { if a != auto { a } else { b } } -#let is-gate(item) = { type(item) == "dictionary" and "gate-type" in item } -#let is-circuit-drawable(item) = { is-gate(item) or type(item) in ("string", "content") } -#let is-circuit-meta-instruction(item) = { type(item) == "dictionary" and "qc-instr" in item } +#let is-gate(item) = { type(item) == dictionary and "gate-type" in item } +#let is-circuit-drawable(item) = { is-gate(item) or type(item) in (str, content) } +#let is-circuit-meta-instruction(item) = { type(item) == dictionary and "qc-instr" in item } diff --git a/src/verifications.typ b/src/verifications.typ index 59c27ea..ac05a41 100644 --- a/src/verifications.typ +++ b/src/verifications.typ @@ -69,9 +69,9 @@ #let verify-annotation-content(annotation-content) = { let content-type = type(annotation-content) - assert(content-type in ("content", "string", "dictionary"), message: "`annotate`: Unsupported callback return type `" + str(content-type) + "` (can be `dictionary` or `content`") + assert(content-type in (content, str, dictionary), message: "`annotate`: Unsupported callback return type `" + str(content-type) + "` (can be `dictionary` or `content`") - if content-type == "dictionary" { + if content-type == dictionary { assert("content" in annotation-content, message: "`annotate`: Missing field `content` in annotation. If the callback returns a dictionary, it must contain the key `content` and may specify coordinates with `dx` and `dy`.") if "z" in annotation-content { let z = annotation-content.z diff --git a/typst.toml b/typst.toml index a07af8a..cafe6b2 100644 --- a/typst.toml +++ b/typst.toml @@ -5,6 +5,7 @@ entrypoint = "src/quill.typ" authors = ["Mc-Zen <https://github.com/Mc-Zen>"] license = "MIT" description = "Effortlessly create quantum circuit diagrams." +compiler = "0.8.0" repository = "https://github.com/Mc-Zen/quill" keywords = ["quantum circuit diagram", "quantikz", "qcircuit"]