Skip to content

Commit

Permalink
feat(args): add Args#ok, refactored Args#error to return Err<Argument…
Browse files Browse the repository at this point in the history
…Error<T>> (#159)

BREAKING CHANGE: changed `Args.err` to return `Err<ArgumentError<T>>` instead of `ArgumentError<T>`
  • Loading branch information
kyranet authored Feb 6, 2021
1 parent 836f1b8 commit 65316a6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
21 changes: 13 additions & 8 deletions src/lib/parsers/Args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { UserError } from '../errors/UserError';
import type { ArgumentContext, ArgumentResult, IArgument } from '../structures/Argument';
import type { Command, CommandContext } from '../structures/Command';
import { isSome, maybe, Maybe } from './Maybe';
import { err, isErr, isOk, ok, Ok, Result } from './Result';
import { Err, err, isErr, isOk, ok, Ok, Result } from './Result';

/**
* The argument parser to be used in [[Command]].
Expand Down Expand Up @@ -647,14 +647,19 @@ export class Args {
}

/**
* Constructs an [[ArgumentError]] with a custom type.
* @param argument The argument that caused the rejection.
* @param parameter The parameter that triggered the argument.
* @param type The identifier for the error.
* @param message The description message for the rejection.
* Constructs an [[Ok]] result.
* @param value The value to pass.
*/
public static error<T>(options: ArgumentError.Options<T>): ArgumentError<T> {
return new ArgumentError<T>(options);
public static ok<T>(value: T): Ok<T> {
return ok(value);
}

/**
* Constructs an [[Err]] result containing an [[ArgumentError]].
* @param options The options for the argument error.
*/
public static error<T>(options: ArgumentError.Options<T>): Err<ArgumentError<T>> {
return err(new ArgumentError<T>(options));
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/lib/structures/Argument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Message } from 'discord.js';
import type { ArgumentError } from '../errors/ArgumentError';
import type { UserError } from '../errors/UserError';
import { Args } from '../parsers/Args';
import { err, ok, Result } from '../parsers/Result';
import type { Result } from '../parsers/Result';
import type { Command, CommandContext } from './Command';

/**
Expand Down Expand Up @@ -94,7 +94,7 @@ export abstract class Argument<T = unknown> extends AliasPiece implements IArgum
* @param value The value to wrap.
*/
public ok(value: T): ArgumentResult<T> {
return ok(value);
return Args.ok(value);
}

/**
Expand All @@ -104,7 +104,7 @@ export abstract class Argument<T = unknown> extends AliasPiece implements IArgum
* @param message The description message for the rejection.
*/
public error(options: Omit<ArgumentError.Options<T>, 'argument'>): ArgumentResult<T> {
return err(Args.error({ argument: this, ...options }));
return Args.error({ argument: this, ...options });
}
}

Expand Down

0 comments on commit 65316a6

Please sign in to comment.