Skip to content

Commit 450f36d

Browse files
authored
Stop using serde_json::Value in AutocompleteChoice::value (#3000)
More type safe, more performant, less reliant on serde_json, what's not to love!
1 parent 6a614cd commit 450f36d

File tree

2 files changed

+51
-34
lines changed

2 files changed

+51
-34
lines changed

examples/testing/src/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,7 @@ impl EventHandler for Handler {
394394
i.create_response(
395395
&ctx.http,
396396
CreateInteractionResponse::Autocomplete(
397-
CreateAutocompleteResponse::new()
398-
.add_string_choice("suggestion", "suggestion"),
397+
CreateAutocompleteResponse::new().add_choice("suggestion"),
399398
),
400399
)
401400
.await

src/builder/create_interaction_response.rs

Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -267,19 +267,59 @@ impl<'a> CreateInteractionResponseMessage<'a> {
267267
super::button_and_select_menu_convenience_methods!(self.components);
268268
}
269269

270+
#[derive(Clone, Debug, Serialize)]
271+
#[serde(untagged)]
272+
#[non_exhaustive]
273+
#[must_use]
274+
pub enum AutocompleteValue<'a> {
275+
String(Cow<'a, str>),
276+
Integer(u64),
277+
Float(f64),
278+
}
279+
280+
impl<'a> From<Cow<'a, str>> for AutocompleteValue<'a> {
281+
fn from(value: Cow<'a, str>) -> Self {
282+
Self::String(value)
283+
}
284+
}
285+
286+
impl From<String> for AutocompleteValue<'static> {
287+
fn from(value: String) -> Self {
288+
Self::String(Cow::Owned(value))
289+
}
290+
}
291+
292+
impl<'a> From<&'a str> for AutocompleteValue<'a> {
293+
fn from(value: &'a str) -> Self {
294+
Self::String(Cow::Borrowed(value))
295+
}
296+
}
297+
298+
impl From<u64> for AutocompleteValue<'static> {
299+
fn from(value: u64) -> Self {
300+
Self::Integer(value)
301+
}
302+
}
303+
304+
impl From<f64> for AutocompleteValue<'static> {
305+
fn from(value: f64) -> Self {
306+
Self::Float(value)
307+
}
308+
}
309+
270310
// Same as CommandOptionChoice according to Discord, see
271311
// [Autocomplete docs](https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-response-object-autocomplete).
272312
#[must_use]
273-
#[derive(Clone, Debug, Deserialize, Serialize)]
313+
#[derive(Clone, Debug, Serialize)]
274314
pub struct AutocompleteChoice<'a> {
275315
pub name: Cow<'a, str>,
276316
#[serde(skip_serializing_if = "Option::is_none")]
277317
pub name_localizations: Option<HashMap<Cow<'a, str>, Cow<'a, str>>>,
278-
pub value: Value,
318+
pub value: AutocompleteValue<'a>,
279319
}
280320

281321
impl<'a> AutocompleteChoice<'a> {
282-
pub fn new(name: impl Into<Cow<'a, str>>, value: impl Into<Value>) -> Self {
322+
pub fn new(name: impl Into<Cow<'a, str>>, value: impl Into<AutocompleteValue<'a>>) -> Self {
283323
Self {
284324
name: name.into(),
285325
name_localizations: None,
@@ -330,36 +370,14 @@ impl<'a> CreateAutocompleteResponse<'a> {
330370
self
331371
}
332372

333-
/// Add an int autocomplete choice.
334-
///
335-
/// **Note**: There can be no more than 25 choices set. Name must be between 1 and 100
336-
/// characters. Value must be between -2^53 and 2^53.
337-
pub fn add_int_choice(self, name: impl Into<Cow<'a, str>>, value: i64) -> Self {
338-
self.add_choice(AutocompleteChoice::new(name, value))
339-
}
340-
341-
/// Adds a string autocomplete choice.
373+
/// Add an autocomplete choice.
342374
///
343-
/// **Note**: There can be no more than 25 choices set. Name must be between 1 and 100
344-
/// characters. Value must be up to 100 characters.
345-
pub fn add_string_choice(
346-
self,
347-
name: impl Into<Cow<'a, str>>,
348-
value: impl Into<Cow<'a, str>>,
349-
) -> Self {
350-
self.add_choice(AutocompleteChoice::new(name, value.into()))
351-
}
352-
353-
/// Adds a number autocomplete choice.
354-
///
355-
/// **Note**: There can be no more than 25 choices set. Name must be between 1 and 100
356-
/// characters. Value must be between -2^53 and 2^53.
357-
pub fn add_number_choice(self, name: impl Into<Cow<'a, str>>, value: f64) -> Self {
358-
self.add_choice(AutocompleteChoice::new(name, value))
359-
}
360-
361-
fn add_choice(mut self, value: AutocompleteChoice<'a>) -> Self {
362-
self.choices.to_mut().push(value);
375+
/// # Limitations
376+
/// - There can be no more than 25 choices set.
377+
/// - Name must be between 1 and 100 characters.
378+
/// - If value is an integer/float must be between -2^53 and 2^53.
379+
pub fn add_choice(mut self, value: impl Into<AutocompleteChoice<'a>>) -> Self {
380+
self.choices.to_mut().push(value.into());
363381
self
364382
}
365383

0 commit comments

Comments
 (0)