diff --git a/src/builder/create_components.rs b/src/builder/create_components.rs index 516f7aeac00..ebc14734fa5 100644 --- a/src/builder/create_components.rs +++ b/src/builder/create_components.rs @@ -1,7 +1,6 @@ use std::borrow::Cow; use serde::Serialize; -use serde_json::json; use crate::model::prelude::*; @@ -31,19 +30,20 @@ impl<'a> CreateActionRow<'a> { } } -impl<'a> serde::Serialize for CreateActionRow<'a> { +impl serde::Serialize for CreateActionRow<'_> { fn serialize(&self, serializer: S) -> Result { - use serde::ser::Error as _; - - json!({ - "type": 1, - "components": match self { - Self::Buttons(x) => serde_json::to_value(x).map_err(S::Error::custom)?, - Self::SelectMenu(x) => serde_json::to_value(vec![x]).map_err(S::Error::custom)?, - Self::InputText(x) => serde_json::to_value(vec![x]).map_err(S::Error::custom)?, - } - }) - .serialize(serializer) + use serde::ser::SerializeMap as _; + + let mut map = serializer.serialize_map(Some(2))?; + map.serialize_entry("type", &1_u8)?; + + match self { + CreateActionRow::Buttons(buttons) => map.serialize_entry("components", &buttons)?, + CreateActionRow::SelectMenu(select) => map.serialize_entry("components", &[select])?, + CreateActionRow::InputText(input) => map.serialize_entry("components", &[input])?, + } + + map.end() } } @@ -191,12 +191,18 @@ struct CreateSelectMenuDefault(Mention); impl Serialize for CreateSelectMenuDefault { fn serialize(&self, serializer: S) -> Result { + use serde::ser::SerializeMap as _; + let (id, kind) = match self.0 { Mention::Channel(c) => (c.get(), "channel"), Mention::Role(r) => (r.get(), "role"), Mention::User(u) => (u.get(), "user"), }; - json!({"id": id, "type": kind}).serialize(serializer) + + let mut map = serializer.serialize_map(Some(2))?; + map.serialize_entry("id", &id)?; + map.serialize_entry("type", kind)?; + map.end() } } diff --git a/src/builder/create_interaction_response.rs b/src/builder/create_interaction_response.rs index c0b17304092..74aedd59d3f 100644 --- a/src/builder/create_interaction_response.rs +++ b/src/builder/create_interaction_response.rs @@ -1,8 +1,6 @@ use std::borrow::Cow; use std::collections::HashMap; -use serde_json::json; - use super::{ CreateActionRow, CreateAllowedMentions, @@ -61,30 +59,30 @@ pub enum CreateInteractionResponse<'a> { impl serde::Serialize for CreateInteractionResponse<'_> { fn serialize(&self, serializer: S) -> StdResult { - use serde::ser::Error as _; - - #[allow(clippy::match_same_arms)] // hurts readability - json!({ - "type": match self { - Self::Pong => 1, - Self::Message(_) => 4, - Self::Defer(_) => 5, - Self::Acknowledge => 6, - Self::UpdateMessage(_) => 7, - Self::Autocomplete(_) => 8, - Self::Modal(_) => 9, - }, - "data": match self { - Self::Pong => Value::Null, - Self::Message(x) => serde_json::to_value(x).map_err(S::Error::custom)?, - Self::Defer(x) => serde_json::to_value(x).map_err(S::Error::custom)?, - Self::Acknowledge => Value::Null, - Self::UpdateMessage(x) => serde_json::to_value(x).map_err(S::Error::custom)?, - Self::Autocomplete(x) => serde_json::to_value(x).map_err(S::Error::custom)?, - Self::Modal(x) => serde_json::to_value(x).map_err(S::Error::custom)?, - } - }) - .serialize(serializer) + use serde::ser::SerializeMap as _; + + let mut map = serializer.serialize_map(Some(2))?; + map.serialize_entry("type", &match self { + Self::Pong => 1, + Self::Message(_) => 4, + Self::Defer(_) => 5, + Self::Acknowledge => 6, + Self::UpdateMessage(_) => 7, + Self::Autocomplete(_) => 8, + Self::Modal(_) => 9, + })?; + + match self { + Self::Pong => map.serialize_entry("data", &None::<()>)?, + Self::Message(x) => map.serialize_entry("data", &x)?, + Self::Defer(x) => map.serialize_entry("data", &x)?, + Self::Acknowledge => map.serialize_entry("data", &None::<()>)?, + Self::UpdateMessage(x) => map.serialize_entry("data", &x)?, + Self::Autocomplete(x) => map.serialize_entry("data", &x)?, + Self::Modal(x) => map.serialize_entry("data", &x)?, + } + + map.end() } } diff --git a/src/http/client.rs b/src/http/client.rs index 564f62bb384..9ab6ede199e 100644 --- a/src/http/client.rs +++ b/src/http/client.rs @@ -16,7 +16,7 @@ use reqwest::{Client, ClientBuilder, Response as ReqwestResponse, StatusCode}; use secrecy::{ExposeSecret as _, Secret}; use serde::de::DeserializeOwned; use serde::ser::SerializeSeq as _; -use serde_json::{from_value, json, to_string, to_vec}; +use serde_json::{from_value, to_string, to_vec}; use to_arraystring::ToArrayString as _; use tracing::{debug, warn}; @@ -938,15 +938,24 @@ impl Http { sku_id: SkuId, owner: EntitlementOwner, ) -> Result { + #[derive(serde::Serialize)] + struct TestEntitlement { + sku_id: SkuId, + owner_id: u64, + owner_type: u8, + } + let (owner_id, owner_type) = match owner { EntitlementOwner::Guild(id) => (id.get(), 1), EntitlementOwner::User(id) => (id.get(), 2), }; - let map = json!({ - "sku_id": sku_id, - "owner_id": owner_id, - "owner_type": owner_type - }); + + let map = TestEntitlement { + sku_id, + owner_id, + owner_type, + }; + self.fire(Request { body: Some(to_vec(&map)?), multipart: None, diff --git a/src/model/application/component_interaction.rs b/src/model/application/component_interaction.rs index cd7bb6f4447..73cd80edbd3 100644 --- a/src/model/application/component_interaction.rs +++ b/src/model/application/component_interaction.rs @@ -1,6 +1,6 @@ use serde::de::Error as DeError; -use serde::ser::{Error as _, Serialize}; -use serde_json::{from_value, json}; +use serde::ser::{Serialize, SerializeMap as _}; +use serde_json::from_value; #[cfg(feature = "model")] use crate::builder::{ @@ -297,27 +297,29 @@ impl<'de> Deserialize<'de> for ComponentInteractionDataKind { } impl Serialize for ComponentInteractionDataKind { + #[rustfmt::skip] // Remove this for horror. fn serialize(&self, serializer: S) -> StdResult { - json!({ - "component_type": match self { - Self::Button { .. } => 2, - Self::StringSelect { .. } => 3, - Self::UserSelect { .. } => 5, - Self::RoleSelect { .. } => 6, - Self::MentionableSelect { .. } => 7, - Self::ChannelSelect { .. } => 8, - Self::Unknown(x) => *x, - }, - "values": match self { - Self::StringSelect { values } => serde_json::to_value(values).map_err(S::Error::custom)?, - Self::UserSelect { values } => serde_json::to_value(values).map_err(S::Error::custom)?, - Self::RoleSelect { values } => serde_json::to_value(values).map_err(S::Error::custom)?, - Self::MentionableSelect { values } => serde_json::to_value(values).map_err(S::Error::custom)?, - Self::ChannelSelect { values } => serde_json::to_value(values).map_err(S::Error::custom)?, - Self::Button | Self::Unknown(_) => Value::Null, - }, - }) - .serialize(serializer) + let mut map = serializer.serialize_map(Some(2))?; + map.serialize_entry("component_type", &match self { + Self::Button { .. } => 2, + Self::StringSelect { .. } => 3, + Self::UserSelect { .. } => 5, + Self::RoleSelect { .. } => 6, + Self::MentionableSelect { .. } => 7, + Self::ChannelSelect { .. } => 8, + Self::Unknown(x) => *x, + })?; + + match self { + Self::StringSelect { values } => map.serialize_entry("values", values)?, + Self::UserSelect { values } => map.serialize_entry("values", values)?, + Self::RoleSelect { values } => map.serialize_entry("values", values)?, + Self::MentionableSelect { values } => map.serialize_entry("values", values)?, + Self::ChannelSelect { values } => map.serialize_entry("values", values)?, + Self::Button | Self::Unknown(_) => map.serialize_entry("values", &None::<()>)?, + }; + + map.end() } }