(layout: Layout, value: unknown, options: SchemaAST.ParseOptions)
| 3425 | // Decimal scale rides above the low three kind bits internally, avoiding a |
| 3426 | // second decimalScale scan when the payload is written. |
| 3427 | function valueWireCode(layout: Layout, value: unknown, options: SchemaAST.ParseOptions): number { |
| 3428 | if (layout._ === "literal") { |
| 3429 | if (!hasLiteral(layout, value)) encodeFail(literalExpected(layout), value, options) |
| 3430 | return valueWireCode(layout.leaf, value, options) |
| 3431 | } |
| 3432 | switch (layout._) { |
| 3433 | case "bool": |
| 3434 | if (typeof value !== "boolean") encodeFail("a boolean", value, options) |
| 3435 | return value ? FIELD_WIRE_TRUE : FIELD_WIRE_FALSE |
| 3436 | case "number": |
| 3437 | if (typeof value !== "number") encodeFail("a number", value, options) |
| 3438 | if (isVarintNumber(value)) return FIELD_WIRE_VARINT |
| 3439 | { |
| 3440 | const scale = decimalScale(value) |
| 3441 | return scale === 0 ? FIELD_WIRE_FIXED64 : scale * FIELD_WIRE_FACTOR + FIELD_WIRE_DECIMAL |
| 3442 | } |
| 3443 | case "int": |
| 3444 | if (!Number.isSafeInteger(value)) encodeFail("an integer", value, options) |
| 3445 | return FIELD_WIRE_VARINT |
| 3446 | // The payload write validates the value. |
| 3447 | case "i32": |
| 3448 | case "u32": |
| 3449 | return FIELD_WIRE_FIXED32 |
| 3450 | default: |
| 3451 | return FIELD_WIRE_SIZED |
| 3452 | } |
| 3453 | } |
| 3454 | |
| 3455 | function encodeWirePayload(ctx: EncodeContext, layout: Layout, value: unknown, wire: number, w: Writer) { |
| 3456 | const kind = wire % FIELD_WIRE_FACTOR |
no test coverage detected