Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove more json! usage #2986

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions src/builder/create_components.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::borrow::Cow;

use serde::Serialize;
use serde_json::json;

use crate::model::prelude::*;

Expand Down Expand Up @@ -31,19 +30,20 @@ impl<'a> CreateActionRow<'a> {
}
}

impl<'a> serde::Serialize for CreateActionRow<'a> {
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) => 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()
}
}

Expand Down Expand Up @@ -191,12 +191,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
50 changes: 24 additions & 26 deletions src/builder/create_interaction_response.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::borrow::Cow;
use std::collections::HashMap;

use serde_json::json;

use super::{
CreateActionRow,
CreateAllowedMentions,
Expand Down Expand Up @@ -61,30 +59,30 @@ pub enum CreateInteractionResponse<'a> {

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

Expand Down
21 changes: 15 additions & 6 deletions src/http/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 tracing::{debug, trace};

use super::multipart::{Multipart, MultipartUpload};
Expand Down Expand Up @@ -937,15 +937,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
46 changes: 24 additions & 22 deletions src/model/application/component_interaction.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -297,27 +297,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 } => 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()
}
}

Expand Down
Loading