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

Attempt to more properly count expected variables in tuple #41

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Added

## Changed
- Tuple type now attempts to calculate the number of "expected" values in a way that respects optional or defaultable schema. (#34)


# [v0.2.1](https://github.com/typst-community/valakyrie/releases/tags/v0.2.1)
Expand Down
40 changes: 34 additions & 6 deletions src/types/tuple.typ
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,48 @@
tuple-exact: exact,
tuple-schema: args.pos(),
handle-descendents: (self, it, ctx: z-ctx(), scope: ()) => {
if (self.tuple-exact and self.tuple-schema.len() != it.len()){

// Issue 34: Handle differing numbers of optional elements

// Calculate number of expected arguments
let min-args = self.tuple-schema.filter(
x=>{
// I'm thinking this might cause issues with table and auto?
// But can't think of a pleasant solution
x.optional==false and x.default==none
}
).len()
let max-args = self.tuple-schema.len()
let num-args = it.len()

// Panic if the number of arguments does not match expected
if (self.tuple-exact and (num-args > max-args or num-args < min-args)){
(self.fail-validation)(self, it, ctx: ctx, scope: scope,
message: "Expected " + str(self.tuple-schema.len()) + " values, but got " + str(it.len())
message: "Expected "
+
if (min-args == max-args){
str(max-args)
} else {
str(min-args) + "-" + str(max-args)
}
+
" values, but got " +
str(it.len()
)
)
}

let parsed = ()

for (key, schema) in self.tuple-schema.enumerate() {
it.at(key) = (schema.validate)(
parsed.insert(key, (schema.validate)(
schema,
it.at(key),
it.at(key, default: none),
ctx: ctx,
scope: (..scope, str(key)),
)
))
}
it
return parsed
},
)
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions tests/issues/34-tuples-do-not-respect-optional/test.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#import "/src/lib.typ" as z
#import "/tests/utility.typ": *

#show: show-rule.with();

#let schema = z.tuple(z.string(), z.string(), z.content(optional: true))

#z.parse(("first", "second"), schema)