Skip to content

Commit

Permalink
more refactoring to better defined props, split out capacitor and res…
Browse files Browse the repository at this point in the history
…istor, add jumper, add pullupFor, decouplingFor (#35)
  • Loading branch information
seveibar authored Aug 31, 2024
1 parent 3818891 commit f5036eb
Show file tree
Hide file tree
Showing 11 changed files with 230 additions and 37 deletions.
39 changes: 38 additions & 1 deletion lib/common/cadModel.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,64 @@
import { z } from "zod"
import { point3 } from "./point3"
import { expectTypesMatch } from "lib/typecheck"

export const rotationPoint3 = z.object({
x: z.union([z.number(), z.string()]),
y: z.union([z.number(), z.string()]),
z: z.union([z.number(), z.string()]),
})

export interface CadModelBase {
rotationOffset?:
| number
| { x: number | string; y: number | string; z: number | string }
positionOffset?: {
x: number | string
y: number | string
z: number | string
}
size?: { x: number | string; y: number | string; z: number | string }
}

export const cadModelBase = z.object({
rotationOffset: z.number().or(rotationPoint3).optional(),
positionOffset: point3.optional(),
size: point3.optional(),
})

expectTypesMatch<CadModelBase, z.input<typeof cadModelBase>>(true)

export interface CadModelStl extends CadModelBase {
stlUrl: string
}
export const cadModelStl = cadModelBase.extend({
stlUrl: z.string(),
})

export interface CadModelObj extends CadModelBase {
objUrl: string
mtlUrl?: string
}
export const cadModelObj = cadModelBase.extend({
objUrl: z.string(),
mtlUrl: z.string().optional(),
})

export interface CadModelJscad extends CadModelBase {
jscad: Record<string, any>
}
export const cadModelJscad = cadModelBase.extend({
jscad: z.any(),
jscad: z.record(z.any()),
})

export type CadModelProp = string | CadModelStl | CadModelObj | CadModelJscad

export const cadModelProp = z.union([
z.string(),
cadModelStl,
cadModelObj,
cadModelJscad,
])

type InferredCadModelProp = z.input<typeof cadModelProp>
expectTypesMatch<CadModelProp, InferredCadModelProp>(true)
60 changes: 54 additions & 6 deletions lib/common/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,24 @@ import {
type AnySoupElementInput,
distance,
layer_ref,
type LayerRef,
type LayerRefInput,
rotation,
supplier_name,
} from "@tscircuit/soup"
import { point3 } from "./point3"
import { cadModelJscad, cadModelObj, cadModelStl } from "./cadModel"
import { footprintProp } from "./footprintProp"
import {
cadModelJscad,
cadModelObj,
cadModelProp,
cadModelStl,
type CadModelJscad,
type CadModelObj,
type CadModelProp,
type CadModelStl,
} from "./cadModel"
import { footprintProp, type Footprint } from "./footprintProp"
import { expectTypesMatch } from "lib/typecheck"

export const pcbLayoutProps = z.object({
pcbX: distance,
Expand All @@ -17,6 +29,19 @@ export const pcbLayoutProps = z.object({
layer: layer_ref.optional(),
})

export interface CommonLayoutProps {
pcbX?: string | number
pcbY?: string | number
pcbRotation?: string | number

schX?: string | number
schY?: string | number
schRotation?: string | number

layer?: LayerRefInput
footprint?: Footprint
}

export const commonLayoutProps = z.object({
pcbX: distance.optional(),
pcbY: distance.optional(),
Expand All @@ -27,22 +52,45 @@ export const commonLayoutProps = z.object({
layer: layer_ref.optional(),
footprint: footprintProp.optional(),
})
export type CommonLayoutProps = z.input<typeof commonLayoutProps>

type InferredCommonLayoutProps = z.input<typeof commonLayoutProps>
expectTypesMatch<CommonLayoutProps, InferredCommonLayoutProps>(true)

export type SupplierName =
| "jlcpcb"
| "macrofab"
| "pcbway"
| "digikey"
| "mouser"
| "lcsc"
export interface SupplierProps {
supplierPartNumbers?: { [k in SupplierName]?: string[] }
}
export const supplierProps = z.object({
supplierPartNumbers: z.record(supplier_name, z.array(z.string())).optional(),
})
export type SupplierProps = z.input<typeof supplierProps>

expectTypesMatch<SupplierProps, z.input<typeof supplierProps>>(true)

export interface CommonComponentProps extends CommonLayoutProps {
name: string
supplierPartNumbers?: SupplierProps["supplierPartNumbers"]
cadModel?: CadModelProp
children?: any
symbolName?: string
}

export const commonComponentProps = commonLayoutProps
.merge(supplierProps)
.extend({
name: z.string(),
cadModel: z.union([cadModelStl, cadModelObj, cadModelJscad]).optional(),
cadModel: cadModelProp.optional(),
children: z.any().optional(),
symbolName: z.string().optional(),
})
export type CommonComponentProps = z.input<typeof commonComponentProps>

type InferredCommonComponentProps = z.input<typeof commonComponentProps>
expectTypesMatch<CommonComponentProps, InferredCommonComponentProps>(true)

export const lrPins = ["pin1", "left", "pin2", "right"] as const
export const lrPolarPins = [
Expand Down
24 changes: 24 additions & 0 deletions lib/common/schematicPinStyle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { z } from "zod"
import { distance } from "@tscircuit/soup"
import { expectTypesMatch } from "lib/typecheck"

export type SchematicPinStyle = Record<
string,
{
leftMargin?: number | string
rightMargin?: number | string
topMargin?: number | string
bottomMargin?: number | string
}
>

export const schematicPinStyle = z.record(
z.object({
leftMargin: distance.optional(),
rightMargin: distance.optional(),
topMargin: distance.optional(),
bottomMargin: distance.optional(),
}),
)

expectTypesMatch<SchematicPinStyle, z.input<typeof schematicPinStyle>>(true)
32 changes: 32 additions & 0 deletions lib/components/capacitor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { z } from "zod"
import { capacitance } from "@tscircuit/soup"
import {
commonComponentProps,
lrPins,
lrPolarPins,
type CommonComponentProps,
} from "lib/common/layout"
import { expectTypesMatch } from "lib/typecheck"

export interface CapacitorProps extends CommonComponentProps {
capacitance: number | string

decouplingFor?: string
decouplingTo?: string

bypassFor?: string
bypassTo?: string
}

export const capacitorProps = commonComponentProps.extend({
capacitance,

decouplingFor: z.string().optional(),
decouplingTo: z.string().optional(),

bypassFor: z.string().optional(),
bypassTo: z.string().optional(),
})
export const capacitorPins = lrPolarPins

expectTypesMatch<CapacitorProps, z.input<typeof capacitorProps>>(true)
12 changes: 2 additions & 10 deletions lib/components/chip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,14 @@ import { z } from "zod"
import { distance } from "@tscircuit/soup"
import { commonComponentProps } from "lib/common/layout"
import { schematicPortArrangement } from "lib/common/schematicPinDefinitions"
import { schematicPinStyle } from "lib/common/schematicPinStyle"

export const chipProps = commonComponentProps.extend({
manufacturerPartNumber: z.string().optional(),
pinLabels: z.record(z.number().or(z.string()), z.string()).optional(),

schPortArrangement: schematicPortArrangement.optional(),
schPinStyle: z
.record(
z.object({
leftMargin: distance.optional(),
rightMargin: distance.optional(),
topMargin: distance.optional(),
bottomMargin: distance.optional(),
}),
)
.optional(),
schPinStyle: schematicPinStyle.optional(),
schPinSpacing: distance.optional(),
schWidth: distance.optional(),
schHeight: distance.optional(),
Expand Down
32 changes: 32 additions & 0 deletions lib/components/jumper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { z } from "zod"
import {
commonComponentProps,
type CommonComponentProps,
} from "lib/common/layout"
import {
schematicPinStyle,
type SchematicPinStyle,
} from "lib/common/schematicPinStyle"
import { distance } from "@tscircuit/soup"
import { expectTypesMatch } from "lib/typecheck"

export interface JumperProps extends CommonComponentProps {
manufacturerPartNumber?: string
pinLabels?: Record<number | string, string>
schPinStyle?: SchematicPinStyle
schPinSpacing?: number | string
schWidth?: number | string
schHeight?: number | string
}

export const jumperProps = commonComponentProps.extend({
manufacturerPartNumber: z.string().optional(),
pinLabels: z.record(z.number().or(z.string()), z.string()).optional(),
schPinStyle: schematicPinStyle.optional(),
schPinSpacing: distance.optional(),
schWidth: distance.optional(),
schHeight: distance.optional(),
})

type InferredJumperProps = z.input<typeof jumperProps>
expectTypesMatch<JumperProps, InferredJumperProps>(true)
30 changes: 30 additions & 0 deletions lib/components/resistor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { z } from "zod"
import { resistance } from "@tscircuit/soup"
import {
commonComponentProps,
lrPins,
type CommonComponentProps,
} from "lib/common/layout"
import { expectTypesMatch } from "lib/typecheck"

export interface ResistorProps extends CommonComponentProps {
resistance: number | string
pullupFor?: string
pullupTo?: string
pulldownFor?: string
pulldownTo?: string
}

export const resistorProps = commonComponentProps.extend({
resistance,

pullupFor: z.string().optional(),
pullupTo: z.string().optional(),

pulldownFor: z.string().optional(),
pulldownTo: z.string().optional(),
})
export const resistorPins = lrPins

type InferredResistorProps = z.input<typeof resistorProps>
expectTypesMatch<ResistorProps, InferredResistorProps>(true)
19 changes: 3 additions & 16 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,10 @@ export * from "./common/schematicPinDefinitions"

export * from "./components/board"
export * from "./components/chip"
export * from "./components/jumper"

export const supplierProps = z.object({
supplierPartNumbers: z.record(supplier_name, z.array(z.string())).optional(),
})
export type SupplierProps = z.input<typeof supplierProps>

export const resistorProps = commonComponentProps.extend({
resistance,
})
export const resistorPins = lrPins
export type ResistorProps = z.input<typeof resistorProps>

export const capacitorProps = commonComponentProps.extend({
capacitance,
})
export const capacitorPins = lrPolarPins
export type CapacitorProps = z.input<typeof capacitorProps>
export * from "./components/resistor"
export * from "./components/capacitor"

export const inductorProps = commonComponentProps.extend({
inductance,
Expand Down
6 changes: 4 additions & 2 deletions lib/typecheck.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const expectTypesMatch = <T1, T2>(
shouldBe: T1 extends T2 ? (T2 extends T1 ? true : false) : false,
import type { TypeEqual } from "ts-expect"

export const expectTypesMatch = <const T1, const T2>(
shouldBe: TypeEqual<T1, T2>,
): void => {}
12 changes: 10 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"expect-type": "^0.20.0",
"madge": "^8.0.0",
"react": "^18.3.1",
"ts-expect": "^1.3.0",
"tsup": "^8.0.2",
"tsx": "^4.10.2",
"typescript": "^5.4.5",
Expand Down

0 comments on commit f5036eb

Please sign in to comment.