Skip to content

Commit

Permalink
use element.declare
Browse files Browse the repository at this point in the history
  • Loading branch information
PgBiel committed Jan 8, 2025
1 parent 60b2ee7 commit b1d153b
Show file tree
Hide file tree
Showing 71 changed files with 163 additions and 159 deletions.
21 changes: 11 additions & 10 deletions src/element.typ
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,7 @@
}

// Create an element with the given name and constructor.
#let element(
#let declare(
name,
display: none,
fields: none,
Expand All @@ -1051,14 +1051,15 @@
synthesize: none,
contextual: auto,
) = {
assert(type(display) == function, message: "element: please specify a show rule in 'display:' to determine how your element is displayed.")
assert(type(fields) == array, message: "element: please specify an array of fields, creating each field with the 'field' function.")
assert(type(prefix) == str, message: "element: the prefix must be a string.")
assert(type(typecheck) == bool, message: "element: the 'typecheck' argument must be a boolean (true to enable typechecking, false to disable).")
assert(type(allow-unknown-fields) == bool, message: "element: the 'allow-unknown-fields' argument must be a boolean.")
assert(synthesize == none or type(synthesize) == function, message: "element: 'synthesize' must be 'none' or a function element fields => element fields.")
assert(contextual == auto or type(contextual) == bool, message: "element: 'contextual' must be 'auto' (true if using a contextual feature) or a boolean (true to wrap the output in a 'context { ... }', false to not).")
assert(construct == none or type(construct) == function, message: "element: 'construct' must be 'none' (use default constructor) or a function receiving the original constructor and returning the new constructor.")
assert(type(display) == function, message: "element.declare: please specify a show rule in 'display:' to determine how your element is displayed.")
assert(type(fields) == array, message: "element.declare: please specify an array of fields, creating each field with the 'field' function.")
assert(prefix != none, message: "element.declare: please specify a 'prefix: ...' for your type, to distinguish it from types with the same name. If you are writing a package or template to be used by others, please do not use an empty prefix.")
assert(type(prefix) == str, message: "element.declare: the prefix must be a string, not '" + str(type(prefix)) + "'")
assert(type(typecheck) == bool, message: "element.declare: the 'typecheck' argument must be a boolean (true to enable typechecking, false to disable).")
assert(type(allow-unknown-fields) == bool, message: "element.declare: the 'allow-unknown-fields' argument must be a boolean.")
assert(synthesize == none or type(synthesize) == function, message: "element:.declare 'synthesize' must be 'none' or a function element fields => element fields.")
assert(contextual == auto or type(contextual) == bool, message: "element.declare: 'contextual' must be 'auto' (true if using a contextual feature) or a boolean (true to wrap the output in a 'context { ... }', false to not).")
assert(construct == none or type(construct) == function, message: "element.declare: 'construct' must be 'none' (use default constructor) or a function receiving the original constructor and returning the new constructor.")

if contextual == auto {
// Provide separate context for synthesize.
Expand Down Expand Up @@ -1332,7 +1333,7 @@
let final-constructor = if construct != none {
{
let test-construct = construct(modified-constructor)
assert(type(test-construct) == function, message: "element: the 'construct' function must receive original constructor and return the new constructor, a new function, not '" + str(type(test-construct)) + "'.")
assert(type(test-construct) == function, message: "element.declare: the 'construct' function must receive original constructor and return the new constructor, a new function, not '" + str(type(test-construct)) + "'.")
}

let final-constructor(..args, __elemmic_data: none) = {
Expand Down
3 changes: 2 additions & 1 deletion src/lib.typ
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#import "element.typ": set_, data, apply, element, revoke, reset, named, style-modes, prepare-get as get
#import "element.typ": set_, data, apply, revoke, reset, named, style-modes, prepare-get as get
#import "fields.typ": field
#import "pub/element.typ"
#import "pub/fields.typ"
#import "pub/types.typ"
#import "pub/leaky.typ"
Expand Down
2 changes: 2 additions & 0 deletions src/pub/element.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Public re-exports for element-related functions.
#import "../element.typ": declare
2 changes: 1 addition & 1 deletion src/types/custom.typ
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
construct: none,
) = {
assert(type(fields) == array, message: "types.declare: please specify an array of fields, creating each field with the 'field' function.")
assert(prefix != none, message: "types.declare: please specify a 'prefix: ...' for your type, to distinguish it from types with the same name. Please do not use an empty prefix if you are writing a package or template, otherwise that is OK.")
assert(prefix != none, message: "types.declare: please specify a 'prefix: ...' for your type, to distinguish it from types with the same name. If you are writing a package or template to be used by others, please do not use an empty prefix.")
assert(type(prefix) == str, message: "types.declare: the prefix must be a string, not '" + str(type(prefix)) + "'")
assert(type(typecheck) == bool, message: "types.declare: the 'typecheck' argument must be a boolean (true to enable typechecking in the constructor, false to disable).")
assert(type(allow-unknown-fields) == bool, message: "types.declare: the 'allow-unknown-fields' argument must be a boolean.")
Expand Down
4 changes: 2 additions & 2 deletions test/integration/bib.typ
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#import "/src/lib.typ" as e: element, field
#import "/src/lib.typ" as e: field

#set bibliography(title: [Custom Title])

#let (wock, wock-e) = element(
#let (wock, wock-e) = e.element.declare(
"wock",
display: it => {
context assert.eq(bibliography.title, [Custom Title])
Expand Down
4 changes: 2 additions & 2 deletions test/integration/chain-manipulation.typ
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#import "/src/lib.typ" as e: element, field
#import "/src/lib.typ" as e: field

#let (wock, wock-e) = element(
#let (wock, wock-e) = e.element.declare(
"wock",
display: it => {
text(it.color)[#it.inner]
Expand Down
4 changes: 2 additions & 2 deletions test/integration/fields.typ
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#import "/src/lib.typ" as e: element, field, types
#import "/src/lib.typ" as e: field, types

#let bool-or-pos-float = types.union(bool, float)
#(bool-or-pos-float.check = x => if type(x) == bool { true } else { x >= 0.0 })
#(bool-or-pos-float.error = _ => "float must be positive or zero")

#let (door, door-e) = element(
#let (door, door-e) = e.element.declare(
"door",
display: it => {
rect(fill: it.color)[#it]
Expand Down
6 changes: 3 additions & 3 deletions test/integration/many-consecutive-rules.typ
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#import "/src/lib.typ" as e: element, field, types
#import "/src/lib.typ" as e: field, types

#let (wock, wock-e) = element(
#let (wock, wock-e) = e.element.declare(
"wock",
display: it => {
[#it.body]
Expand All @@ -12,7 +12,7 @@
)
)

#let (dock, dock-e) = element(
#let (dock, dock-e) = e.element.declare(
"dock",
display: it => {
[#it.body]
Expand Down
4 changes: 2 additions & 2 deletions test/integration/many-rules.typ
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#import "/src/lib.typ" as e: element, field
#import "/src/lib.typ" as e: field

#let (wock, wock-e) = element(
#let (wock, wock-e) = e.element.declare(
"wock",
display: it => {
text(it.color)[#it.inner]
Expand Down
6 changes: 3 additions & 3 deletions test/integration/multi.typ
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#import "/src/lib.typ" as e: element, field
#import "/src/lib.typ" as e: field

#let (wibble, wibble-e) = element(
#let (wibble, wibble-e) = e.element.declare(
"wibble",
display: it => {
text(it.color)[#it.inner]
Expand All @@ -11,7 +11,7 @@
)
)

#let (wobble, wobble-e) = element(
#let (wobble, wobble-e) = e.element.declare(
"wobble",
display: it => {
rect(fill: it.fill, it.inner)
Expand Down
4 changes: 2 additions & 2 deletions test/integration/wock.typ
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#import "/src/lib.typ" as e: element, field, types
#import "/src/lib.typ" as e: field, types

#let (wock, wock-e) = element(
#let (wock, wock-e) = e.element.declare(
"wock",
display: it => {
text(it.color)[#it.inner]
Expand Down
4 changes: 2 additions & 2 deletions test/unit/elements/bib-clobber/test.typ
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#import "/test/unit/base.typ": empty
#show: empty

#import "/src/lib.typ" as e: element, field
#import "/src/lib.typ" as e: field

#set bibliography(title: [Custom Title])

#let (wock, wock-e) = element(
#let (wock, wock-e) = e.element.declare(
"wock",
display: it => {
context assert.eq(bibliography.title, [Custom Title])
Expand Down
6 changes: 3 additions & 3 deletions test/unit/elements/construct/test.typ
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#import "/test/unit/base.typ": template
#show: template

#import "/src/lib.typ" as e: element, field
#import "/src/lib.typ" as e: field

#let (wock, wock-e) = element(
#let (wock, wock-e) = e.element.declare(
"wock",
display: it => {
assert.eq(it.color, yellow)
Expand All @@ -15,7 +15,7 @@
)
)

#let (dock, dock-e) = element(
#let (dock, dock-e) = e.element.declare(
"dock",
construct: constructor => {
(a: red, b: 10%) => {
Expand Down
4 changes: 2 additions & 2 deletions test/unit/elements/declare/test.typ
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#import "/test/unit/base.typ": empty
#show: empty

#import "/src/lib.typ" as e: element, field
#import "/src/lib.typ" as e: field

#let (wock, wock-e) = element(
#let (wock, wock-e) = e.element.declare(
"wock",
display: it => {
assert.eq(it.color, red)
Expand Down
4 changes: 2 additions & 2 deletions test/unit/elements/decompose/test.typ
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#import "/test/unit/base.typ": empty
#show: empty

#import "/src/lib.typ" as e: element, field
#import "/src/lib.typ" as e: field

#let (wock, wock-e) = element(
#let (wock, wock-e) = e.element.declare(
"wock",
display: it => {
square(width: 6pt, fill: it.color)[#it.inner]
Expand Down
6 changes: 3 additions & 3 deletions test/unit/elements/fields/all-any/test.typ
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#import "/test/unit/base.typ": empty
#show: empty

#import "/src/lib.typ" as e: element, field, types
#import "/src/lib.typ" as e: field, types

#let bool-or-pos-float = types.wrap(
types.union(bool, float),
Expand All @@ -16,13 +16,13 @@
field("name", types.any, default: 3em),
)

#let (door, door-e) = element(
#let (door, door-e) = e.element.declare(
"door",
display: it => {},
fields: all-fields
)

#let (udoor, udoor-e) = element(
#let (udoor, udoor-e) = e.element.declare(
"udoor",
display: it => {},
fields: all-fields,
Expand Down
6 changes: 3 additions & 3 deletions test/unit/elements/fields/empty/test.typ
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#import "/test/unit/base.typ": empty
#show: empty

#import "/src/lib.typ" as e: element, field, types
#import "/src/lib.typ" as e: field, types

#let (door, door-e) = element(
#let (door, door-e) = e.element.declare(
"door",
display: it => {},
fields: ()
)

#let (udoor, udoor-e) = element(
#let (udoor, udoor-e) = e.element.declare(
"udoor",
display: it => {},
fields: (),
Expand Down
6 changes: 3 additions & 3 deletions test/unit/elements/fields/not-typechecked/test.typ
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#import "/test/unit/base.typ": empty
#show: empty

#import "/src/lib.typ" as e: element, field, types
#import "/src/lib.typ" as e: field, types

#let bool-or-pos-float = types.wrap(
types.union(bool, float),
Expand Down Expand Up @@ -34,7 +34,7 @@
field("bool-or-pos-float", bool-or-pos-float, default: 5.0)
)

#let (door, door-e) = element(
#let (door, door-e) = e.element.declare(
"door",
display: it => {
rect(fill: it.color)[#it]
Expand All @@ -43,7 +43,7 @@
fields: all-fields
)

#let (udoor, udoor-e) = element(
#let (udoor, udoor-e) = e.element.declare(
"udoor",
display: it => {
rect(fill: it.color)[#it]
Expand Down
6 changes: 3 additions & 3 deletions test/unit/elements/fields/typechecked/test.typ
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#import "/test/unit/base.typ": empty
#show: empty

#import "/src/lib.typ" as e: element, field, types
#import "/src/lib.typ" as e: field, types

#let bool-or-pos-float = types.wrap(
types.union(bool, float),
Expand Down Expand Up @@ -55,15 +55,15 @@
field("owner", person),
)

#let (door, door-e) = element(
#let (door, door-e) = e.element.declare(
"door",
display: it => {
rect(fill: it.color)[#it]
},
fields: all-fields
)

#let (udoor, udoor-e) = element(
#let (udoor, udoor-e) = e.element.declare(
"udoor",
display: it => {
rect(fill: it.color)[#it]
Expand Down
4 changes: 2 additions & 2 deletions test/unit/elements/fold/alignment/test.typ
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#import "/test/unit/base.typ": empty
#show: empty

#import "/src/lib.typ" as e: element, field, types
#import "/src/lib.typ" as e: field, types

#let (wock, wock-e) = element(
#let (wock, wock-e) = e.element.declare(
"wock",
display: it => {
(it.run)(it)
Expand Down
4 changes: 2 additions & 2 deletions test/unit/elements/fold/array/test.typ
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#import "/test/unit/base.typ": empty
#show: empty

#import "/src/lib.typ" as e: element, field, types
#import "/src/lib.typ" as e: field, types

#let (wock, wock-e) = element(
#let (wock, wock-e) = e.element.declare(
"wock",
display: it => {
(it.run)(it)
Expand Down
4 changes: 2 additions & 2 deletions test/unit/elements/fold/option-smart/test.typ
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#import "/test/unit/base.typ": empty
#show: empty

#import "/src/lib.typ" as e: element, field, types
#import "/src/lib.typ" as e: field, types

#let (wock, wock-e) = element(
#let (wock, wock-e) = e.element.declare(
"wock",
display: it => {
(it.run)(it)
Expand Down
6 changes: 3 additions & 3 deletions test/unit/elements/fold/stroke/test.typ
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#import "/test/unit/base.typ": template
#show: template

#import "/src/lib.typ" as e: element, field
#import "/src/lib.typ" as e: field

#let (wock, wock-e) = element(
#let (wock, wock-e) = e.element.declare(
"wock",
display: it => {
(it.run)(it)
Expand All @@ -16,7 +16,7 @@
)
)

#let (dock, dock-e) = element(
#let (dock, dock-e) = e.element.declare(
"dock",
display: it => {
(it.run)(it)
Expand Down
4 changes: 2 additions & 2 deletions test/unit/elements/get-default/test.typ
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#import "/test/unit/base.typ": empty
#show: empty

#import "/src/lib.typ" as e: element, field
#import "/src/lib.typ" as e: field

#let (wock, wock-e) = element(
#let (wock, wock-e) = e.element.declare(
"wock",
display: it => {
assert.eq(it.color, blue)
Expand Down
4 changes: 2 additions & 2 deletions test/unit/elements/get-nested/test.typ
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#import "/test/unit/base.typ": empty
#show: empty

#import "/src/lib.typ" as e: element, field
#import "/src/lib.typ" as e: field

#let (wock, wock-e) = element(
#let (wock, wock-e) = e.element.declare(
"wock",
display: it => {
assert.eq(it.color, blue)
Expand Down
4 changes: 2 additions & 2 deletions test/unit/elements/get/test.typ
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#import "/test/unit/base.typ": empty
#show: empty

#import "/src/lib.typ" as e: element, field
#import "/src/lib.typ" as e: field

#let (wock, wock-e) = element(
#let (wock, wock-e) = e.element.declare(
"wock",
display: it => {
assert.eq(it.color, blue)
Expand Down
Loading

0 comments on commit b1d153b

Please sign in to comment.