feat(config-editor): add RecordValue serializer with tests

This commit is contained in:
panFD
2026-06-21 00:05:53 +08:00
parent c0755b3b8d
commit 7bb5f8bacc
3 changed files with 77 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
import { RecordValue, isScalar } from './recordValue';
export function serializeValue(v: RecordValue): string {
switch (v.kind) {
case 'num': return String(v.value);
case 'str': return JSON.stringify(v.value);
case 'bool': return String(v.value);
case 'enumRef': return `${v.qualifier}.${v.member}`;
case 'speed': return `AtkSpeedSet[AtkSpeedLv.${v.level}].cd`;
case 'arr': return `[${v.items.map(serializeValue).join(',')}]`;
case 'obj': {
const body = Object.entries(v.props).map(([k, val]) => `${k}:${serializeValue(val)}`).join(',');
return `{${body}}`;
}
case 'raw': return v.text;
}
}
/** 序列化一条顶层记录。首行无缩进(缩进由调用处保留的 trivia 提供);嵌套字段 8 空格续行;闭合 4 空格。 */
export function serializeEntry(key: string, obj: RecordValue): string {
if (obj.kind !== 'obj') return `${key}:${serializeValue(obj)},`;
const props = obj.props;
const keys = Object.keys(props);
const scalars = keys.filter(k => isScalar(props[k]));
const nested = keys.filter(k => !isScalar(props[k]));
const scalarTxt = scalars.map(k => `${k}:${serializeValue(props[k])}`).join(',');
if (nested.length === 0) return `${key}:{${scalarTxt}},`;
const lines = [`${key}:{${scalarTxt},`];
for (const k of nested) lines.push(` ${k}:${serializeValue(props[k])},`);
lines.push(' },');
return lines.join('\n');
}