Skip to content

Commit

Permalink
reverts and corrections to refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
seveibar committed Sep 20, 2024
1 parent a83df51 commit 03edfc4
Show file tree
Hide file tree
Showing 14 changed files with 121 additions and 164 deletions.
9 changes: 5 additions & 4 deletions scripts/refactor-template.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ Some of the new rules:
- Prefer absolute imports from "src/"
- Use `getZodPrefixedIdWithDefault` to generate a unique id for the primary id of the object
- The main interface should have a `/**` multi-line comment describing the object
- If the zod type is a union or `or` type, then export interfaces for each of the
of the types in the union, then define a union type for each of the exported
interfaces `export type MainInterface = SomeInterface1 | SomeInterface2`.
- If the zod type is a union or `or` type, then it into separate zod types
and export interfaces for each of the of the types in the union, then define a
union type for each of the exported interfaces
`export type MainInterface = SomeInterface1 | SomeInterface2`.

```ts
// EXAMPLE FILE OF NEW CIRCUIT SPECIFICATION
import { z } from "zod"
import { point, type Point, getZodPrefixedIdWithDefault } from "src/common"
import { layer_ref, type LayerRef } from "src/properties/layer_ref"
import { layer_ref, type LayerRef } from "src/pcb/properties/layer_ref"
import { rotation, length, type Rotation, type Length } from "src/units"
import { expectTypesMatch } from "src/utils/expect-types-match"

Expand Down
2 changes: 1 addition & 1 deletion src/pcb/pcb_component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { z } from "zod"
import { point, type Point, getZodPrefixedIdWithDefault } from "src/common"
import { layer_ref, type LayerRef } from "src/properties/layer_ref"
import { layer_ref, type LayerRef } from "src/pcb/properties/layer_ref"
import { rotation, length, type Rotation, type Length } from "src/units"
import { expectTypesMatch } from "src/utils/expect-types-match"

Expand Down
12 changes: 9 additions & 3 deletions src/pcb/pcb_fabrication_note_path.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { z } from "zod"
import { getZodPrefixedIdWithDefault } from "src/common"
import { visible_layer, type LayerRef } from "src/properties/layer_ref"
import {
layer_ref,
visible_layer,
type LayerRef,
} from "src/pcb/properties/layer_ref"
import { point, type Point } from "src/common"
import { length, type Length } from "src/units"
import { expectTypesMatch } from "src/utils/expect-types-match"
Expand All @@ -12,7 +16,7 @@ export const pcb_fabrication_note_path = z
"pcb_fabrication_note_path",
),
pcb_component_id: z.string(),
layer: visible_layer,
layer: layer_ref,
route: z.array(point),
stroke_width: length,
color: z.string().optional(),
Expand All @@ -21,7 +25,9 @@ export const pcb_fabrication_note_path = z
"Defines a fabrication path on the PCB for fabricators or assemblers",
)

export type PcbFabricationNotePathInput = z.input<typeof pcb_fabrication_note_path>
export type PcbFabricationNotePathInput = z.input<
typeof pcb_fabrication_note_path
>
type InferredPcbFabricationNotePath = z.infer<typeof pcb_fabrication_note_path>

/**
Expand Down
13 changes: 10 additions & 3 deletions src/pcb/pcb_fabrication_note_text.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { z } from "zod"
import { point, type Point } from "src/common"
import { distance, type Length } from "src/units"
import { visible_layer, type LayerRef } from "src/properties/layer_ref"
import { visible_layer, type LayerRef } from "src/pcb/properties/layer_ref"
import { getZodPrefixedIdWithDefault } from "src/common/getZodPrefixedIdWithDefault"
import { expectTypesMatch } from "src/utils/expect-types-match"

Expand All @@ -26,7 +26,9 @@ export const pcb_fabrication_note_text = z
"Defines a fabrication note in text on the PCB, useful for leaving notes for assemblers or fabricators",
)

export type PcbFabricationNoteTextInput = z.input<typeof pcb_fabrication_note_text>
export type PcbFabricationNoteTextInput = z.input<
typeof pcb_fabrication_note_text
>
type InferredPcbFabricationNoteText = z.infer<typeof pcb_fabrication_note_text>

/**
Expand All @@ -41,7 +43,12 @@ export interface PcbFabricationNoteText {
text: string
layer: LayerRef
anchor_position: Point
anchor_alignment: "center" | "top_left" | "top_right" | "bottom_left" | "bottom_right"
anchor_alignment:
| "center"
| "top_left"
| "top_right"
| "bottom_left"
| "bottom_right"
color?: string
}

Expand Down
92 changes: 28 additions & 64 deletions src/pcb/pcb_hole.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,33 @@
import { z } from "zod"
import { distance, type Length, getZodPrefixedIdWithDefault } from "src/units"
import { expectTypesMatch } from "src/utils/expect-types-match"
import { distance } from "../units"

export const pcb_hole = z
.union([
z
.object({
type: z.literal("pcb_hole"),
pcb_hole_id: getZodPrefixedIdWithDefault("pcb_hole"),
hole_shape: z
.enum(["circle", "square", "round"])
.default("circle")
.transform((shape) => {
if (shape === "round") return "circle"
return shape as "circle" | "square"
}),
hole_diameter: z.number(),
x: distance,
y: distance,
})
.describe("Defines a circular or square hole on the PCB"),
z
.object({
type: z.literal("pcb_hole"),
pcb_hole_id: getZodPrefixedIdWithDefault("pcb_hole"),
hole_shape: z.literal("oval"),
hole_width: z.number(),
hole_height: z.number(),
x: distance,
y: distance,
})
.describe("Defines an oval hole on the PCB"),
])
.object({
pcb_hole_id: z.string(),
type: z.literal("pcb_hole"),
hole_shape: z
.enum(["circle", "square", "round"])
.default("circle")
.transform((shape) => {
if (shape === "round") return "circle"
return shape as "circle" | "square"
}),
hole_diameter: z.number(),
x: distance,
y: distance,
})
.or(
z.object({
pcb_hole_id: z.string(),
type: z.literal("pcb_hole"),
hole_shape: z.literal("oval"),
hole_width: z.number(),
hole_height: z.number(),
x: distance,
y: distance,
}),
)
.describe("Defines a hole on the PCB")

export type PcbHoleInput = z.input<typeof pcb_hole>
type InferredPcbHole = z.infer<typeof pcb_hole>

/**
* Defines a hole on the PCB
*/
export interface PcbHole {
type: "pcb_hole"
pcb_hole_id: string
hole_shape: "circle" | "square" | "oval"
x: Length
y: Length
}

interface PcbCircularOrSquareHole extends PcbHole {
hole_shape: "circle" | "square"
hole_diameter: number
}

interface PcbOvalHole extends PcbHole {
hole_shape: "oval"
hole_width: number
hole_height: number
}

export type PcbHole = PcbCircularOrSquareHole | PcbOvalHole

/**
* @deprecated use PcbHole
*/
export type PCBHole = PcbHole

expectTypesMatch<PcbHole, InferredPcbHole>(true)
export type PCBHoleInput = z.input<typeof pcb_hole>
export type PCBHole = z.infer<typeof pcb_hole>
70 changes: 19 additions & 51 deletions src/pcb/pcb_keepout.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,29 @@
import { z } from "zod"
import { point, type Point, getZodPrefixedIdWithDefault } from "src/common"
import { distance, type Distance } from "src/units"
import { expectTypesMatch } from "src/utils/expect-types-match"
import { point } from "../common"
import { distance } from "../units"

export const pcb_keepout = z.union([
z.object({
export const pcb_keepout = z
.object({
type: z.literal("pcb_keepout"),
shape: z.literal("rect"),
pcb_keepout_id: getZodPrefixedIdWithDefault("pcb_keepout"),
center: point,
width: distance,
height: distance,
pcb_keepout_id: z.string(),
layers: z.array(z.string()), // Specify layers where the keepout applies
description: z.string().optional(), // Optional description of the keepout
}),
z.object({
type: z.literal("pcb_keepout"),
shape: z.literal("circle"),
pcb_keepout_id: getZodPrefixedIdWithDefault("pcb_keepout"),
center: point,
radius: distance,
layers: z.array(z.string()), // Specify layers where the keepout applies
description: z.string().optional(), // Optional description of the keepout
}),
])

export type PcbKeepoutInput = z.input<typeof pcb_keepout>
type InferredPcbKeepout = z.infer<typeof pcb_keepout>

/**
* Defines a keepout area on the PCB, which can be either a rectangle or a circle.
* The keepout area is specified for one or more layers, and an optional description can be provided.
*/
export interface PcbKeepout {
type: "pcb_keepout"
pcb_keepout_id: string
center: Point
layers: string[]
description?: string
}

// Rectangular Keepout
export interface PcbKeepoutRect extends PcbKeepout {
shape: "rect"
width: Distance
height: Distance
}

// Circular Keepout
export interface PcbKeepoutCircle extends PcbKeepout {
shape: "circle"
radius: Distance
}

/**
* @deprecated use PcbKeepout
*/
export type PCBKeepout = PcbKeepout
})
.or(
z.object({
type: z.literal("pcb_keepout"),
shape: z.literal("circle"),
center: point,
radius: distance,
pcb_keepout_id: z.string(),
layers: z.array(z.string()), // Specify layers where the keepout applies
description: z.string().optional(), // Optional description of the keepout
}),
)

expectTypesMatch<PcbKeepout, InferredPcbKeepout>(true)
export type PCBKeepoutInput = z.input<typeof pcb_keepout>
export type PCBKeepout = z.infer<typeof pcb_keepout>
2 changes: 1 addition & 1 deletion src/pcb/pcb_silkscreen_pill.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { z } from "zod"
import { point, type Point, getZodPrefixedIdWithDefault } from "src/common"
import { layer_ref, type LayerRef } from "src/properties/layer_ref"
import { layer_ref, type LayerRef } from "src/pcb/properties/layer_ref"
import { length, type Length } from "src/units"
import { expectTypesMatch } from "src/utils/expect-types-match"

Expand Down
2 changes: 1 addition & 1 deletion src/pcb/pcb_silkscreen_rect.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { z } from "zod"
import { point, type Point, getZodPrefixedIdWithDefault } from "src/common"
import { layer_ref, type LayerRef } from "src/properties/layer_ref"
import { layer_ref, type LayerRef } from "src/pcb/properties/layer_ref"
import { length, type Length } from "src/units"
import { expectTypesMatch } from "src/utils/expect-types-match"

Expand Down
9 changes: 7 additions & 2 deletions src/pcb/pcb_silkscreen_text.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { z } from "zod"
import { point, type Point, getZodPrefixedIdWithDefault } from "src/common"
import { layer_ref, type LayerRef } from "src/properties/layer_ref"
import { layer_ref, type LayerRef } from "src/pcb/properties/layer_ref"
import { distance, type Length } from "src/units"
import { expectTypesMatch } from "src/utils/expect-types-match"

Expand Down Expand Up @@ -35,7 +35,12 @@ export interface PcbSilkscreenText {
text: string
layer: LayerRef
anchor_position: Point
anchor_alignment: "center" | "top_left" | "top_right" | "bottom_left" | "bottom_right"
anchor_alignment:
| "center"
| "top_left"
| "top_right"
| "bottom_left"
| "bottom_right"
}

/**
Expand Down
57 changes: 29 additions & 28 deletions src/pcb/pcb_smtpad.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
import { z } from "zod"
import { distance, type Distance, getZodPrefixedIdWithDefault } from "src/units"
import { layer_ref, type LayerRef } from "src/properties/layer_ref"
import { layer_ref, type LayerRef } from "src/pcb/properties/layer_ref"
import { expectTypesMatch } from "src/utils/expect-types-match"

export const pcb_smtpad = z.union([
z.object({
pcb_smtpad_id: getZodPrefixedIdWithDefault("pcb_smtpad"),
type: z.literal("pcb_smtpad"),
shape: z.literal("circle"),
x: distance,
y: distance,
radius: z.number(),
layer: layer_ref,
port_hints: z.array(z.string()).optional(),
pcb_component_id: z.string().optional(),
pcb_port_id: z.string().optional(),
}),
z.object({
pcb_smtpad_id: getZodPrefixedIdWithDefault("pcb_smtpad"),
type: z.literal("pcb_smtpad"),
shape: z.literal("rect"),
x: distance,
y: distance,
width: z.number(),
height: z.number(),
layer: layer_ref,
port_hints: z.array(z.string()).optional(),
pcb_component_id: z.string().optional(),
pcb_port_id: z.string().optional(),
}),
])
export const pcb_smtpad = z
.union([
z.object({
pcb_smtpad_id: getZodPrefixedIdWithDefault("pcb_smtpad"),
type: z.literal("pcb_smtpad"),
shape: z.literal("circle"),
x: distance,
y: distance,
radius: z.number(),
layer: layer_ref,
port_hints: z.array(z.string()).optional(),
pcb_component_id: z.string().optional(),
pcb_port_id: z.string().optional(),
}),
z.object({
pcb_smtpad_id: getZodPrefixedIdWithDefault("pcb_smtpad"),
type: z.literal("pcb_smtpad"),
shape: z.literal("rect"),
x: distance,
y: distance,
width: z.number(),
height: z.number(),
layer: layer_ref,
port_hints: z.array(z.string()).optional(),
pcb_component_id: z.string().optional(),
pcb_port_id: z.string().optional(),
}),
])
.describe("Defines an SMT pad on the PCB")

export type PCBSMTPadInput = z.input<typeof pcb_smtpad>
Expand Down
2 changes: 1 addition & 1 deletion src/pcb/pcb_text.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { z } from "zod"
import { point, type Point, getZodPrefixedIdWithDefault } from "src/common"
import { layer_ref, type LayerRef } from "src/properties/layer_ref"
import { layer_ref, type LayerRef } from "src/pcb/properties/layer_ref"
import { length, type Length } from "src/units"
import { expectTypesMatch } from "src/utils/expect-types-match"

Expand Down
2 changes: 1 addition & 1 deletion src/pcb/pcb_trace.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { z } from "zod"
import { distance, type Distance, getZodPrefixedIdWithDefault } from "src/units"
import { layer_ref, type LayerRef } from "src/properties/layer_ref"
import { layer_ref, type LayerRef } from "src/pcb/properties/layer_ref"
import { expectTypesMatch } from "src/utils/expect-types-match"

export const pcb_trace = z
Expand Down
Loading

0 comments on commit 03edfc4

Please sign in to comment.