pub trait CommandOption: Sized {
// Required method
fn from_option(
value: CommandOptionValue,
data: CommandOptionData,
resolved: Option<&InteractionDataResolved>,
) -> Result<Self, ParseOptionErrorType>;
}Expand description
Parse command option into a concrete type.
This trait is used by the implementation of CommandModel generated
by the derive macro. See the module documentation for
a list of implemented types.
§Option choices
This trait can be derived on enums to represent command options with
predefined choices. The #[option] attribute must be present on each
variant.
The corresponding slash command types are automatically inferred from
the value attribute. In the example below, the inferred type would
be INTEGER.
A value method is also generated for each variant to obtain the
value of the variant. This method is not described in the trait
as it is only implemented for option choices.
§Example
use twilight_interactions::command::CommandOption;
#[derive(CommandOption)]
enum TimeUnit {
#[option(name = "Minute", value = 60)]
Minute,
#[option(name = "Hour", value = 3600)]
Hour,
#[option(name = "Day", value = 86400)]
Day,
}
assert_eq!(TimeUnit::Minute.value(), 60);§Macro attributes
The macro provides an #[option] attribute to configure the generated code.
| Attribute | Type | Location | Description |
|---|---|---|---|
name | str | Variant | Set the name of the command option choice. |
value | str, i64 or f64 | Variant | Value of the command option choice. |
Required Methods§
Sourcefn from_option(
value: CommandOptionValue,
data: CommandOptionData,
resolved: Option<&InteractionDataResolved>,
) -> Result<Self, ParseOptionErrorType>
fn from_option( value: CommandOptionValue, data: CommandOptionData, resolved: Option<&InteractionDataResolved>, ) -> Result<Self, ParseOptionErrorType>
Convert a CommandOptionValue into this value.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.