serenity-rs / framework

The official command framework for bots written in Serenity

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add a `OneOf` type for command arguments

AriusX7 opened this issue · comments

commented

Having a OneOf (OneOfTwo more specifically) enum to allow a command argument to be one of two types would be very convenient with the new framework.

enum OneOf<T: Parse, U: Parse> {
    VariantOne(T),
    VariantTwo(U),
}

The OneOf enum would implement the Parse trait and try to parse the string as type T first. If that fails, it'd try type U. If both fail, it'd return an error that contains both T's and U's parse errors. Inside a command's function body, a simple match could be used to handle the OneOf argument.

Currently, without such a type, a developer would have to create newtypes and implement the Parse trait for all combinations they wish to support (or create something like this type themselves). I've often had to parse user input as one of two possible types, so having the framework support something like this would be very nice.

I wanted to create this issue before working on a PR for the same to see if such a type would be appropriate for the new framework.

Seems reasonable to add. It'd neatly handle parsing a combination of types for one argument as well by chaining OneOfs (e.g. OneOf<f32, OneOf<i32, String>>).

I would likely name it ParseEither or ParseOne.