Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add named schemas for missing built in types #18

Merged
merged 17 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# [unreleased](https://github.com/typst-community/valakyrie/)

## Added
- Added schema generators for: angle, bytes, direction, fraction, function, label, length, location, plugin, ratio, relative, regex, selector, stroke, symbol, and version

## Changed
- Valkyrie is now distributed under the MIT license rather than GPL-3.0-only.
- Number schema generator now takes additional optional parameters `min` and `max` which a sugar for value assertions. These changes also apply to number specializations such as `float` and `integer`
Expand Down
Binary file modified docs/manual.pdf
Binary file not shown.
72 changes: 72 additions & 0 deletions docs/manual.typ
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,14 @@ For the sake of brevity and owing to their consistency, the arguments that each
)

#pagebreak()
#command("alignment", sarg[args], ret: "schema")[
Generates a schema that accepts only aligment objects as valid.
]

#command("angle", sarg[args], ret: "schema")[
Generates a schema that accepts only angles as valid.
]

#command("any", sarg[args], ret: "schema")[
Generates a schema that accepts any input as valid.
]
Expand All @@ -335,6 +343,10 @@ For the sake of brevity and owing to their consistency, the arguments that each
Generates a schema that accepts only booleans as valid.
]

#command("bytes", sarg[args], ret: "schema")[
Generates a schema that accepts only bytes as valid.
]

#command("color", sarg[args], ret: "schema")[
Generates a schema that accepts only colors as valid.
]
Expand Down Expand Up @@ -365,6 +377,10 @@ For the sake of brevity and owing to their consistency, the arguments that each
)[Dictionary of schema elements, used to define the validation rules for each entry.]
]

#command("direction", sarg[args], ret: "schema")[
Generates a schema that accepts only directions as valid.
]

#command("either", sarg[schema], sarg[args], ret: "schema")[
#argument(
"schema",
Expand All @@ -373,14 +389,66 @@ For the sake of brevity and owing to their consistency, the arguments that each
)[Positional arguments of validation schemes in order or preference that an input value should satisfy.]
]

#command("function", sarg[args], ret: "schema")[
Generates a schema that accepts only functions as valid.
]

#command("fraction", sarg[args], ret: "schema")[
Generates a schema that accepts only fractions as valid.
]

#command("gradient", sarg[args], ret: "schema")[
Generates a schema that accepts only gradient objects as valid.
]

#command("label", sarg[args], ret: "schema")[
Generates a schema that accepts only labels as valid.
]

#command("length", sarg[args], ret: "schema")[
Generates a schema that accepts only lengths as valid.
]

#command("location", sarg[args], ret: "schema")[
Generates a schema that accepts only locations as valid.
]

#command("number", arg(min: none), arg(max: none), sarg[args], ret: "schema")[
Generates a schema that accepts only numbers as valid.
]

#command("plugin", sarg[args], ret: "schema")[
Generates a schema that accepts only plugins as valid.
]

#command("ratio", sarg[args], ret: "schema")[
Generates a schema that accepts only ratios as valid.
]

#command("relative", sarg[args], ret: "schema")[
Generates a schema that accepts only relative types, lengths, or ratios as valid.
]

#command("regex", sarg[args], ret: "schema")[
Generates a schema that accepts only regex expressions as valid.
]

#command("selector", sarg[args], ret: "schema")[
Generates a schema that accepts only selectors as valid.
]

#command("string", arg(min: none), arg(max: none), sarg[args], ret: "schema")[
Generates a schema that accepts only strings as valid.
]

#command("stroke", sarg[args], ret: "schema")[
Generates a schema that accepts only stroke objects as valid.
]

#command("symbol", sarg[args], ret: "schema")[
Generates a schema that accepts only symbol types as valid.
]

#command("tuple", sarg[schema], sarg[args], ret: "schema")[
#argument(
"schema",
Expand All @@ -389,6 +457,10 @@ For the sake of brevity and owing to their consistency, the arguments that each
)[Positional arguments of validation schemes representing a tuple.]
]

#command("version", sarg[args], ret: "schema")[
Generates a schema that accepts only version objects as valid.
]

#command(
"sink",
arg(positional: none),
Expand Down
21 changes: 21 additions & 0 deletions src/types.typ
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,32 @@
#import "types/string.typ": string, ip, email
#import "types/tuple.typ": tuple

#let alignment = base-type.with(name: "alignment", types: (alignment,))
#let angle = base-type.with(name: "angle", types: (angle,))
#let any = base-type.with(name: "any")
#let boolean = base-type.with(name: "bool", types: (bool,))
#let bytes = base-type.with(name: "bytes", types: (bytes,))
#let color = base-type.with(name: "color", types: (color,))
#let content = base-type.with(name: "content", types: (content, str))
#let date = base-type.with(name: "date", types: (datetime,))
#let direction = base-type.with(name: "direction", types: (direction,))
#let function = base-type.with(name: "function", types: (function,))
#let fraction = base-type.with(name: "fraction", types: (fraction,))
#let gradient = base-type.with(name: "gradient", types: (gradient,))
#let label = base-type.with(name: "label", types: (label,))
#let length = base-type.with(name: "length", types: (length,))
#let location = base-type.with(name: "location", types: (location,))
#let plugin = base-type.with(name: "plugin", types: (plugin,))
#let ratio = base-type.with(name: "ratio", types: (ratio,))
#let regex = base-type.with(name: "regex", types: (regex,))
#let relative = base-type.with(
name: "relative",
types: (relative, ratio, length),
)
Comment on lines +29 to +32
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little concerned about this one, length and ratio will not have the same methods as relative for example, z.parse would return whatever type is passed without converting it to a relative.

This can be problematic, as shown here.

Should we be less permissive about the types or add a default post-transformation?

#let selector = base-type.with(name: "selector", types: (selector,))
#let stroke = base-type.with(name: "stroke", types: (stroke,))
#let symbol = base-type.with(name: "symbol", types: (symbol,))
#let version = base-type.with(name: "version", types: (version,))

#let choice(list, assertions: (), ..args) = base-type(
name: "enum",
Expand Down
Binary file added tests/types/gradient/ref/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions tests/types/gradient/test.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#import "/src/lib.typ" as z
#import "/tests/utility.typ": *

#show: show-rule.with();

#let schema = z.gradient()

= types/gradient
== Input types
#let _ = z.parse(gradient.linear(..color.map.rainbow), schema)
Binary file modified tests/types/string/ref/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/types/stroke/ref/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions tests/types/stroke/test.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#import "/src/lib.typ" as z
#import "/tests/utility.typ": *

#show: show-rule.with();

#let schema = z.stroke()

= types/stroke
== Input types
#let _ = z.parse(stroke(), schema)
Binary file added tests/types/version/ref/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions tests/types/version/test.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#import "/src/lib.typ" as z
#import "/tests/utility.typ": *

#show: show-rule.with();

#let schema = z.version()

= types/version
== Input types
#let _ = z.parse(version(0, 1, 0), schema)
Loading