import { RnException } from './RnException'; /** * Represents an error object containing a message and error details. * @template ErrObj - The type of the error object */ export interface RnError { /** The error message */ message: string; /** The error object containing additional error details */ error: ErrObj; } /** * An interface to handle results in a unified manner, * regardless of whether they are successful or not. * This follows the Result pattern commonly used in functional programming. * @template T - The type of the success value * @template ErrObj - The type of the error object */ interface IResult { /** * Chains operations on successful results. If this result is Ok, applies the function * to the contained value. If this result is Err, returns the error unchanged. * @template U - The type of the new success value * @param f - Function to apply to the success value * @returns A new Result containing either the transformed value or the original error */ andThen(f: (value: T) => Result): Result; /** * Provides an alternative result if this result is an error. * If this result is Ok, returns this result unchanged. * If this result is Err, returns the result of the provided function. * @template U - The type of the alternative success value * @param f - Function that provides an alternative result * @returns This result if Ok, otherwise the alternative result */ orElse(f: () => Result): Result; /** * Pattern matching for Result types. Executes the appropriate function * based on whether this result is Ok or Err. * @template R - The type of the return value from the Ok function * @template ErrObj2 - The type of the new error object * @param obj - Object containing pattern matching functions for Ok and Err cases * @param obj.Ok - Function to execute if this result is Ok * @param obj.Err - Function to execute if this result is Err * @returns A new Result containing the result of the pattern matching */ match({ Ok, Err, }: { Ok: (value: T) => R; Err: (value: RnError) => RnError; }): Result; /** * Safely extracts the inner value with error compensation. * If the result is Ok, returns the contained value. * If the result is Err, calls the compensation function to provide a fallback value. * @param catchFn - Function that provides a fallback value in case of error * @returns The success value or the compensated value */ unwrapWithCompensation(catchFn: (err: RnError) => T): T; /** * Forcefully extracts the inner value. * If the result is Ok, returns the contained value. * If the result is Err, throws an exception. * @throws {RnException} When called on an Err result * @returns The success value */ unwrapForce(): T; /** * Type guard to check if this result is Ok. * @returns True if this result is Ok, false otherwise */ isOk(): this is Ok; /** * Type guard to check if this result is Err. * @returns True if this result is Err, false otherwise */ isErr(): this is Err; /** * Gets the class name of this result. * @returns The name of the class ('Ok' or 'Err') */ name(): string; } /** * Abstract base class for Result implementations. * Provides common functionality for both Ok and Err classes. * @template T - The type of the success value * @template ErrObj - The type of the error object */ declare abstract class CResult { protected val?: T | RnError | undefined; /** * Creates a new CResult instance. * @param val - The value to store (either success value or error) */ constructor(val?: T | RnError | undefined); /** * Pattern matching implementation for Result types. * @template R - The type of the return value from the Ok function * @template ErrObj2 - The type of the new error object * @param obj - Object containing pattern matching functions * @param obj.Ok - Function to execute if this result is Ok * @param obj.Err - Function to execute if this result is Err * @returns A new Result containing the result of the pattern matching * @throws {Error} If the result is neither Ok nor Err */ match(obj: { Ok: (value: T) => R; Err: (value: RnError) => RnError; }): Result; /** * Gets the name of this result class. * @returns The constructor name ('Ok' or 'Err') */ name(): string; } /** * Represents a successful result containing a value. * This class indicates that an operation completed successfully. * @template T - The type of the success value * @template ErrObj - The type of the error object (for type compatibility) */ export declare class Ok extends CResult implements IResult { /** * Chains operations on this successful result. * Applies the provided function to the contained value. * @template U - The type of the new success value * @param f - Function to apply to the success value * @returns The result of applying the function */ andThen(f: (value: T) => Result): Result; /** * Returns this Ok result unchanged since it's already successful. * The alternative function is not called. * @template U - The type of the alternative success value * @param f - Alternative function (not called for Ok results) * @returns This Ok result cast to the new type */ orElse(_f: () => Result): Result; /** * Returns the contained value since this is a successful result. * The compensation function is not called. * @param catchFn - Compensation function (not called for Ok results) * @returns The contained success value */ unwrapWithCompensation(_catchFn: (err: RnError) => T): T; /** * Safely returns the contained value. * @returns The contained success value */ unwrapForce(): T; /** * Always returns true for Ok results. * @deprecated Use isOk() instead * @returns Always true */ true(): this is Ok; /** * Type guard indicating this is an Ok result. * @returns Always true for Ok instances */ isOk(): this is Ok; /** * Type guard indicating this is not an Err result. * @returns Always false for Ok instances */ isErr(): this is Err; /** * Safely extracts the contained success value. * @returns The contained success value */ get(): T; } /** * Represents a failed result containing an error. * This class indicates that an operation failed with specific error information. * @template T - The type of the success value (for type compatibility) * @template ErrObj - The type of the error object */ export declare class Err extends CResult implements IResult { /** The RnException instance created from the error */ _rnException: RnException; /** * Creates a new Err result. * @param val - The error information to store */ constructor(val: RnError); /** * Returns this Err result unchanged since the operation already failed. * The chaining function is not called. * @template U - The type of the new success value * @param f - Chaining function (not called for Err results) * @returns This Err result cast to the new type */ andThen(_f: (value: T) => Result): Result; /** * Provides an alternative result since this result failed. * Calls the provided function to get an alternative result. * @template U - The type of the alternative success value * @param f - Function that provides an alternative result * @returns The result of the alternative function */ orElse(f: () => Result): Result; /** * Calls the compensation function to provide a fallback value. * @param catchFn - Function that provides a fallback value based on the error * @returns The compensated value from the catch function */ unwrapWithCompensation(catchFn: (err: RnError) => T): T; /** * Throws an exception since this result contains an error. * @throws {RnException} Always throws the contained exception * @returns Never returns (always throws) */ unwrapForce(): never; /** * Always returns false for Err results. * @deprecated This method seems to be unused * @returns Always false */ false(): false; /** * Type guard indicating this is not an Ok result. * @returns Always false for Err instances */ isOk(): this is Ok; /** * Type guard indicating this is an Err result. * @returns Always true for Err instances */ isErr(): this is Err; /** * Gets the contained error information. * @returns The RnError object containing error details */ getRnError(): RnError; /** * Converts this error to a string representation. * @returns The stack trace from the contained exception */ toString(): string; } /** * Union type representing either a successful result (Ok) or a failed result (Err). * @template T - The type of the success value * @template ErrObj - The type of the error object */ export type Result = Ok | Err; /** * Type assertion function to ensure a result is Ok. * Throws an error if the result is Err. * @param result - The result to check * @throws {Error} If the result is Err */ export declare function assertIsOk(result: IResult): asserts result is Ok; /** * Type assertion function to ensure a result is Err. * Throws an error if the result is Ok. * @param result - The result to check * @throws {Error} If the result is Ok */ export declare function assertIsErr(result: IResult): asserts result is Err; export {};