Skip to content

Commit

Permalink
Backport: Remove more json! usage (#2989)
Browse files Browse the repository at this point in the history
  • Loading branch information
GnomedDev authored Oct 8, 2024
1 parent 29e6d97 commit 57af3e0
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 69 deletions.
32 changes: 19 additions & 13 deletions src/builder/create_components.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use serde::Serialize;

use crate::json::{self, json};
use crate::model::prelude::*;

/// A builder for creating a components action row in a message.
Expand All @@ -17,17 +16,18 @@ pub enum CreateActionRow {

impl serde::Serialize for CreateActionRow {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
use serde::ser::Error as _;

json!({
"type": 1,
"components": match self {
Self::Buttons(x) => json::to_value(x).map_err(S::Error::custom)?,
Self::SelectMenu(x) => json::to_value(vec![x]).map_err(S::Error::custom)?,
Self::InputText(x) => 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()
}
}

Expand Down Expand Up @@ -139,12 +139,18 @@ struct CreateSelectMenuDefault(Mention);

impl Serialize for CreateSelectMenuDefault {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
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()
}
}

Expand Down
55 changes: 27 additions & 28 deletions src/builder/create_interaction_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use crate::constants;
#[cfg(feature = "http")]
use crate::http::CacheHttp;
use crate::internal::prelude::*;
use crate::json::{self, json};
use crate::model::prelude::*;

/// [Discord docs](https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-response-object).
Expand Down Expand Up @@ -68,34 +67,34 @@ pub enum CreateInteractionResponse {
}

impl serde::Serialize for CreateInteractionResponse {
#[allow(deprecated)] // We have to cover deprecated variants
fn serialize<S: serde::Serializer>(&self, serializer: S) -> StdResult<S::Ok, S::Error> {
use serde::ser::Error as _;

#[allow(deprecated)]
#[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,
Self::PremiumRequired => 10,
},
"data": match self {
Self::Pong => json::NULL,
Self::Message(x) => json::to_value(x).map_err(S::Error::custom)?,
Self::Defer(x) => json::to_value(x).map_err(S::Error::custom)?,
Self::Acknowledge => json::NULL,
Self::UpdateMessage(x) => json::to_value(x).map_err(S::Error::custom)?,
Self::Autocomplete(x) => json::to_value(x).map_err(S::Error::custom)?,
Self::Modal(x) => json::to_value(x).map_err(S::Error::custom)?,
Self::PremiumRequired => json::NULL,
}
})
.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,
Self::PremiumRequired => 10,
})?;

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)?,
Self::PremiumRequired => map.serialize_entry("data", &None::<()>)?,
}

map.end()
}
}

Expand Down
21 changes: 15 additions & 6 deletions src/http/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -967,15 +967,24 @@ impl Http {
sku_id: SkuId,
owner: EntitlementOwner,
) -> Result<Entitlement> {
#[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,
Expand Down Expand Up @@ -3212,7 +3221,7 @@ impl Http {
after: Option<UserId>,
limit: Option<u8>,
) -> Result<Vec<User>> {
#[derive(serde::Deserialize)]
#[derive(Deserialize)]
struct VotersResponse {
users: Vec<User>,
}
Expand Down
46 changes: 24 additions & 22 deletions src/model/application/component_interaction.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use serde::de::Error as DeError;
use serde::ser::{Error as _, Serialize};
use serde::ser::{Serialize, SerializeMap as _};

#[cfg(feature = "model")]
use crate::builder::{
Expand All @@ -14,7 +14,7 @@ use crate::client::Context;
#[cfg(feature = "model")]
use crate::http::{CacheHttp, Http};
use crate::internal::prelude::*;
use crate::json::{self, json};
use crate::json;
use crate::model::prelude::*;
#[cfg(all(feature = "collector", feature = "utils"))]
use crate::utils::{CreateQuickModal, QuickModalResponse};
Expand Down Expand Up @@ -307,27 +307,29 @@ impl<'de> Deserialize<'de> for ComponentInteractionDataKind {
}

impl Serialize for ComponentInteractionDataKind {
#[rustfmt::skip] // Remove this for horror.
fn serialize<S: serde::Serializer>(&self, serializer: S) -> StdResult<S::Ok, S::Error> {
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 } => json::to_value(values).map_err(S::Error::custom)?,
Self::UserSelect { values } => json::to_value(values).map_err(S::Error::custom)?,
Self::RoleSelect { values } => json::to_value(values).map_err(S::Error::custom)?,
Self::MentionableSelect { values } => json::to_value(values).map_err(S::Error::custom)?,
Self::ChannelSelect { values } => json::to_value(values).map_err(S::Error::custom)?,
Self::Button | Self::Unknown(_) => json::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()
}
}

Expand Down

0 comments on commit 57af3e0

Please sign in to comment.