CommandOption

Trait CommandOption 

Source
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.

AttributeTypeLocationDescription
namestrVariantSet the name of the command option choice.
valuestr, i64 or f64VariantValue of the command option choice.

Required Methods§

Source

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.

Implementations on Foreign Types§

Source§

impl CommandOption for Cow<'_, str>

Source§

fn from_option( value: CommandOptionValue, data: CommandOptionData, resolved: Option<&InteractionDataResolved>, ) -> Result<Self, ParseOptionErrorType>

Source§

impl CommandOption for CommandOptionValue

Source§

fn from_option( value: CommandOptionValue, _data: CommandOptionData, _resolved: Option<&InteractionDataResolved>, ) -> Result<Self, ParseOptionErrorType>

Source§

impl CommandOption for bool

Source§

fn from_option( value: CommandOptionValue, _data: CommandOptionData, _resolved: Option<&InteractionDataResolved>, ) -> Result<Self, ParseOptionErrorType>

Source§

impl CommandOption for f64

Source§

fn from_option( value: CommandOptionValue, data: CommandOptionData, _resolved: Option<&InteractionDataResolved>, ) -> Result<Self, ParseOptionErrorType>

Source§

impl CommandOption for i64

Source§

fn from_option( value: CommandOptionValue, data: CommandOptionData, _resolved: Option<&InteractionDataResolved>, ) -> Result<Self, ParseOptionErrorType>

Source§

impl CommandOption for String

Source§

fn from_option( value: CommandOptionValue, data: CommandOptionData, _resolved: Option<&InteractionDataResolved>, ) -> Result<Self, ParseOptionErrorType>

Source§

impl CommandOption for InteractionChannel

Source§

fn from_option( value: CommandOptionValue, data: CommandOptionData, resolved: Option<&InteractionDataResolved>, ) -> Result<Self, ParseOptionErrorType>

Source§

impl CommandOption for Attachment

Source§

fn from_option( value: CommandOptionValue, _data: CommandOptionData, resolved: Option<&InteractionDataResolved>, ) -> Result<Self, ParseOptionErrorType>

Source§

impl CommandOption for Role

Source§

fn from_option( value: CommandOptionValue, _data: CommandOptionData, resolved: Option<&InteractionDataResolved>, ) -> Result<Self, ParseOptionErrorType>

Source§

impl CommandOption for Id<AttachmentMarker>

Source§

fn from_option( value: CommandOptionValue, _data: CommandOptionData, _resolved: Option<&InteractionDataResolved>, ) -> Result<Self, ParseOptionErrorType>

Source§

impl CommandOption for Id<ChannelMarker>

Source§

fn from_option( value: CommandOptionValue, _data: CommandOptionData, _resolved: Option<&InteractionDataResolved>, ) -> Result<Self, ParseOptionErrorType>

Source§

impl CommandOption for Id<GenericMarker>

Source§

fn from_option( value: CommandOptionValue, _data: CommandOptionData, _resolved: Option<&InteractionDataResolved>, ) -> Result<Self, ParseOptionErrorType>

Source§

impl CommandOption for Id<RoleMarker>

Source§

fn from_option( value: CommandOptionValue, _data: CommandOptionData, _resolved: Option<&InteractionDataResolved>, ) -> Result<Self, ParseOptionErrorType>

Source§

impl CommandOption for Id<UserMarker>

Source§

fn from_option( value: CommandOptionValue, _data: CommandOptionData, _resolved: Option<&InteractionDataResolved>, ) -> Result<Self, ParseOptionErrorType>

Source§

impl CommandOption for User

Source§

fn from_option( value: CommandOptionValue, _data: CommandOptionData, resolved: Option<&InteractionDataResolved>, ) -> Result<Self, ParseOptionErrorType>

Implementors§