@mark1russell7/layout-contract 0.6.0 → 0.6.1

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/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.6.0",
4
+ "version": "0.6.1",
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/record.ts CHANGED
@@ -67,6 +67,27 @@ export interface LayoutRecord {
67
67
  /** The reason when the strategy did not run, for example an input over its `maxNodes`. Then the record has no measures. */
68
68
  readonly skipped?: string;
69
69
  readonly commit?: string;
70
+ /**
71
+ * The facts of a carry: a later run kept this record after an engine release, with no new measure (L147).
72
+ * Without the field, the engines of the run of the record measured it.
73
+ * The measures, the hashes and the times stay the values of the versions of `carried.from`.
74
+ */
75
+ readonly carried?: Carried;
76
+ }
77
+
78
+ /**
79
+ * The facts of a record that a run kept from an older run after an engine release (L147).
80
+ * The run kept it because each strategy of the record has the same fingerprint at the two versions.
81
+ */
82
+ export interface Carried {
83
+ /** The version of each engine at the measure, by the name of the engine. */
84
+ readonly from: Readonly<Record<string, string>>;
85
+ /** The version of each engine of the run that keeps the record. The names are the names of `from`. */
86
+ readonly to: Readonly<Record<string, string>>;
87
+ /** The fingerprint of each strategy of the record, by its name: `fingerprintOf` of its probes, the same at the two versions. */
88
+ readonly fingerprints: Readonly<Record<string, string>>;
89
+ /** The id of the run that measured the record. */
90
+ readonly run?: string;
70
91
  }
71
92
 
72
93
  /**
@@ -135,6 +156,25 @@ function numberMap(out: string[], v: unknown, name: string): void {
135
156
  for (const [key, n] of Object.entries(v)) if (typeof n !== "number") out.push(`${name}.${key} is not a number`);
136
157
  }
137
158
 
159
+ /** This function tells if a value is an object whose values are strings. */
160
+ const isStringMap = (v: unknown): v is Readonly<Record<string, string>> => isFields(v) && Object.values(v).every((s) => typeof s === "string");
161
+
162
+ /** This function gives the problems of the field `carried` of a record. */
163
+ function carriedProblems(carried: unknown): string[] {
164
+ if (!isFields(carried)) return ["carried is not an object"];
165
+ const out: string[] = [];
166
+ for (const key of ["from", "to", "fingerprints"] as const) if (!isStringMap(carried[key])) out.push(`carried.${key} is not a map of strings`);
167
+ if (carried["run"] !== undefined && typeof carried["run"] !== "string") out.push("carried.run is not a string");
168
+ if (out.length > 0) return out;
169
+ const from = carried["from"] as Readonly<Record<string, string>>;
170
+ const to = carried["to"] as Readonly<Record<string, string>>;
171
+ const names = (m: Readonly<Record<string, string>>): string => Object.keys(m).sort().join(",");
172
+ if (names(from) === "" || names(from) !== names(to)) out.push("carried.from and carried.to do not name the same engines");
173
+ else if (Object.keys(from).every((name) => from[name] === to[name])) out.push("carried.to has no other version than carried.from");
174
+ if (Object.keys(carried["fingerprints"] as object).length === 0) out.push("carried.fingerprints is empty");
175
+ return out;
176
+ }
177
+
138
178
  /** This function gives the problems of the field `env` of a record. */
139
179
  function envProblems(env: unknown): string[] {
140
180
  if (!isFields(env)) return ["env is not an object"];
@@ -180,6 +220,10 @@ export function checkLayoutRecord(value: unknown): string[] {
180
220
  const because = v["because"];
181
221
  if (because !== undefined && (!Array.isArray(because) || !because.every((s) => typeof s === "string"))) out.push("because is not a list of strings");
182
222
  if (v["env"] !== undefined) out.push(...envProblems(v["env"]));
223
+ if (v["carried"] !== undefined) {
224
+ out.push(...carriedProblems(v["carried"]));
225
+ if (v["skipped"] !== undefined) out.push("a skipped record has carried");
226
+ }
183
227
  const deterministic = v["deterministic"];
184
228
  if (v["skipped"] === undefined) {
185
229
  const noClaim = deterministic === null && v["oneRun"] === true;
@@ -199,3 +243,32 @@ export function parseLayoutRecord(value: unknown): LayoutRecord {
199
243
  const record = value as LayoutRecord;
200
244
  return record.skipped !== undefined && record.deterministic !== null ? { ...record, deterministic: null } : record;
201
245
  }
246
+
247
+ /** The facts of one carry, for `carryRecord`. */
248
+ export interface CarryFacts {
249
+ /** The version of each engine of the run that keeps the record. */
250
+ readonly to: Readonly<Record<string, string>>;
251
+ /** The fingerprint of each strategy of the record, the same at the two versions. */
252
+ readonly fingerprints: Readonly<Record<string, string>>;
253
+ /** The version of each engine at the measure. A record that a run carried before keeps its `carried.from`, and the function ignores this value. */
254
+ readonly from?: Readonly<Record<string, string>>;
255
+ /** The id of the run that measured the record. A record that a run carried before keeps its `carried.run`. */
256
+ readonly run?: string;
257
+ }
258
+
259
+ /**
260
+ * This function gives a record that a run keeps after an engine release, with the field `carried` (L147).
261
+ * A record that a run carried before keeps the versions and the run of its measure, so `carried.from` always names the measure.
262
+ * The function throws a `RangeError` for a skipped record: a skip makes no claim on an output, so the run measures the pair again.
263
+ * It also throws a `RangeError` when `checkLayoutRecord` refuses the field `carried` of the result.
264
+ */
265
+ export function carryRecord(record: LayoutRecord, facts: CarryFacts): LayoutRecord {
266
+ if (record.skipped !== undefined) throw new RangeError(`The record of ${record.strategy} on ${record.input} is skipped, so a run measures it again.`);
267
+ const from = record.carried?.from ?? facts.from;
268
+ if (from === undefined) throw new RangeError(`The record of ${record.strategy} on ${record.input} has no carried.from, so the carry needs from.`);
269
+ const run = record.carried?.run ?? facts.run;
270
+ const carried: Carried = { from, to: facts.to, fingerprints: facts.fingerprints, ...(run === undefined ? {} : { run }) };
271
+ const problems = carriedProblems(carried);
272
+ if (problems.length > 0) throw new RangeError(`The carry of ${record.strategy} on ${record.input} is bad: ${problems.join(", ")}.`);
273
+ return { ...record, carried };
274
+ }