@mark1russell7/layout-contract 0.3.9 → 0.3.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/build/index.d.ts +23 -3
- package/build/index.d.ts.map +1 -1
- package/build/index.js +74 -16
- package/build/index.js.map +1 -1
- package/package.json +1 -1
- package/src/strategy-catalog.ts +102 -19
- package/src/strategy-options.ts +6 -1
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package",
|
|
3
3
|
"name": "@mark1russell7/layout-contract",
|
|
4
|
-
"version": "0.3.
|
|
4
|
+
"version": "0.3.10",
|
|
5
5
|
"description": "The shared contract of the packing and highways layout engines: the input and record formats, the turn rule, margins, tolerances and hard rules.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"layout",
|
package/src/strategy-catalog.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { canonicalJson } from "./hash.ts";
|
|
3
3
|
import type { Supports } from "./record.ts";
|
|
4
4
|
import { type AnyOption, declarationIssues, type ListItem, type NoDefaultReason, optionIssue, type OptionScale, type ValueOption } from "./strategy-options.ts";
|
|
5
|
-
import type { StrategySpace } from "./strategy-space.ts";
|
|
5
|
+
import type { ParamValue, StrategySpace } from "./strategy-space.ts";
|
|
6
6
|
|
|
7
7
|
/** The schema of a strategy catalog. A new field is a new version of the schema. */
|
|
8
8
|
export const STRATEGY_CATALOG_SCHEMA = "strategy-catalog/1";
|
|
@@ -95,10 +95,22 @@ export interface SupportProbe {
|
|
|
95
95
|
readonly refuses: (key: string) => boolean | undefined;
|
|
96
96
|
}
|
|
97
97
|
|
|
98
|
+
/**
|
|
99
|
+
* The statement of how one place of a space reaches the options of an engine. The file of a catalog does not hold it, because a value can hold code.
|
|
100
|
+
*
|
|
101
|
+
* - `spaceOnly` tells that the place sets no option, with one sentence that tells why. A stage option without an entry can also have this statement.
|
|
102
|
+
* - `option` names the option of the param, when the option has another name. `value` gives the option value of one param value, and `undefined` when the param value sets no option.
|
|
103
|
+
*/
|
|
104
|
+
export type ParamRoute =
|
|
105
|
+
| { readonly spaceOnly: string }
|
|
106
|
+
| { readonly option?: string; readonly value?: (value: ParamValue) => unknown };
|
|
107
|
+
|
|
98
108
|
/** The options of `checkCatalog`. */
|
|
99
109
|
export interface CatalogCheckOptions {
|
|
100
110
|
/** Each param of these spaces is examined against the declaration of its option. */
|
|
101
111
|
readonly spaces?: readonly StrategySpace[];
|
|
112
|
+
/** The routes of the params of `spaces`. The key is `<stage>/<option>.<param>`, or `<stage>/<option>` for a stage option. A route that matches nothing is a fault. */
|
|
113
|
+
readonly paramRoutes?: Readonly<Record<string, ParamRoute>>;
|
|
102
114
|
readonly probes?: readonly SupportProbe[];
|
|
103
115
|
/** This function runs `owner` with `choice` in its option `option`. It gives `true` for a refusal, or `undefined` if the input cannot go to `owner`. */
|
|
104
116
|
readonly refusesChoice?: (owner: string, option: string, choice: string) => boolean | undefined;
|
|
@@ -110,8 +122,10 @@ export interface CatalogCheck {
|
|
|
110
122
|
readonly faults: readonly string[];
|
|
111
123
|
/** The claims that no run examined, by key and with the reason. */
|
|
112
124
|
readonly notRun: readonly string[];
|
|
113
|
-
/** The stage options and the params of a space that have no entry
|
|
125
|
+
/** The stage options and the params of a space that have no entry, no declaration and no route. */
|
|
114
126
|
readonly notMatched: readonly string[];
|
|
127
|
+
/** The places of a space with a `spaceOnly` route, each with its reason. */
|
|
128
|
+
readonly spaceOnly: readonly string[];
|
|
115
129
|
}
|
|
116
130
|
|
|
117
131
|
/** The value of a strategy option that holds the options of the chosen strategy. A declaration with `inlineOptions` takes it. */
|
|
@@ -138,11 +152,11 @@ const ENTRY_FIELDS: readonly string[] = ["key", "id", "step", "family", "kind",
|
|
|
138
152
|
const COMMON_FIELDS: readonly string[] = ["type", "describe", "default", "noDefault", "when"];
|
|
139
153
|
const NUMBER_FIELDS: readonly string[] = [...COMMON_FIELDS, "min", "max", "step", "scale", "special", "marks", "values"];
|
|
140
154
|
const ENUM_FIELDS: readonly string[] = [...COMMON_FIELDS, "values", "custom"];
|
|
141
|
-
const STRATEGY_FIELDS: readonly string[] = [...COMMON_FIELDS, "step", "kinds", "optionsKey", "inlineOptions", "optIn"];
|
|
155
|
+
const STRATEGY_FIELDS: readonly string[] = [...COMMON_FIELDS, "step", "kinds", "optionsKey", "inlineOptions", "optIn", "needs"];
|
|
142
156
|
const MEMBER_FIELDS: readonly string[] = ["strategy", "options", "id"];
|
|
143
157
|
const LIST_FIELDS: readonly string[] = [...COMMON_FIELDS, "of", "min", "max"];
|
|
144
158
|
/** The fields of a declaration, in the order of the file. */
|
|
145
|
-
const OPTION_ORDER: readonly string[] = ["type", "describe", "min", "max", "step", "scale", "special", "marks", "values", "custom", "kinds", "optionsKey", "inlineOptions", "optIn", "of", "default", "noDefault", "when"];
|
|
159
|
+
const OPTION_ORDER: readonly string[] = ["type", "describe", "min", "max", "step", "scale", "special", "marks", "values", "custom", "kinds", "optionsKey", "inlineOptions", "optIn", "needs", "of", "default", "noDefault", "when"];
|
|
146
160
|
|
|
147
161
|
type Fields = Readonly<Record<string, unknown>>;
|
|
148
162
|
|
|
@@ -262,6 +276,10 @@ function readStrategyFields(at: string, raw: Fields, faults: string[]): Fields {
|
|
|
262
276
|
if (typeof raw[f] === "boolean") out[f] = raw[f];
|
|
263
277
|
else faults.push(`${at}: "${f}" is not true or false.`);
|
|
264
278
|
}
|
|
279
|
+
if (raw["needs"] !== undefined) {
|
|
280
|
+
if (isFields(raw["needs"]) && Object.values(raw["needs"]).every(isText)) out["needs"] = raw["needs"];
|
|
281
|
+
else faults.push(`${at}: "needs" is not a record of texts.`);
|
|
282
|
+
}
|
|
265
283
|
if (raw["inlineOptions"] === true && raw["optionsKey"] !== undefined) faults.push(`${at}: "inlineOptions" and "optionsKey" give two places for the options.`);
|
|
266
284
|
if (raw["default"] !== undefined) {
|
|
267
285
|
if (isText(raw["default"]) || (raw["inlineOptions"] === true && isMember(raw["default"]))) out["default"] = raw["default"];
|
|
@@ -430,8 +448,9 @@ function readEntry(at: string, raw: unknown, faults: string[]): CatalogEntry | u
|
|
|
430
448
|
}
|
|
431
449
|
|
|
432
450
|
/**
|
|
433
|
-
* This function gives the strategies that a chooser offers, by the `step`, `kinds` and `
|
|
451
|
+
* This function gives the strategies that a chooser offers, by the `step`, `kinds`, `optIn` and `needs` of an option. Thus no chooser keeps a copy of this rule.
|
|
434
452
|
* A baseline is not in the result. A list gives the strategies of its items. An option of each other type gives nothing.
|
|
453
|
+
* A strategy that does not list a dimension of `needs` has no declared limit there, so the option offers it.
|
|
435
454
|
* Without a declaration, the function gives each strategy that is not a baseline.
|
|
436
455
|
*/
|
|
437
456
|
export function catalogChoices(strategies: readonly CatalogEntry[], declaration?: AnyOption | ListItem): readonly CatalogEntry[] {
|
|
@@ -439,7 +458,14 @@ export function catalogChoices(strategies: readonly CatalogEntry[], declaration?
|
|
|
439
458
|
if (declaration === undefined) return offered;
|
|
440
459
|
const d = (declaration.type === "list" ? declaration.of : declaration) as ValueOption | undefined;
|
|
441
460
|
if (d?.type !== "strategy") return [];
|
|
442
|
-
|
|
461
|
+
const needs = Object.entries(d.needs ?? {});
|
|
462
|
+
return offered.filter(
|
|
463
|
+
(e) =>
|
|
464
|
+
(d.step === undefined || e.step === d.step) &&
|
|
465
|
+
(d.kinds === undefined || d.kinds.includes(e.kind)) &&
|
|
466
|
+
(d.optIn === undefined || d.optIn === e.optIn) &&
|
|
467
|
+
needs.every(([dimension, value]) => e.supports[dimension]?.includes(value) !== false),
|
|
468
|
+
);
|
|
443
469
|
}
|
|
444
470
|
|
|
445
471
|
/** This function adds one fault for each option of a member that its chosen strategy does not take. */
|
|
@@ -691,31 +717,85 @@ function checkChoices(entry: CatalogEntry, strategies: readonly CatalogEntry[],
|
|
|
691
717
|
}
|
|
692
718
|
}
|
|
693
719
|
|
|
694
|
-
/**
|
|
695
|
-
|
|
720
|
+
/** The lists that the check of the spaces writes, and the routes that it reads. */
|
|
721
|
+
interface SpaceCheck {
|
|
722
|
+
readonly routes: Readonly<Record<string, ParamRoute>>;
|
|
723
|
+
/** The keys of the routes that matched a place. */
|
|
724
|
+
readonly used: Set<string>;
|
|
725
|
+
readonly faults: string[];
|
|
726
|
+
readonly notMatched: string[];
|
|
727
|
+
readonly spaceOnly: string[];
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
/** This function examines each value of one param against the declaration of its option, through the route of the param. */
|
|
731
|
+
function checkParamValues(entry: CatalogEntry, engine: string, place: string, option: string, values: readonly ParamValue[], route: ParamRoute | undefined, check: SpaceCheck): void {
|
|
732
|
+
const declaration = entry.options[option]!;
|
|
733
|
+
const toOption = route !== undefined && "value" in route ? route.value : undefined;
|
|
734
|
+
for (const value of values) {
|
|
735
|
+
let mapped: unknown = value;
|
|
736
|
+
try {
|
|
737
|
+
if (toOption !== undefined) mapped = toOption(value);
|
|
738
|
+
} catch (error) {
|
|
739
|
+
check.faults.push(`${entry.key}: the route "${place}" fails on ${String(value)}: ${error instanceof Error ? error.message : String(error)}`);
|
|
740
|
+
continue;
|
|
741
|
+
}
|
|
742
|
+
if (mapped === undefined) continue;
|
|
743
|
+
const issue = optionIssue(option, mapped, declaration);
|
|
744
|
+
if (issue !== undefined) check.faults.push(`${entry.key}: the space ${engine} is outside the declaration. ${issue.message}`);
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
/** This function examines each param of a space against the declaration of its option, with the routes of the engine. */
|
|
749
|
+
function checkSpaceParams(space: StrategySpace, entries: ReadonlyMap<string, CatalogEntry>, check: SpaceCheck): void {
|
|
750
|
+
const { routes, used, faults, notMatched, spaceOnly } = check;
|
|
751
|
+
const routeOf = (place: string): ParamRoute | undefined => {
|
|
752
|
+
const route = routes[place];
|
|
753
|
+
if (route !== undefined) used.add(place);
|
|
754
|
+
return route;
|
|
755
|
+
};
|
|
696
756
|
for (const stage of space.stages) {
|
|
697
757
|
for (const option of stage.options) {
|
|
698
|
-
const
|
|
758
|
+
const name = `${stage.name}/${option.id}`;
|
|
759
|
+
const entry = entries.get(name) ?? entries.get(option.id);
|
|
699
760
|
if (entry === undefined) {
|
|
700
|
-
|
|
761
|
+
const reason = routes[name];
|
|
762
|
+
if (reason !== undefined && "spaceOnly" in reason) {
|
|
763
|
+
used.add(name);
|
|
764
|
+
spaceOnly.push(`${name}: ${reason.spaceOnly}`);
|
|
765
|
+
} else {
|
|
766
|
+
notMatched.push(name);
|
|
767
|
+
}
|
|
701
768
|
continue;
|
|
702
769
|
}
|
|
703
770
|
for (const param of option.params) {
|
|
704
|
-
const
|
|
705
|
-
|
|
706
|
-
|
|
771
|
+
const place = `${name}.${param.name}`;
|
|
772
|
+
const route = routeOf(place);
|
|
773
|
+
if (route !== undefined && "spaceOnly" in route) {
|
|
774
|
+
if (entry.options[param.name] === undefined) spaceOnly.push(`${place}: ${route.spaceOnly}`);
|
|
775
|
+
else faults.push(`${entry.key}: the route "${place}" says that the param is only in the space, and the strategy has this option.`);
|
|
707
776
|
continue;
|
|
708
777
|
}
|
|
709
|
-
const
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
778
|
+
const target = route?.option ?? param.name;
|
|
779
|
+
if (entry.options[target] === undefined) {
|
|
780
|
+
if (route === undefined) notMatched.push(place);
|
|
781
|
+
else faults.push(`${entry.key}: the route "${place}" names "${target}", and the strategy has no such option.`);
|
|
782
|
+
continue;
|
|
713
783
|
}
|
|
784
|
+
const values = [param.min, param.max, ...(param.values ?? []), param.default].filter((v): v is ParamValue => v !== undefined);
|
|
785
|
+
checkParamValues(entry, space.engine, place, target, values, route, check);
|
|
714
786
|
}
|
|
715
787
|
}
|
|
716
788
|
}
|
|
717
789
|
}
|
|
718
790
|
|
|
791
|
+
/** This function adds one fault for each route that matched nothing, and for each `spaceOnly` route with no reason. */
|
|
792
|
+
function checkRoutes({ routes, used, faults }: SpaceCheck): void {
|
|
793
|
+
for (const [place, route] of Object.entries(routes)) {
|
|
794
|
+
if (!used.has(place)) faults.push(`The route "${place}" matches nothing in the spaces.`);
|
|
795
|
+
if ("spaceOnly" in route && !isText(route.spaceOnly)) faults.push(`The route "${place}" has no reason.`);
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
|
|
719
799
|
/**
|
|
720
800
|
* This function examines a catalog against its engine: the live defaults, the set of strategies and a round trip through JSON.
|
|
721
801
|
* It also examines the claims of determinism, `supports`, `limits`, the choices of each strategy option and the spaces.
|
|
@@ -725,6 +805,7 @@ export function checkCatalog(source: StrategyCatalogSource, registry: readonly R
|
|
|
725
805
|
const faults: string[] = [];
|
|
726
806
|
const notRun: string[] = [];
|
|
727
807
|
const notMatched: string[] = [];
|
|
808
|
+
const spaceOnly: string[] = [];
|
|
728
809
|
let text: string | undefined;
|
|
729
810
|
try {
|
|
730
811
|
text = writeStrategyCatalog(source, "0.0.0");
|
|
@@ -747,6 +828,8 @@ export function checkCatalog(source: StrategyCatalogSource, registry: readonly R
|
|
|
747
828
|
checkProbes(entry, options.probes ?? [], faults, notRun);
|
|
748
829
|
if (options.refusesChoice !== undefined) checkChoices(entry, source.strategies, options.refusesChoice, faults, notRun);
|
|
749
830
|
}
|
|
750
|
-
|
|
751
|
-
|
|
831
|
+
const spaceCheck: SpaceCheck = { routes: options.paramRoutes ?? {}, used: new Set(), faults, notMatched, spaceOnly };
|
|
832
|
+
for (const space of options.spaces ?? []) checkSpaceParams(space, entries, spaceCheck);
|
|
833
|
+
checkRoutes(spaceCheck);
|
|
834
|
+
return { faults, notRun, notMatched, spaceOnly };
|
|
752
835
|
}
|
package/src/strategy-options.ts
CHANGED
|
@@ -86,6 +86,8 @@ export interface ValueOption<V = unknown, S extends string = string> extends Opt
|
|
|
86
86
|
readonly inlineOptions?: boolean;
|
|
87
87
|
/** The value `false` leaves out the opt-in strategies, because the option finds only the default set of the engine. This field is for the type `strategy`. */
|
|
88
88
|
readonly optIn?: boolean;
|
|
89
|
+
/** For each dimension of `supports`, the value that a chosen strategy must take. This field is for the type `strategy`, and also for an item of a list. */
|
|
90
|
+
readonly needs?: Readonly<Record<string, string>>;
|
|
89
91
|
/** The declaration of each item, for the type `list`. */
|
|
90
92
|
readonly of?: ListItem<S>;
|
|
91
93
|
/** The smallest number of items, for the type `list`. */
|
|
@@ -108,13 +110,16 @@ export type ListItem<S extends string = string> = ItemOf<AnyOption<S>>;
|
|
|
108
110
|
/**
|
|
109
111
|
* The declaration that fits the type of an option value: numbers, booleans, string unions or other values.
|
|
110
112
|
* A union of strings and other values takes an enum of the strings or a value declaration. An example is a rule or the name of a rule.
|
|
113
|
+
* A plain `string` takes an enum or a value declaration, because the value can be the key of a strategy.
|
|
111
114
|
*/
|
|
112
115
|
export type OptionDeclaration<V, S extends string = string> = [V] extends [number]
|
|
113
116
|
? NumberOption
|
|
114
117
|
: [V] extends [boolean]
|
|
115
118
|
? BooleanOption
|
|
116
119
|
: [V] extends [string]
|
|
117
|
-
?
|
|
120
|
+
? string extends V
|
|
121
|
+
? EnumOption<V> | ValueOption<V, S>
|
|
122
|
+
: EnumOption<V>
|
|
118
123
|
: [Extract<V, string>] extends [never]
|
|
119
124
|
? ValueOption<V, S>
|
|
120
125
|
: EnumOption<Extract<V, string>> | ValueOption<V, S>;
|