(text: string)
| 464 | /** Parse TOML text into a portable config object plus its version. Throws on |
| 465 | * malformed TOML — callers fall back to the last good config. */ |
| 466 | export function deserializeConfig(text: string): { version: number; portable: AppConfigPortable } { |
| 467 | const parsed = parseToml(text) as Record<string, unknown> |
| 468 | const version = |
| 469 | typeof parsed.config_version === 'number' ? parsed.config_version : 0 |
| 470 | const portable: AppConfigPortable = {} |
| 471 | |
| 472 | for (const [key, map] of Object.entries(SCALAR_FIELDS)) { |
| 473 | if (!map) continue |
| 474 | const section = parsed[map.section] |
| 475 | if (!section || typeof section !== 'object') continue |
| 476 | const raw = (section as Record<string, unknown>)[map.tomlKey] |
| 477 | if (raw === undefined) continue |
| 478 | if (NULLABLE_FIELDS.has(key as PortablePrefKey) && raw === '') { |
| 479 | portable[key as PortablePrefKey] = null |
| 480 | continue |
| 481 | } |
| 482 | portable[key as PortablePrefKey] = raw |
| 483 | } |
| 484 | |
| 485 | for (const [key, def] of Object.entries(MAP_TABLE_FIELDS)) { |
| 486 | if (!def) continue |
| 487 | const value = parsed[def.table] |
| 488 | if (value && typeof value === 'object' && !Array.isArray(value)) { |
| 489 | portable[key as PortablePrefKey] = value |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | return { version, portable: migrateConfig(version, portable) } |
| 494 | } |
| 495 | |
| 496 | /** Forward-migrate an older config layout. No migrations needed yet (v1). */ |
| 497 | function migrateConfig(_version: number, portable: AppConfigPortable): AppConfigPortable { |
no test coverage detected