Skip to content

Commit

Permalink
feat(model): Add name() getter to CommandOptionChoice
Browse files Browse the repository at this point in the history
  • Loading branch information
suneettipirneni committed Jan 20, 2023
1 parent 227e407 commit 2d6b4e8
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions twilight-model/src/application/command/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,18 @@ pub enum CommandOptionChoice {
Number(CommandOptionChoiceData<f64>),
}

impl CommandOptionChoice {
/// The name of the option choice.
#[allow(clippy::missing_const_for_fn)] // It can't be const because of destructors in match arms.
pub fn name(self) -> String {
match self {
Self::String(data) => data.name,
Self::Integer(data) => data.name,
Self::Number(data) => data.name,
}
}
}

/// Data of [`CommandOptionChoice`].
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct CommandOptionChoiceData<T> {
Expand Down Expand Up @@ -234,3 +246,33 @@ impl CommandOptionType {
}
}
}

#[cfg(test)]
mod tests {
use super::{CommandOptionChoice, CommandOptionChoiceData};

#[test]
fn option_name() {
let choice1 = CommandOptionChoice::String(CommandOptionChoiceData {
name: "test".to_string(),
name_localizations: None,
value: "test".to_string(),
});

let choice2 = CommandOptionChoice::Integer(CommandOptionChoiceData {
name: "test".to_string(),
name_localizations: None,
value: 100,
});

let choice3 = CommandOptionChoice::Number(CommandOptionChoiceData {
name: "test".to_string(),
name_localizations: None,
value: 1.5,
});

assert_eq!(choice1.name(), "test");
assert_eq!(choice2.name(), "test");
assert_eq!(choice3.name(), "test");
}
}

0 comments on commit 2d6b4e8

Please sign in to comment.