@mark1russell7/layout-contract 0.3.11 → 0.4.0

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/src/shapes.ts CHANGED
@@ -17,7 +17,7 @@ export interface ShapeGeometry {
17
17
  readonly area: boolean;
18
18
  /** The test that the shape is inside a region: `rectInRegion` for a `rect`. */
19
19
  readonly contains: boolean;
20
- /** The test that two shapes overlap: `overlapOf` for two `rect` shapes. */
20
+ /** The test that two shapes of this kind overlap: `overlapOf` for two `rect` shapes, `shapesOverlap` for two `circle` shapes. */
21
21
  readonly overlap: boolean;
22
22
  }
23
23
 
@@ -33,12 +33,12 @@ export interface ShapeKindSpec {
33
33
 
34
34
  /**
35
35
  * The declared shape kinds, in their order. Only `rect` has all of its geometry.
36
- * `polygon` and `circle` have their bounding box and their exact area. `free` has its bounding box only.
36
+ * `polygon` and `circle` have their bounding box and their exact area. A `circle` also has the overlap test of two circles. `free` has its bounding box only.
37
37
  */
38
38
  export const SHAPE_KINDS: readonly ShapeKindSpec[] = /* @__PURE__ */ Object.freeze([
39
39
  { id: "rect", fields: ["w", "h"], regions: ["rect"], geometry: { bounds: true, area: true, contains: true, overlap: true } },
40
40
  { id: "polygon", fields: ["points", "holes?"], regions: ["polygon"], geometry: { bounds: true, area: true, contains: false, overlap: false } },
41
- { id: "circle", fields: ["cx", "cy", "r"], regions: ["circle"], geometry: { bounds: true, area: true, contains: false, overlap: false } },
41
+ { id: "circle", fields: ["cx", "cy", "r"], regions: ["circle"], geometry: { bounds: true, area: true, contains: false, overlap: true } },
42
42
  { id: "free", fields: ["w", "h", "shape?"], regions: ["rect", "polygon", "circle", "ellipse"], geometry: { bounds: true, area: false, contains: false, overlap: false } },
43
43
  ] as const);
44
44
 
@@ -63,6 +63,22 @@ export interface ShapeSupport<Id extends string = string> {
63
63
  * A kind is in one list only: `shapes`, `safe` or `bound.shapes`. `docs/SHAPES.md` tells what a bound value proves.
64
64
  */
65
65
  readonly bound?: ShapeBound;
66
+ /** The inputs where the exact count of this rule is only safe, with the reason and the date of the fix. `inputShapeLevel` reads it. */
67
+ readonly limit?: ShapeLimit;
68
+ }
69
+
70
+ /**
71
+ * A stated limit of an exact rule on circles. An input that the limit names gets the level `safe` for each kind that is exact.
72
+ *
73
+ * - `neighbours`: the input has a circle beside another shape kind or an obstacle.
74
+ * - `container`: the input has a circle in a container that is a polygon or an ellipse.
75
+ */
76
+ export interface ShapeLimit {
77
+ readonly reads: "neighbours" | "container";
78
+ /** The reason, in words. */
79
+ readonly because: string;
80
+ /** The date of the fix, as `YYYY-MM-DD`. */
81
+ readonly until: string;
66
82
  }
67
83
 
68
84
  /**
@@ -111,14 +127,40 @@ export function shapeLevel(use: Pick<ShapeSupport, "shapes" | "safe" | "bound">,
111
127
  return use.bound?.shapes.includes(kind) === true ? "bound" : "none";
112
128
  }
113
129
 
130
+ /** The parts of an input that `inputShapeLevel` and `checkShapes` read. */
131
+ export type ShapeInput = Pick<LayoutInput, "id" | "boxes"> & Partial<Pick<LayoutInput, "obstacles" | "container">>;
132
+
133
+ /** This function tells if a limit names an input. An input without a circle box is not in a limit. */
134
+ function inLimit(limit: ShapeLimit, input: ShapeInput): boolean {
135
+ const kinds = shapeKindsOf(input);
136
+ if (!kinds.includes("circle")) return false;
137
+ if (limit.reads === "neighbours") return kinds.length > 1 || (input.obstacles?.length ?? 0) > 0;
138
+ const shape = input.container?.shape?.kind;
139
+ return shape === "polygon" || shape === "ellipse";
140
+ }
141
+
142
+ /**
143
+ * This function gives the level of a measure or a rule on a shape kind for one input. It is `shapeLevel` with the `limit` of the rule.
144
+ * When the limit names the input, the level `exact` becomes `safe`. Each other level does not change.
145
+ */
146
+ export function inputShapeLevel(use: Pick<ShapeSupport, "shapes" | "safe" | "bound" | "limit">, kind: ShapeKind, input: ShapeInput): ShapeLevel {
147
+ const level = shapeLevel(use, kind);
148
+ return level === "exact" && use.limit !== undefined && inLimit(use.limit, input) ? "safe" : level;
149
+ }
150
+
114
151
  /**
115
152
  * This function gives the problems of a list of measures and rules. An empty list tells that the list is good.
116
153
  * It refuses an unknown kind, a kind two times in one list, and a kind in more than one of `shapesOf`, `safe` and `bound.shapes`.
117
154
  * It also refuses a `bound` without a list of kinds, and a `side` that is not `upper` or `lower`.
155
+ * It also refuses a `limit` with a bad `reads` or a bad `until`.
118
156
  */
119
157
  export function shapeSupportProblems(uses: readonly ShapeSupport[]): string[] {
120
158
  const out: string[] = [];
121
159
  for (const use of uses) {
160
+ if (use.limit !== undefined) {
161
+ if (use.limit.reads !== "neighbours" && use.limit.reads !== "container") out.push(`${use.id}: the limit reads ${String(use.limit.reads)}, not neighbours or container`);
162
+ if (!/^\d{4}-\d{2}-\d{2}$/.test(String(use.limit.until))) out.push(`${use.id}: the limit date ${String(use.limit.until)} is not YYYY-MM-DD`);
163
+ }
122
164
  const bound: unknown = use.bound;
123
165
  const isBound = typeof bound === "object" && bound !== null && !Array.isArray(bound);
124
166
  if (bound !== undefined && !isBound) out.push(`${use.id}: the bound value is not an object with shapes and a side`);
@@ -222,13 +264,13 @@ export interface ShapeCheck {
222
264
  * By default, a kind of `safe` or of `bound.shapes` is a break, because the result is not exact.
223
265
  * With the level `safe`, a kind of `safe` is not a break. With the level `bound`, a kind of `safe` and a kind of `bound.shapes` are not a break.
224
266
  */
225
- export function checkShapes(input: Pick<LayoutInput, "id" | "boxes">, uses: readonly ShapeSupport[], options: ShapeCheck = {}): ClaimBreak[] {
267
+ export function checkShapes(input: ShapeInput, uses: readonly ShapeSupport[], options: ShapeCheck = {}): ClaimBreak[] {
226
268
  const kinds = shapeKindsOf(input);
227
269
  const out: ClaimBreak[] = [];
228
270
  for (const use of uses) {
229
271
  const shapes = shapesOf(use);
230
272
  for (const kind of kinds) {
231
- const level = shapeLevel(use, kind);
273
+ const level = inputShapeLevel(use, kind, input);
232
274
  const accepts = level === "exact" || (level === "safe" && (options.level === "safe" || options.level === "bound")) || (level === "bound" && options.level === "bound");
233
275
  if (accepts) continue;
234
276
  const ids = input.boxes.filter((b) => shapeKindOf(b) === kind).map((b) => b.id);