feat(core): project file save/open with schema check (plan B3)

- serializeProject / parseProject for the .pzr.json format (spec §8,
  schemaVersion 1). Parsing refuses other versions and damaged piers, and
  names the first broken pier.
- The 3D model is not stored; it is regenerated on open.
- Tests: save → open equality without the source workbook; regenerated
  models equal for 20 piers; version and damage refusals.

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
This commit is contained in:
ypshimandClaude Haiku 5.5 committed 2026-10-22 09:39:44 +09:00
1 parent 9e1e25f8fe
commit 5ce6d0e2f1
2 files changed
+108

No files matched your search

+59
View File
@@ -0,0 +1,59 @@
import type { PierInput } from './types';
/** Project file schema (spec §8). Bump only with a migration. */
export const SCHEMA_VERSION = 1;
export interface ProjectInfo {
name: string;
bridge: string;
client: string;
author: string;
crs: string;
memo: string;
}
export interface ProjectPier {
id: string;
/** Input as read from the spreadsheet. The 3D model is not stored; it is regenerated on open (spec §8). */
input: PierInput;
}
export interface ProjectFile {
schemaVersion: number;
project: ProjectInfo;
source: { fileName: string; sheets: string[] };
piers: ProjectPier[];
history: { version: string; note: string; at: string }[];
}
/** Serialises a project to the pretty-printed JSON stored in <교량명>_교각설계.pzr.json. */
export function serializeProject(p: ProjectFile): string {
return `${JSON.stringify(p, null, 2)}\n`;
}
/**
* Parses and checks a project file. Throws with a message that names the first problem, so a damaged file is
* refused instead of being half-loaded.
*/
export function parseProject(text: string): ProjectFile {
let raw: unknown;
try {
raw = JSON.parse(text);
} catch {
throw new Error('프로젝트 파일이 JSON으로 읽히지 않습니다.');
}
const obj = raw as Partial<ProjectFile>;
if (!obj || typeof obj !== 'object') throw new Error('프로젝트 파일 형식이 아닙니다.');
if (obj.schemaVersion !== SCHEMA_VERSION) {
throw new Error(`지원하지 않는 파일 버전입니다 (schemaVersion ${String(obj.schemaVersion)}, 지원: ${SCHEMA_VERSION}).`);
}
if (!Array.isArray(obj.piers)) throw new Error('piers 목록이 없습니다.');
obj.piers.forEach((p, i) => {
if (!p || typeof p.id !== 'string' || !p.input) throw new Error(`교각 ${i + 1}: id 또는 input이 없습니다.`);
const input = p.input;
if (typeof input.name !== 'string' || !Array.isArray(input.ips) || !Array.isArray(input.bearings)) {
throw new Error(`교각 ${i + 1}(${String(input.name)}): 입력 형식이 맞지 않습니다.`);
}
});
return obj as ProjectFile;
}