| 1 | // The shape pass of applyOps — what serde does for core/src/ops.rs's Op |
| 2 | // enum: required fields, the kinds of value each takes, and the difference |
| 3 | // between an absent key and an explicit null where it matters (`after` |
| 4 | // absent appends, `after: null` puts first; `accent`, `arrive`, `talk`, |
| 5 | // `step` and `items` null clear). The |
| 6 | // result is one op in the enum's shape: absent fields undefined, nulls |
| 7 | // kept only where they mean something. Every check here is one the core |
| 8 | // makes too, so a batch this module accepts the core accepts. |
| 9 | |
| 10 | const fail = msg => { throw new Error(msg) } |
| 11 | |
| 12 | const isObject = v => v !== null && typeof v === 'object' && !Array.isArray(v) |
| 13 | const isInt = v => Number.isInteger(v) |
| 14 | |
| 15 | function field (o, key, check, what) { |
| 16 | const v = o[key] |
| 17 | if (v === undefined) fail(`missing field \`${key}\``) |
| 18 | if (!check(v)) fail(`invalid type for \`${key}\`, expected ${what}`) |
| 19 | return v |
| 20 | } |
| 21 | function optional (o, key, check, what) { |
| 22 | const v = o[key] |
| 23 | if (v === undefined || v === null) return undefined |
| 24 | if (!check(v)) fail(`invalid type for \`${key}\`, expected ${what}`) |
| 25 | return v |
| 26 | } |
| 27 | /** undefined when absent, null when explicitly null (so `after: null` and `accent: null` mean something). */ |
| 28 | function nullable (o, key, check, what) { |
| 29 | const v = o[key] |
| 30 | if (v === undefined) return undefined |
| 31 | if (v === null) return null |
| 32 | if (!check(v)) fail(`invalid type for \`${key}\`, expected ${what}`) |
| 33 | return v |
| 34 | } |
| 35 | const str = (o, k) => field(o, k, v => typeof v === 'string', 'a string') |
| 36 | const optStr = (o, k) => optional(o, k, v => typeof v === 'string', 'a string') |
| 37 | const nullStr = (o, k) => nullable(o, k, v => typeof v === 'string', 'a string') |
| 38 | const optNum = (o, k) => optional(o, k, v => typeof v === 'number' && Number.isFinite(v), 'a number') |
| 39 | // Any number, finite or not: the model refuses a step that is not above 0, |
| 40 | // with the core's message (serde reads any JSON number as an f64). |
| 41 | const nullNum = (o, k) => nullable(o, k, v => typeof v === 'number', 'a number') |
| 42 | const optProps = (o, k) => optional(o, k, isObject, 'a map') |
| 43 | |
| 44 | function boxSpec (v) { |
| 45 | if (!isObject(v)) fail('invalid type for `box`, expected a map') |
| 46 | for (const k of ['x', 'y', 'w', 'h']) field(v, k, n => typeof n === 'number' && Number.isFinite(n), 'a number') |
| 47 | return { x: v.x, y: v.y, w: v.w, h: v.h } |
| 48 | } |
| 49 | |
| 50 | /** One op as the core's enum: absent fields undefined, explicit nulls kept where they mean something. */ |
| 51 | export function parseOp (raw) { |
| 52 | if (!isObject(raw)) fail('an op must be an object') |
| 53 | const op = raw.op |
| 54 | if (typeof op !== 'string') fail('missing field `op`') |
| 55 | switch (op) { |
| 56 | case 'set_meta': { |
| 57 | let fonts = nullable(raw, 'fonts', isObject, 'a map') |
| 58 | if (fonts) fonts = { heading: str(fonts, 'heading'), body: str(fonts, 'body') } |
| 59 | const fontFiles = nullable(raw, 'fontFiles', Array.isArray, 'a list') |
| 60 | return { op, title: optStr(raw, 'title'), theme: optStr(raw, 'theme'), accent: nullStr(raw, 'accent'), fonts, fontFiles, arrive: nullStr(raw, 'arrive') } |
| 61 | } |
| 62 | case 'add_slide': |
| 63 | return { op, id: str(raw, 'id'), after: nullStr(raw, 'after'), layout: str(raw, 'layout'), template: optStr(raw, 'template') } |
| 64 | case 'remove_slide': |
| 65 | case 'remove_asset': |
| 66 | return { op, id: str(raw, 'id') } |
| 67 | case 'move_slide': |
| 68 | return { op, id: str(raw, 'id'), after: optStr(raw, 'after') } |
| 69 | case 'duplicate_slide': { |
| 70 | const ids = field(raw, 'new_block_ids', v => Array.isArray(v) && v.every(x => typeof x === 'string'), 'a list of strings') |
| 71 | return { op, id: str(raw, 'id'), new_id: str(raw, 'new_id'), new_block_ids: ids } |
| 72 | } |
| 73 | case 'set_slide': { |
| 74 | let background = optional(raw, 'background', isObject, 'a map') |
| 75 | if (background) background = { kind: str(background, 'kind'), value: optStr(background, 'value') ?? '' } |
| 76 | return { |
| 77 | op, id: str(raw, 'id'), layout: optStr(raw, 'layout'), template: optStr(raw, 'template'), background, |
| 78 | phone: optStr(raw, 'phone'), transition: optStr(raw, 'transition'), talk: nullStr(raw, 'talk') |
| 79 | } |
| 80 | } |
| 81 | case 'set_notes': |
| 82 | return { op, id: str(raw, 'id'), markdown: str(raw, 'markdown') } |
| 83 | case 'add_block': { |
| 84 | const bx = raw.box === undefined || raw.box === null ? undefined : boxSpec(raw.box) |
| 85 | return { |
| 86 | op, slide: str(raw, 'slide'), id: str(raw, 'id'), type: str(raw, 'type'), props: optProps(raw, 'props') ?? {}, |
| 87 | region: optStr(raw, 'region'), box: bx, z: optNum(raw, 'z'), after: nullStr(raw, 'after'), markdown: optStr(raw, 'markdown') |
| 88 | } |
| 89 | } |
| 90 | case 'remove_block': |
| 91 | return { op, slide: str(raw, 'slide'), id: str(raw, 'id') } |
| 92 | case 'move_block': |
| 93 | return { op, slide: str(raw, 'slide'), id: str(raw, 'id'), after: optStr(raw, 'after') } |
| 94 | case 'set_block': { |
| 95 | const bx = raw.box === undefined ? undefined : raw.box === null ? null : boxSpec(raw.box) |
| 96 | return { |
| 97 | op, slide: str(raw, 'slide'), id: str(raw, 'id'), props: optProps(raw, 'props'), region: nullStr(raw, 'region'), box: bx, z: optNum(raw, 'z'), |
| 98 | step: nullNum(raw, 'step'), items: nullStr(raw, 'items') |
| 99 | } |
| 100 | } |
| 101 | case 'set_text': |
| 102 | return { op, slide: str(raw, 'slide'), id: str(raw, 'id'), markdown: str(raw, 'markdown') } |
| 103 | case 'set_phone': { |
| 104 | const p = field(raw, 'phone', isObject, 'a map') |
| 105 | for (const k of Object.keys(p)) if (!['hidden', 'order', 'size', 'box'].includes(k)) fail(`unknown field \`${k}\` in phone`) |
| 106 | const phone = { |
| 107 | hidden: optional(p, 'hidden', v => typeof v === 'boolean', 'a boolean'), |
| 108 | order: optional(p, 'order', isInt, 'an integer'), |
| 109 | size: optStr(p, 'size'), |
| 110 | box: p.box === undefined || p.box === null ? undefined : boxSpec(p.box) |
| 111 | } |
| 112 | return { op, slide: str(raw, 'slide'), id: str(raw, 'id'), phone } |
| 113 | } |
| 114 | case 'add_scene': |
| 115 | return { |
| 116 | op, id: str(raw, 'id'), name: str(raw, 'name'), code: str(raw, 'code'), states: field(raw, 'states', () => true, 'a value'), |
| 117 | words: raw.words ?? undefined, data: raw.data ?? undefined, author: optStr(raw, 'author') |
| 118 | } |
| 119 | case 'set_scene': |
| 120 | return { |
| 121 | op, id: str(raw, 'id'), name: optStr(raw, 'name'), code: optStr(raw, 'code'), states: raw.states ?? undefined, |
| 122 | words: raw.words ?? undefined, data: raw.data, stills: raw.stills ?? undefined, author: optStr(raw, 'author') |
| 123 | } |
| 124 | case 'remove_scene': |
| 125 | return { op, id: str(raw, 'id') } |
| 126 | case 'register_asset': |
| 127 | return { |
| 128 | op, id: str(raw, 'id'), ref: str(raw, 'ref'), name: optStr(raw, 'name') ?? '', |
| 129 | width: optional(raw, 'width', isInt, 'an integer') ?? 0, height: optional(raw, 'height', isInt, 'an integer') ?? 0, alt: optStr(raw, 'alt') ?? '', |
| 130 | // A clip's video (scenes and clips spec §2). |
| 131 | poster: optStr(raw, 'poster') ?? '', duration: optional(raw, 'duration', isInt, 'an integer') ?? 0 |
| 132 | } |
| 133 | default: |
| 134 | fail(`unknown variant \`${op}\``) |
| 135 | } |
| 136 | } |
| 137 | |