import { Constructor } from '@rustable/type'; /** * Decorator for creating enum variants. * Use this decorator to define static factory methods that create enum instances. * * @example * class Result extends Enum { * @variant static Ok(value: T): Result { } * @variant static Err(error: E): Result { } * } * * @param target The class prototype * @param name The name of the variant * @param descriptor The property descriptor * @returns Modified property descriptor */ export declare function variant(target: any, name: string, descriptor: PropertyDescriptor): PropertyDescriptor; export interface DefaultMatch { _: (() => U) | U; } type EnumMatchPatternBase = C | (Partial & DefaultMatch); type EnumMatch = { [K in keyof Omit]: E[K] extends (...args: infer P) => InstanceType ? ((...args: P) => U) | U : never; }; export type EnumMatchPattern = EnumMatchPatternBase>; export interface EnumModify { [key: string]: (...args: any[]) => any[]; } export interface EnumLetPattern any, R> { if: (...args: Parameters) => R; else: (() => R) | R; } export type EnumParam = Record any>; type CustomMatch = { [K in keyof U]: ((...args: Parameters) => T) | T; }; export type CustomModify = { [K in keyof U]: (...args: Parameters) => Parameters; }; export type EnumInstance = Omit & { match(patterns: EnumMatchPatternBase>): T; modify(patterns: Partial>): void; clone(): EnumInstance; eq(other: EnumInstance): boolean; } & { [P in keyof U as `is${Capitalize}`]: () => boolean; } & { [P in keyof U as `let${Capitalize}`]: (cb: EnumLetPattern) => T | undefined; }; export type CustomEnum = typeof Enum & { [K in keyof U]: (...args: Parameters) => EnumInstance; }; export declare namespace Enums { /** * Creates a custom Enum class with the given variant definitions. * @param name Optional name for the created Enum class * @param variants An object defining the variants and their parameters * @returns A new custom Enum class with the specified variants * * @example * const SimpleEnum = Enums.create({ * A: () => {}, * B: (_x: number) => {}, * C: (_x: string, _y: number) => {}, * }); * * const a = SimpleEnum.A(); * const b = SimpleEnum.B(42); * const c = SimpleEnum.C('hello', 5); */ function create(variants: U): CustomEnum; function create(name: string, variants: U): CustomEnum; } /** * Base class for implementing Rust-style enums with pattern matching. * Provides a type-safe way to handle multiple variants of a type. * * @example * class Result extends Enum { * @variant static Ok(value: T): Result { } * @variant static Err(error: E): Result { } * * unwrap(): T { * if (this.is('Ok')) return this.unwrapArg(); * throw new Error('Called unwrap on an Err value'); * } * } */ export declare class Enum { private name; private vars; constructor(name: string, ...vars: any[]); /** * Checks if the enum is a specific variant * @param variant The variant name to check * @returns true if the enum is the specified variant */ is(variant: string): boolean; /** * Checks if the enum is a specific variant and executes a callback if it matches * @param variant The variant name to check * @param callback The callback function to execute if the variant matches * @returns The result of the callback if variant matches, undefined otherwise */ let(variant: string, cb: EnumLetPattern<(...arg: any[]) => T, T>): T; /** * Unwraps the first argument of a variant * @throws Error if the variant has no arguments * @returns The first argument of the variant */ unwrap(): T; /** * Unwraps all arguments of a variant as a tuple * @throws Error if the variant has no arguments * @returns Tuple of all variant arguments */ unwrapTuple(): T; /** * Converts the enum to a string representation * Format: VariantName for variants without arguments * Format: VariantName(arg1, arg2, ...) for variants with arguments */ toString(): string; /** * Pattern matches on the enum variant, similar to Rust's enum expression * Use this method to handle different variants of the enum in a type-safe way. * * @param patterns Object mapping variant names to handler functions * @param defaultPatterns Optional default patterns to use if a variant isn't matched * @throws Error if no matching pattern is found and no default pattern is provided * @example * ```typescript * enum.enum({ * Success: (value) => `Got ${value}`, * Error: (err) => `Error: ${err.message}`, * }) * ``` */ match(patterns: EnumMatchPatternBase>): U; /** * Checks if this enum instance equals another enum instance * Compares both variant names and their arguments */ eq(other: Enum): boolean; /** * Creates a deep clone of the current enum instance * @returns A new instance of the enum with the same variant and cloned arguments */ clone(hash?: WeakMap): this; /** * Replaces the current variant with a new one, returning the old variant * @param newVariant The new variant to replace with * @param ...args Arguments for the new variant * @throws Error if the new variant is not a valid variant of this enum * @returns The old variant instance */ replace(newInstance: Enum): this; /** * Modifies the arguments of the current variant based on the variant name * @param patterns Object mapping variant names to modifier functions * @throws Error if no matching pattern is found for the current variant */ modify(patterns: EnumModify): void; } export {};