The RelishValue to encode (discriminated union of all Relish types)
A Result containing the encoded bytes on success, or an EncodeError on failure
import { encode, U32, String_ } from '@grounds/core';
// Encode a simple value
const result = encode(U32(42));
result.match(
(bytes) => console.log('Encoded:', Array.from(bytes)),
(error) => console.error('Failed:', error.message)
);
// Encode a string
encode(String_('hello')).match(
(bytes) => console.log('String encoded to', bytes.length, 'bytes'),
(error) => console.error(error)
);
The function uses neverthrow's Result type for error handling. All errors
are captured in the Result and never thrown. The signature is never -> throws
(uses error handling at callsite, not exceptions).
Defense-in-depth validation: The encoder re-validates integer ranges even if a value somehow bypasses constructor validation. This ensures wire format correctness by catching non-integer floats (e.g., 3.14) and out-of-range values before they are encoded.
For encoding composite types, ensure:
Encodes a Relish value to binary bytes in wire format.
Converts a typed Relish value into its binary representation using the Relish wire format. Uses a reusable internal Encoder with a pre-allocated buffer for efficient encoding.