From f9eae5cba3c07340780eab52a09eda628574f69b Mon Sep 17 00:00:00 2001 From: James R Swift Date: Wed, 24 Jul 2024 14:06:11 +0100 Subject: [PATCH] Attempt to more properly count expected variables --- CHANGELOG.md | 1 + src/types/tuple.typ | 40 +++++++++++++++--- .../ref/1.png | Bin 0 -> 4711 bytes .../test.typ | 8 ++++ 4 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 tests/issues/34-tuples-do-not-respect-optional/ref/1.png create mode 100644 tests/issues/34-tuples-do-not-respect-optional/test.typ diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d08f3d..0e3af45 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/src/types/tuple.typ b/src/types/tuple.typ index 151c3f7..3b0cf56 100644 --- a/src/types/tuple.typ +++ b/src/types/tuple.typ @@ -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 }, ) } diff --git a/tests/issues/34-tuples-do-not-respect-optional/ref/1.png b/tests/issues/34-tuples-do-not-respect-optional/ref/1.png new file mode 100644 index 0000000000000000000000000000000000000000..bf699124ac10673236a3255b59554d30cf80ba1d GIT binary patch literal 4711 zcmcInc{tQ--yf9_*&P%HN11cg zUy_{|%V0b+UDx}ZXU^qZ?{%K{dhhFZ-Shi?zMtQHf4BML6LU{b zrhD@ZJIqxtgk?u{*qn1yb8daG!d9#VDb0Uc{m@GvD8#KjmXGcQma&6^zAbiN^e`f| z?GI9C28qSbCXMGk(s&k<#`CXEdD1K|6K7eOG|Q)uPj@DWbI|>e7weYf>fZG{)bOY_ zY&hIpS<|axp-(d9}LEcRs;wqhI_19RTG3&vQ%pYV8qjt^lYpk~PwhpyG;*EJiB z4GLS2MJzXm~QS5Msys{{v3JS z>RR!vl(ilPN1}-C7tHD#OW3~biyB6xbc8>2oZ5qw%Bk3(16^A+f(}&Y8~f+EjSj4MQY6L zFKq7pkY(EV=C1?g%WDk40rQRGO1yJ)^`^XjKe~OUg&V;4lu?D+5V>nwVw_f>W?%tb zmK$k$+mIK3#89B93%~xX)y9V7yf1)6DhqK?-iCrap8 zKfb7ZxNy#?L4@mRh_s34qw}kyLDlfpVEdk&k6Axv_1f(4zAKtATwGqKWnlcIVWWj( zPKWpRbd6d`0R;RrcZ58g5T4{1pn41;MfIbC#f6T5MN_9Iow|F$K@1SySbxyH=gO{ z#_;0Jtk~^!*t+Jh;YaRAcTi{w3(iE%;z_#vCBvHar^I*Q|uvO_NKA9N5M+&XQ)Yq zOvR&#|IgT5-K+gUhCqQ=v5tqf%7Kg3-*ESYU@{1ZUPx*V*JvNwr*s2P@0-NQb8Nq1 z0UNv44$cbZo1xu`y~HdL+~UNM&Y#FzbNMoYLhq{C7s^OUY2HK$`x;x-5z0GlH_B<- zY{15xpF_mGrNAj&4>-0Z@6skE_x&Q2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>Cm&&nMzBI)z{XYQ4H0*t-v&}%iRlH&3@B; zfmc$Wg3$+XAwC8W22{(p*?p3+?XhMR>=^^CU>f(*Dt@G89#(ITF&+8=bL;k{U9~_# zrQeso$XfxK&7kmpkh%_OF6W%}Xvkf!8fWFV`B;lG|ahWN>UVg6Dr-89r zu$Jivnu`e1c=HvVoU$>KyEHfI}xaA#14*|mDYPC9P9d!Cw6Q6d8wDLfJ zaR5$OWK`5)I(~EMNUdPzrpyHySEe&d*v*+wx7gKA^x#_@U*_JtHSu@};N%_~qxX4) zaoSpZX>4o^CM+!2!x^H9O9;pP$ji%1#OaHx4Dl#x*gziSiC`($k~nT+^pn)P#5dxR z2<5iNNPkFl1iE4S>${H9&m|=#oUr$uotDi8lkn7M3+W%GURnA$jWX`!#X*O`RlC4Ey_XQ0=*#+%ppsG2R%cm95(Slrk0ogkhqHI z+EKts6g{8TK*MB#DPJAM_B$+Pwc*kH{6My5KZc!kos#94qN1Y2#6+Rkoh!Wj{QS8C zz8@%Wi%UxPw^P4Af?_k5^ zu3z3o1?;f}^Ud}F^Shvf0I1GQ*zas|jr=vUE6)Z1^~a4Z9F=JFe4T1fVn0C?yvN1nn&$m@)ZjMUWfp=GJwaM+WY@Q1;{!QS4|bayf}n20OCDO7pl z*L3WwZhMf8gQMf{@Gy?zduWwUK)~4Eo>KHL?Ci7=VW0Cz&4wUJJE`e0WWUjnDGxG* zXY^ND_ouG<8{PMl@NX6PC!HK3w@%TD|G$J&E+mJ@ty8l2KM_tjj~pVme#+bA-Ib7Y zL|LiadosL6y%RA+lKUhjX+u(+mL$Mp_4=8*3XE@A#&@#CYVrIr=@1( TE&&>e2?Dg#^i)e#Y{LEpuvkNm literal 0 HcmV?d00001 diff --git a/tests/issues/34-tuples-do-not-respect-optional/test.typ b/tests/issues/34-tuples-do-not-respect-optional/test.typ new file mode 100644 index 0000000..20516a1 --- /dev/null +++ b/tests/issues/34-tuples-do-not-respect-optional/test.typ @@ -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) \ No newline at end of file