From 5a26f6d7be9b8cb032049bc1440e375821994b69 Mon Sep 17 00:00:00 2001 From: GnomedDev Date: Mon, 21 Oct 2024 22:22:44 +0100 Subject: [PATCH] Stop using serde_json::Value in AutocompleteChoice::value --- examples/testing/src/main.rs | 3 +- src/builder/create_interaction_response.rs | 82 +++++++++++++--------- 2 files changed, 51 insertions(+), 34 deletions(-) diff --git a/examples/testing/src/main.rs b/examples/testing/src/main.rs index 54718226969..4c13aa3602e 100644 --- a/examples/testing/src/main.rs +++ b/examples/testing/src/main.rs @@ -394,8 +394,7 @@ impl EventHandler for Handler { i.create_response( &ctx.http, CreateInteractionResponse::Autocomplete( - CreateAutocompleteResponse::new() - .add_string_choice("suggestion", "suggestion"), + CreateAutocompleteResponse::new().add_choice("suggestion"), ), ) .await diff --git a/src/builder/create_interaction_response.rs b/src/builder/create_interaction_response.rs index 74aedd59d3f..caaa2d772b3 100644 --- a/src/builder/create_interaction_response.rs +++ b/src/builder/create_interaction_response.rs @@ -267,19 +267,59 @@ impl<'a> CreateInteractionResponseMessage<'a> { super::button_and_select_menu_convenience_methods!(self.components); } +#[derive(Clone, Debug, Serialize)] +#[serde(untagged)] +#[non_exhaustive] +#[must_use] +pub enum AutocompleteValue<'a> { + String(Cow<'a, str>), + Integer(u64), + Float(f64), +} + +impl<'a> From> for AutocompleteValue<'a> { + fn from(value: Cow<'a, str>) -> Self { + Self::String(value) + } +} + +impl From for AutocompleteValue<'static> { + fn from(value: String) -> Self { + Self::String(Cow::Owned(value)) + } +} + +impl<'a> From<&'a str> for AutocompleteValue<'a> { + fn from(value: &'a str) -> Self { + Self::String(Cow::Borrowed(value)) + } +} + +impl From for AutocompleteValue<'static> { + fn from(value: u64) -> Self { + Self::Integer(value) + } +} + +impl From for AutocompleteValue<'static> { + fn from(value: f64) -> Self { + Self::Float(value) + } +} + // Same as CommandOptionChoice according to Discord, see // [Autocomplete docs](https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-response-object-autocomplete). #[must_use] -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Serialize)] pub struct AutocompleteChoice<'a> { pub name: Cow<'a, str>, #[serde(skip_serializing_if = "Option::is_none")] pub name_localizations: Option, Cow<'a, str>>>, - pub value: Value, + pub value: AutocompleteValue<'a>, } impl<'a> AutocompleteChoice<'a> { - pub fn new(name: impl Into>, value: impl Into) -> Self { + pub fn new(name: impl Into>, value: impl Into>) -> Self { Self { name: name.into(), name_localizations: None, @@ -330,36 +370,14 @@ impl<'a> CreateAutocompleteResponse<'a> { self } - /// Add an int autocomplete choice. - /// - /// **Note**: There can be no more than 25 choices set. Name must be between 1 and 100 - /// characters. Value must be between -2^53 and 2^53. - pub fn add_int_choice(self, name: impl Into>, value: i64) -> Self { - self.add_choice(AutocompleteChoice::new(name, value)) - } - - /// Adds a string autocomplete choice. + /// Add an autocomplete choice. /// - /// **Note**: There can be no more than 25 choices set. Name must be between 1 and 100 - /// characters. Value must be up to 100 characters. - pub fn add_string_choice( - self, - name: impl Into>, - value: impl Into>, - ) -> Self { - self.add_choice(AutocompleteChoice::new(name, value.into())) - } - - /// Adds a number autocomplete choice. - /// - /// **Note**: There can be no more than 25 choices set. Name must be between 1 and 100 - /// characters. Value must be between -2^53 and 2^53. - pub fn add_number_choice(self, name: impl Into>, value: f64) -> Self { - self.add_choice(AutocompleteChoice::new(name, value)) - } - - fn add_choice(mut self, value: AutocompleteChoice<'a>) -> Self { - self.choices.to_mut().push(value); + /// # Limitations + /// - There can be no more than 25 choices set. + /// - Name must be between 1 and 100 characters. + /// - If value is an integer/float must be between -2^53 and 2^53. + pub fn add_choice(mut self, value: impl Into>) -> Self { + self.choices.to_mut().push(value.into()); self }