Skip to content

Commit

Permalink
Use the auto-generated rust types and auto-generate typeguards
Browse files Browse the repository at this point in the history
Signed-off-by: Dimitris Zervas <[email protected]>
  • Loading branch information
dzervas committed May 24, 2024
1 parent a9fc641 commit 3ddb453
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 467 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
},
"engines": {
"node": ">=20.13.1",
"pnpm": ">=9.1.2"
"pnpm": ">=9.1.0"
}
}
}
7 changes: 4 additions & 3 deletions packages/cadmium/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
"scripts": {
"dev": "onchange 'src/**/*.rs' -- pnpm run build:dev",
"build:dev": "cargo check && pnpm run build:wasm",
"build:wasm": "wasm-pack build --target web && ./patchpackage.cjs",
"build:wasm": "wasm-pack build --target web && ./patchpackage.cjs && ts-auto-guard pkg/cadmium.d.ts --export-all",
"test": "cargo test"
},
"devDependencies": {
"onchange": "^7.1.0"
"onchange": "^7.1.0",
"ts-auto-guard": "^4.2.0"
}
}
}
13 changes: 13 additions & 0 deletions packages/cadmium/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
"moduleResolution": "bundler"
}
}
6 changes: 3 additions & 3 deletions packages/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"dependencies": {
"cadmium": "workspace:*",
"svelte": "^4.2.12",
"three": "^0.162.0"
"svelte": "^4.2.12",
"three": "^0.162.0"
}
}
}
161 changes: 12 additions & 149 deletions packages/shared/projectUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import type {
Entity,
ExtrusionHistoryStep,
HistoryStep,
Message,
MessageHistory,
PlaneHistoryStep,
PointHistoryStep,
Expand All @@ -26,23 +25,8 @@ import type {
WithTarget,
WorkBench
} from "./types"
import type { Realization as WasmRealization } from "cadmium"
import {
isDeleteArcs,
isDeleteCircles,
isDeleteLines,
isNewCircleBetweenPoints,
isNewExtrusion,
isNewLineOnSketch,
isNewPointOnSketch2,
isNewRectangleBetweenPoints,
isNewSketchOnPlane,
isRenameStep,
isRenameWorkbench,
isSetSketchPlane,
isUpdateExtrusion
} from "./typeGuards"
// import { _isDevelopment } from "../+layout"
import type { Realization as WasmRealization, Message } from "cadmium"
import { isMessage } from "cadmium/cadmium.guard"

// prettier-ignore
const log = (function () { const context = "[projectUtils.ts]"; const color = "aqua"; return Function.prototype.bind.call(console.log, console, `%c${context}`, `font-weight:bold;color:${color};`) })()
Expand Down Expand Up @@ -71,6 +55,11 @@ export function arraysEqual(a: any[], b: any[]) {
}

function sendWasmMessage(message: Message) {
if (!isMessage(message)) {
log("[sendWasmMessage][isMessage] message is not valid:", message)
return
}

let wp = get(wasmProject)
const messageStr = JSON.stringify(message)
log("[sendWasmMessage] sending message:", message)
Expand Down Expand Up @@ -98,24 +87,24 @@ export function updateExtrusion(extrusionId: string, sketchId: string, length: n
extrusion_id: extrusionId
}
}
const isValid = checkWasmMessage(message)
const isValid = isMessage(message)
const hasFaceIds = notEmpty(message.UpdateExtrusion.face_ids)
if (isValid) {
sendWasmMessage(message)
workbenchIsStale.set(true)
if (hasFaceIds) {
log("[updateExtrusion]", "[checkWasmMessage]", "is valid,", "sending message...", message)
log("[updateExtrusion]", "[isMessage]", "is valid,", "sending message...", message)
// sendWasmMessage(message)
} else
log(
"[updateExtrusion]",
"[checkWasmMessage]",
"[isMessage]",
"is valid,",
"but face_ids is empty,",
"NOT sending message:",
message
)
} else log("[updateExtrusion]", "[checkWasmMessage]", "is bogus,", "abort message send!", message)
} else log("[updateExtrusion]", "[isMessage]", "is bogus,", "abort message send!", message)

// sendWasmMessage(message)

Expand All @@ -131,7 +120,6 @@ export function setSketchPlane(sketchId: string, planeId: string) {
plane_id: planeId
}
}
checkWasmMessage(message)
sendWasmMessage(message)
workbenchIsStale.set(true)
}
Expand All @@ -144,7 +132,6 @@ export function newSketchOnPlane() {
sketch_name: "" // a sensible name will be generated by the rust code
}
}
checkWasmMessage(message)
sendWasmMessage(message)
workbenchIsStale.set(true)
}
Expand All @@ -154,7 +141,7 @@ export function newExtrusion() {
// log("[newExtrusion] workbench:", workbench)
// log("[newExtrusion] bench:", bench)

let sketchId = null
let sketchId = ""
for (let step of bench.history) {
if (step.data.type === "Sketch") {
sketchId = step.unique_id
Expand All @@ -180,7 +167,6 @@ export function newExtrusion() {
// we check for face_ids: [] to contain numbers but we send an empty array
// todo: maybe change isNewExtrusion? although with the rust api it is possible to send an array of faceids so we ought to check them...
// probably best to alter isNewExtrusion to allow an empty array or a number[]
checkWasmMessage(message)
sendWasmMessage(message)
workbenchIsStale.set(true)
}
Expand Down Expand Up @@ -223,7 +209,6 @@ function deleteLines(workbenchIdx: number, sketchIdx: string, lineIds: number[])
line_ids: lineIds
}
}
checkWasmMessage(message)
sendWasmMessage(message)
}

Expand All @@ -235,7 +220,6 @@ function deleteArcs(workbenchIdx: number, sketchIdx: string, arcIds: number[]) {
arc_ids: arcIds
}
}
checkWasmMessage(message)
sendWasmMessage(message)
}

Expand All @@ -247,7 +231,6 @@ function deleteCircles(workbenchIdx: number, sketchIdx: string, circleIds: numbe
circle_ids: circleIds
}
}
checkWasmMessage(message)
sendWasmMessage(message)
}

Expand All @@ -261,7 +244,6 @@ export function addRectangleBetweenPoints(sketchIdx: string, point1: number, poi
end_id: point2
}
}
checkWasmMessage(message)
sendWasmMessage(message)
workbenchIsStale.set(true)
}
Expand All @@ -286,7 +268,6 @@ export function addCircleBetweenPoints(sketchIdx: string, point1: string, point2
edge_id: parseInt(point2, 10)
}
}
checkWasmMessage(message)
sendWasmMessage(message)
workbenchIsStale.set(true)
}
Expand All @@ -300,7 +281,6 @@ export function addLineToSketch(sketchIdx: string, point1: number, point2: numbe
end_point_id: point2
}
}
checkWasmMessage(message)
sendWasmMessage(message)
workbenchIsStale.set(true)
}
Expand All @@ -316,7 +296,6 @@ export function addPointToSketch(sketchIdx: string, point: Vector2Like, hidden:
hidden: hidden
}
}
checkWasmMessage(message)
const reply = sendWasmMessage(message)
// log("[addPointToSketch sendWasmMessage]", "message:", message, "reply:", reply)

Expand Down Expand Up @@ -351,7 +330,6 @@ export function renameStep(stepIdx: number, newName: string): void {
new_name: newName
}
}
checkWasmMessage(message)
sendWasmMessage(message)
}

Expand All @@ -363,7 +341,6 @@ export function renameWorkbench(newName: string): void {
new_name: newName
}
}
checkWasmMessage(message)
sendWasmMessage(message)
}

Expand Down Expand Up @@ -522,117 +499,3 @@ function reduceToInts(data: string[], errorCallback: (id: any) => void): number[
function notEmpty(array: unknown[]): boolean {
return array && Array.isArray(array) && array.length > 0
}

function checkWasmMessage(message: Message, abort = true, logError = true): boolean {
const key = Object.keys(message)[0]
const command = message[key as keyof Message]
if (!command) {
console.error("[projectUtils.ts] [checkWasmMessage]", "messageType not found:", key, message)
return false
}
log("[checkWasmMessage]", "checking...", key, message)

switch (key) {
case "UpdateExtrusion":
if (!isUpdateExtrusion(command)) {
logOrAbort()
return false
}
return true

case "SetSketchPlane":
if (!isSetSketchPlane(command)) {
logOrAbort()
return false
}
return true

case "NewSketchOnPlane":
if (!isNewSketchOnPlane(command)) {
logOrAbort()
return false
}
return true

case "NewExtrusion":
if (!isNewExtrusion(command)) {
logOrAbort()
return false
}
return true

case "DeleteLines":
if (!isDeleteLines(command)) {
logOrAbort()
return false
}
return true

case "DeleteArcs":
if (!isDeleteArcs(command)) {
logOrAbort()
return false
}
return true

case "DeleteCircles":
if (!isDeleteCircles(command)) {
logOrAbort()
return false
}
return true

case "NewRectangleBetweenPoints":
if (!isNewRectangleBetweenPoints(command)) {
logOrAbort()
return false
}
return true

case "NewCircleBetweenPoints":
if (!isNewCircleBetweenPoints(command)) {
logOrAbort()
return false
}
return true

case "NewLineOnSketch":
if (!isNewLineOnSketch(command)) {
logOrAbort()
return false
}
return true

case "NewPointOnSketch2":
if (!isNewPointOnSketch2(command)) {
logOrAbort()
return false
}
return true

case "RenameStep":
if (!isRenameStep(command)) {
logOrAbort()
return false
}
return true

case "RenameWorkbench":
if (!isRenameWorkbench(command)) {
logOrAbort()
return false
}
return true

default:
console.error("[projectUtils.ts] [checkWasmMessage]", "messageType typeGuard not implemented:", key)
return false
}

function logOrAbort() {
const error = `[${key}] message failed typecheck:`
if (logError) console.error("[projectUtils.ts]", error, message)
// if (abort && _isDevelopment()) throw new Error(`"[projectUtils.ts]" ${error}`)
return false
}
}
Loading

0 comments on commit 3ddb453

Please sign in to comment.