//#region src/result/result.d.ts /** * Internal result helper types and constructors shared by low-level modules. * * Mirrors the public result model without depending on the public barrel. * * @module */ /** * Discriminated `ok` union: either `{ ok: true; value }` or `{ ok: false; error }`. * * Every fallible public API in micro509 returns a specialization of this type. */ type Result = { /** Operation succeeded. */ readonly ok: true; /** Successful payload. */ readonly value: TValue; } | { /** Operation failed. */ readonly ok: false; /** Structured error payload. */ readonly error: TError; }; /** Failed result with a flattened code/message/details surface for ergonomic matching. */ interface ErrorResult> { /** Always `false` for failures. */ readonly ok: false; /** Structured error payload. */ readonly error: TError; /** Machine-readable failure reason, mirrored from `error.code`. */ readonly code: TCode; /** Human-readable diagnostic, mirrored from `error.message`. */ readonly message: string; /** Optional structured context for the failure. */ readonly details?: TDetails; } /** Like {@link ErrorResult} but also carries an index into the collection that was being processed. */ interface IndexedErrorResult> extends ErrorResult { /** Zero-based position of the failing item in the input collection. */ readonly index?: number; } /** Base error shape carried by all failure results in the library. */ interface Micro509Error> { /** Machine-readable failure reason (e.g. `'malformed'`, `'expired'`). */ readonly code: TCode; /** Human-readable diagnostic message. */ readonly message: string; /** Optional structured context for the failure. */ readonly details?: TDetails; } /** Like {@link Micro509Error} but includes a positional index for collection-processing APIs. */ interface IndexedMicro509Error> extends Micro509Error { /** Zero-based position of the failing item in the input collection. */ readonly index?: number; } /** Wraps a value in a success result (`{ ok: true, value }`). */ declare function successResult(value: TValue): { readonly ok: true; readonly value: TValue; }; /** * Exception form of a {@link Micro509Error}: a branded {@link Error} carrying the structured * {@linkcode code}, `message`, and any `details`. * * Thrown by {@link unwrap} for a failed result and by {@link throwMicro509Error} when a builder * rejects invalid construction input. Detect with {@link isResultError}. */ interface ResultError = Micro509Error> extends Error { /** Machine-readable failure reason, mirrored from `error.code`. */ readonly code: TError["code"]; /** The structured error payload that produced this exception. */ readonly error: TError; } /** Type guard: was {@linkcode value} thrown by {@link unwrap}? Narrows to {@link ResultError}. */ declare function isResultError(value: unknown): value is ResultError; /** A minimal fallible-result shape: `{ ok: true, value }` or `{ ok: false, error }`. */ type UnwrappableResult = { readonly ok: true; readonly value: TValue; } | { readonly ok: false; readonly error: TError; }; /** * Explicit escape hatch: returns the success value, or throws a {@link ResultError} carrying the structured failure. * * Use when you have already validated the input (or prefer exceptions) and the Result ceremony is noise. * Accepts any of the library's `*Result` types. */ declare function unwrap>(result: UnwrappableResult): TValue; /** Returns the success value, or {@linkcode fallback} when the result is a failure. */ declare function unwrapOr(result: UnwrappableResult, fallback: TValue): TValue; /** * Rethrows {@link error} if it is an invariant/programmer error ({@link TypeError}, {@link RangeError}, * {@link ReferenceError}, {@link SyntaxError}); otherwise returns. * * Boundary wrappers that turn an expected failure into a `Result` should call this first, so a genuine crash * is never masked as a clean parse/decode failure. */ declare function rethrowIfInvariant(error: unknown): void; /** Constructs a {@link Micro509Error} payload. */ declare function micro509Error>(code: TCode, message: string, details?: TDetails): Micro509Error; /** Constructs an {@link IndexedMicro509Error} payload with an optional collection index. */ declare function indexedMicro509Error>(code: TCode, message: string, index?: number, details?: TDetails): IndexedMicro509Error; /** Wraps a {@link Micro509Error} in a flattened {@link ErrorResult}. */ declare function errorResult>(error: TError): ErrorResult; /** * Builds a flattened failure result in one step. * * Single source of truth for the `{ ok: false, error, code, message }` shape: * modules should construct failures with this instead of hand-rolling the * object literal. The `error` payload carries the redundant `ok: false` * discriminant so it matches the per-operation `*Failure` interfaces * (`interface XFailure extends Micro509Error<…> { ok: false }`). */ declare function failureResult>(code: TCode, message: string, details?: TDetails): ErrorResult & { readonly ok: false; }>; /** Wraps an {@link IndexedMicro509Error} in a flattened {@link IndexedErrorResult}. */ declare function indexedErrorResult>(error: TError): IndexedErrorResult; //#endregion export { ErrorResult, IndexedErrorResult, IndexedMicro509Error, Micro509Error, Result, ResultError, UnwrappableResult, errorResult, failureResult, indexedErrorResult, indexedMicro509Error, isResultError, micro509Error, rethrowIfInvariant, successResult, unwrap, unwrapOr }; //# sourceMappingURL=result.d.ts.map