import { LoopAction } from './loopAction'; import { Result } from './result'; /** * A strategy for running an input loop. * @typeparam A - Input type. * @typeparam Z - Output type. * @typeparam E - Error type. */ export interface LoopStrategy { /** * Gets new input from somewhere e.g. reading a line. * @returns A loop action that can: step with the input; finish with some parsed value; fail due to an error. */ getInput(): LoopAction; /** * Parses given input into the desired type. * @param input - The input. * @returns A loop action that can: step on; finish with some parsed value; fail due to an error. */ parse(input: A): LoopAction; /** * Handles error on getting new input. * This function intercepts the `fail` case of `getInput`. * @param error - The error encountered. * @returns A loop action that can: step on; finish with some parsed value; fail due to an error. */ onInputError?(error: E): LoopAction; /** * Handles error on parsing input. * This function intercepts the `fail` case of `parse`. * @param error - The error encountered. * @param input - The input that could not be parsed. * @returns A loop action that can: step on; finish with some parsed value; fail due to an error. */ onParseError?(error: E, input: A): LoopAction; } /** * A strategy for running an input loop asynchronously via `Promise`. * @typeparam A - Input type. * @typeparam Z - Output type. * @typeparam E - Error type. */ export interface LoopStrategyAsync { /** * Gets new input from somewhere e.g. reading a line. * @returns A loop action that can: step with the input; finish with some parsed value; fail due to an error. */ getInput(): Promise>; /** * Parses given input into the desired type. * @param input - The input. * @returns A loop action that can: step on; finish with some parsed value; fail due to an error. */ parse(input: A): Promise>; /** * Handles error on getting new input. * This function intercepts the `fail` case of `getInput`. * @param error - The error encountered. * @returns A loop action that can: step on; finish with some parsed value; fail due to an error. */ onInputError?(error: E): Promise>; /** * Handles error on parsing input. * This function intercepts the `fail` case of `parse`. * @param error - The error encountered. * @param input - The input that could not be parsed. * @returns A loop action that can: step on; finish with some parsed value; fail due to an error. */ onParseError?(error: E, input: A): Promise>; } /** * Runs a loop which continuously gets input and attempts to parse it. * The loop strategy used will determine how the loop continues and ends. * * ```ts * const getInputFromSomewhere = () => '2'; * * const x = loop('1', { * getInput() { * const i = getInputFromSomewhere(); * return i == null ? fail('no input') : step(i); * }, * * parse(x: string) { * const n = Number(x); * return isNaN(n) ? fail('bad input') : finish(n); * } * }); * * console.log(x); * >>> 1 * ``` * * @typeparam A - Input type. * @typeparam Z - Output type. * @typeparam E - Error type. * @param intialInput - The first input to parse. * @param strat - The loop strategy to use. * @returns Either the parsed value or an error. */ export declare function loop(intialInput: A, strat: LoopStrategy): Result; /** * Runs a loop which continuously gets input and attempts to parse it. * The loop strategy used will determine how the loop continues and ends. * This variant has no initial input. * * ```ts * const getInputFromSomewhere = () => '2'; * * const x = loop1({ * getInput() { * const i = getInputFromSomewhere(); * return i == null ? fail('no input') : step(i); * }, * * parse(x: string) { * const n = Number(x); * return isNaN(n) ? fail('bad input') : finish(n); * } * }); * * console.log(x); * >>> 2 * ``` * * @typeparam A - Input type. * @typeparam Z - Output type. * @typeparam E - Error type. * @param strat - The loop strategy to use. * @returns Either the parsed value or an error. */ export declare function loop1(strat: LoopStrategy): Result; /** * Runs a loop which continuously gets input and attempts to parse it. * The loop strategy used will determine how the loop continues and ends. * This variant of the function is asynchronous using `Promise`. * @typeparam A - Input type. * @typeparam Z - Output type. * @typeparam E - Error type. * @param intialInput - The first input to parse. * @param strat - The loop strategy to use. * @returns Either the parsed value or an error. */ export declare function loopAsync(intialInput: A, strat: LoopStrategyAsync): Promise>; /** * Runs a loop which continuously gets input and attempts to parse it. * The loop strategy used will determine how the loop continues and ends. * This variant has no initial input. * This variant of the function is asynchronous using `Promise`. * @typeparam A - Input type. * @typeparam Z - Output type. * @typeparam E - Error type. * @param strat - The loop strategy to use. * @returns Either the parsed value or an error. */ export declare function loop1Async(strat: LoopStrategyAsync): Promise>;