Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Improve Result serde APIs for 3.0 #70

@dmmulroy

Description

@dmmulroy

Problem

The current serialization APIs (Result.serialize / Result.deserialize) are too inflexible for 3.0. They only preserve the outer Result shape and assume the inner ok / err payloads are already wire-safe and can be trusted on the way back in.

That makes common boundary use cases awkward:

  • mapping domain objects/errors into explicit wire schemas
  • validating unknown input while deserializing
  • using different schemas for outbound vs inbound payloads
  • colocating serde behavior with a reusable Result-level codec

Proposed direction

Introduce a codec-style API that accepts per-variant serializers/deserializers and returns an object with serialize / deserialize helpers.

Result.codec API mockup

Sketch:

import { Result } from "better-result";

const UserResultCodec = Result.codec({
  serialize: {
    ok: UserToWireSchema,
    err: AppErrorToWireSchema,
  },
  deserialize: {
    ok: WireToUserSchema,
    err: WireToAppErrorSchema,
  },
});

// sending network boundary
UserResultCodec.serialize(result);

// receiving network boundary
UserResultCodec.deserialize(input);

Open questions

  • What schema/codec interface should Result.codec accept? (Standard Schema, custom parse/encode contract, adapters, etc.)
  • Should deserialize return Result<Result<T, E>, ResultDeserializationError> or keep the current flattened Result<T, E | ResultDeserializationError> style?
  • Should the legacy Result.serialize / Result.deserialize stay as simple defaults, become wrappers over Result.codec, or be removed/reworked in 3.0?
  • How should errors from ok/err payload decoding be represented and typed?

Acceptance criteria

  • Design a flexible serde API for 3.0 centered around Result.codec or equivalent.
  • Preserve strong inference for both ok and err payloads in serialize and deserialize directions.
  • Support safe deserialization from unknown at network/process boundaries.
  • Document migration guidance from the current Result.serialize / Result.deserialize APIs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions