From 5e06f7b39c2a8413144a0cb442943369f28272ec Mon Sep 17 00:00:00 2001 From: Michael Krasnitski Date: Tue, 26 Sep 2023 20:40:03 -0400 Subject: [PATCH] Remove explicit reference to `serde_json` elsewhere in serenity --- src/builder/create_components.rs | 10 ++-- src/builder/create_interaction_response.rs | 17 ++++--- src/cache/cache_update.rs | 2 +- src/error.rs | 17 ++----- src/http/error.rs | 2 +- src/internal/prelude.rs | 3 -- src/model/application/command_interaction.rs | 51 ++++++++++--------- .../application/component_interaction.rs | 23 ++++----- src/model/colour.rs | 2 +- src/model/guild/audit_log/mod.rs | 4 +- src/model/guild/emoji.rs | 2 +- src/model/guild/guild_id.rs | 5 +- src/model/invite.rs | 4 +- src/utils/message_builder.rs | 2 +- 14 files changed, 67 insertions(+), 77 deletions(-) diff --git a/src/builder/create_components.rs b/src/builder/create_components.rs index ef1087ae2e8..8c1650ccaa4 100644 --- a/src/builder/create_components.rs +++ b/src/builder/create_components.rs @@ -1,6 +1,6 @@ use serde::Serialize; -use crate::json::json; +use crate::json::{self, json}; use crate::model::prelude::*; /// A builder for creating a components action row in a message. @@ -19,12 +19,12 @@ impl serde::Serialize for CreateActionRow { fn serialize(&self, serializer: S) -> Result { use serde::ser::Error as _; - serde_json::json!({ + 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)?, + 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) diff --git a/src/builder/create_interaction_response.rs b/src/builder/create_interaction_response.rs index 0afc9f0f6fa..3349a89ad43 100644 --- a/src/builder/create_interaction_response.rs +++ b/src/builder/create_interaction_response.rs @@ -6,6 +6,7 @@ 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). @@ -57,7 +58,7 @@ impl serde::Serialize for CreateInteractionResponse { use serde::ser::Error as _; #[allow(clippy::match_same_arms)] // hurts readability - serde_json::json!({ + json!({ "type": match self { Self::Pong { .. } => 1, Self::Message { .. } => 4, @@ -68,13 +69,13 @@ impl serde::Serialize for CreateInteractionResponse { Self::Modal { .. } => 9, }, "data": match self { - Self::Pong => serde_json::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 => serde_json::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)?, + 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)?, } }) .serialize(serializer) diff --git a/src/cache/cache_update.rs b/src/cache/cache_update.rs index 21b69cfc6e8..c05faf4efd5 100644 --- a/src/cache/cache_update.rs +++ b/src/cache/cache_update.rs @@ -56,7 +56,7 @@ use super::Cache; /// Some(old_user) /// }, /// Entry::Vacant(entry) => { -/// // We can convert a [`serde_json::Value`] to a User for test +/// // We can convert a [`json::Value`] to a User for test /// // purposes. /// let user = from_value::(json!({ /// "id": self.user_id, diff --git a/src/error.rs b/src/error.rs index 4b66f146834..dc449c3af91 100644 --- a/src/error.rs +++ b/src/error.rs @@ -4,7 +4,6 @@ use std::io::Error as IoError; #[cfg(feature = "http")] use reqwest::{header::InvalidHeaderValue, Error as ReqwestError}; -use serde_json::Error as JsonError; #[cfg(feature = "gateway")] use tokio_tungstenite::tungstenite::error::Error as TungsteniteError; use tracing::instrument; @@ -16,6 +15,7 @@ use crate::gateway::GatewayError; #[cfg(feature = "http")] use crate::http::HttpError; use crate::internal::prelude::*; +use crate::json::JsonError; use crate::model::ModelError; /// The common result type between most library functions. @@ -38,11 +38,9 @@ pub enum Error { Format(FormatError), /// An [`std::io`] error. Io(IoError), - /// An error from the [`serde_json`] crate. + #[cfg_attr(not(feature = "simd_json"), doc = "An error from the [`serde_json`] crate.")] + #[cfg_attr(feature = "simd_json", doc = "An error from the [`simd_json`] crate.")] Json(JsonError), - #[cfg(feature = "simd-json")] - /// An error from the `simd_json` crate. - SimdJson(simd_json::Error), /// An error from the [`model`] module. /// /// [`model`]: crate::model @@ -90,13 +88,6 @@ pub enum Error { Tungstenite(TungsteniteError), } -#[cfg(feature = "simd-json")] -impl From for Error { - fn from(e: simd_json::Error) -> Self { - Error::SimdJson(e) - } -} - impl From for Error { fn from(e: FormatError) -> Error { Error::Format(e) @@ -167,8 +158,6 @@ impl fmt::Display for Error { Self::Json(inner) => fmt::Display::fmt(&inner, f), Self::Model(inner) => fmt::Display::fmt(&inner, f), Self::Url(msg) => f.write_str(msg), - #[cfg(feature = "simd-json")] - Error::SimdJson(inner) => fmt::Display::fmt(&inner, f), #[cfg(feature = "client")] Self::Client(inner) => fmt::Display::fmt(&inner, f), #[cfg(feature = "gateway")] diff --git a/src/http/error.rs b/src/http/error.rs index 1709e21e283..12b6e5f3575 100644 --- a/src/http/error.rs +++ b/src/http/error.rs @@ -7,7 +7,7 @@ use serde::de::{Deserialize, Deserializer, Error as _}; use url::ParseError as UrlError; use crate::internal::prelude::*; -use crate::json::decode_resp; +use crate::json::*; #[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)] #[non_exhaustive] diff --git a/src/internal/prelude.rs b/src/internal/prelude.rs index b53306fff8d..b0e83a6e385 100644 --- a/src/internal/prelude.rs +++ b/src/internal/prelude.rs @@ -4,9 +4,6 @@ pub use std::result::Result as StdResult; -#[cfg(feature = "simd-json")] -pub use simd_json::{Mutable, Value as ValueTrait, ValueAccess}; - #[cfg(feature = "client")] pub use crate::client::ClientError; pub use crate::error::{Error, Result}; diff --git a/src/model/application/command_interaction.rs b/src/model/application/command_interaction.rs index 05c2474b7f3..30f41d7383b 100644 --- a/src/model/application/command_interaction.rs +++ b/src/model/application/command_interaction.rs @@ -17,6 +17,7 @@ use crate::client::Context; #[cfg(feature = "model")] use crate::http::{CacheHttp, Http}; use crate::internal::prelude::*; +use crate::json::{self, JsonError}; use crate::model::application::{CommandOptionType, CommandType}; use crate::model::channel::{Attachment, Message, PartialChannel}; use crate::model::guild::{Member, PartialMember, Role}; @@ -529,19 +530,20 @@ struct RawCommandDataOption { #[serde(rename = "type")] kind: CommandOptionType, #[serde(skip_serializing_if = "Option::is_none")] - value: Option, + value: Option, #[serde(skip_serializing_if = "Option::is_none")] options: Option>, #[serde(skip_serializing_if = "Option::is_none")] focused: Option, } -fn option_from_raw(raw: RawCommandDataOption) -> StdResult { +fn option_from_raw(raw: RawCommandDataOption) -> Result { macro_rules! value { - () => { - serde_json::from_value(raw.value.ok_or_else(|| DeError::missing_field("value"))?) - .map_err(DeError::custom)? - }; + () => {{ + json::from_value( + raw.value.ok_or_else::(|| DeError::missing_field("value"))?, + )? + }}; } let value = match raw.kind { @@ -554,13 +556,15 @@ fn option_from_raw(raw: RawCommandDataOption) -> StdResult CommandDataOptionValue::Number(value!()), CommandOptionType::String => CommandDataOptionValue::String(value!()), CommandOptionType::SubCommand => { - let options = raw.options.ok_or_else(|| DeError::missing_field("options"))?; - let options = options.into_iter().map(option_from_raw).collect::>()?; + let options = + raw.options.ok_or_else::(|| DeError::missing_field("options"))?; + let options = options.into_iter().map(option_from_raw).collect::>()?; CommandDataOptionValue::SubCommand(options) }, CommandOptionType::SubCommandGroup => { - let options = raw.options.ok_or_else(|| DeError::missing_field("options"))?; - let options = options.into_iter().map(option_from_raw).collect::>()?; + let options = + raw.options.ok_or_else::(|| DeError::missing_field("options"))?; + let options = options.into_iter().map(option_from_raw).collect::>()?; CommandDataOptionValue::SubCommandGroup(options) }, CommandOptionType::Attachment => CommandDataOptionValue::Attachment(value!()), @@ -577,7 +581,7 @@ fn option_from_raw(raw: RawCommandDataOption) -> StdResult StdResult { +fn option_to_raw(option: &CommandDataOption) -> Result { let mut raw = RawCommandDataOption { name: option.name.clone(), kind: option.kind(), @@ -591,22 +595,21 @@ fn option_to_raw(option: &CommandDataOption) -> StdResult { - raw.value = Some(serde_json::to_value(value)?); + raw.value = Some(json::to_value(value)?); raw.focused = Some(true); }, - CommandDataOptionValue::Boolean(v) => raw.value = Some(serde_json::to_value(v)?), - CommandDataOptionValue::Integer(v) => raw.value = Some(serde_json::to_value(v)?), - CommandDataOptionValue::Number(v) => raw.value = Some(serde_json::to_value(v)?), - CommandDataOptionValue::String(v) => raw.value = Some(serde_json::to_value(v)?), + CommandDataOptionValue::Boolean(v) => raw.value = Some(json::to_value(v)?), + CommandDataOptionValue::Integer(v) => raw.value = Some(json::to_value(v)?), + CommandDataOptionValue::Number(v) => raw.value = Some(json::to_value(v)?), + CommandDataOptionValue::String(v) => raw.value = Some(json::to_value(v)?), CommandDataOptionValue::SubCommand(o) | CommandDataOptionValue::SubCommandGroup(o) => { - raw.options = - Some(o.iter().map(option_to_raw).collect::>()?); + raw.options = Some(o.iter().map(option_to_raw).collect::>()?); }, - CommandDataOptionValue::Attachment(v) => raw.value = Some(serde_json::to_value(v)?), - CommandDataOptionValue::Channel(v) => raw.value = Some(serde_json::to_value(v)?), - CommandDataOptionValue::Mentionable(v) => raw.value = Some(serde_json::to_value(v)?), - CommandDataOptionValue::Role(v) => raw.value = Some(serde_json::to_value(v)?), - CommandDataOptionValue::User(v) => raw.value = Some(serde_json::to_value(v)?), + CommandDataOptionValue::Attachment(v) => raw.value = Some(json::to_value(v)?), + CommandDataOptionValue::Channel(v) => raw.value = Some(json::to_value(v)?), + CommandDataOptionValue::Mentionable(v) => raw.value = Some(json::to_value(v)?), + CommandDataOptionValue::Role(v) => raw.value = Some(json::to_value(v)?), + CommandDataOptionValue::User(v) => raw.value = Some(json::to_value(v)?), CommandDataOptionValue::Unknown(_) => {}, } @@ -616,7 +619,7 @@ fn option_to_raw(option: &CommandDataOption) -> StdResult Deserialize<'de> for CommandDataOption { fn deserialize>(deserializer: D) -> StdResult { - option_from_raw(RawCommandDataOption::deserialize(deserializer)?) + option_from_raw(RawCommandDataOption::deserialize(deserializer)?).map_err(D::Error::custom) } } diff --git a/src/model/application/component_interaction.rs b/src/model/application/component_interaction.rs index 132d76044da..505e3eb6898 100644 --- a/src/model/application/component_interaction.rs +++ b/src/model/application/component_interaction.rs @@ -14,6 +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::model::prelude::*; #[cfg(all(feature = "collector", feature = "utils"))] use crate::utils::{CreateQuickModal, QuickModalResponse}; @@ -256,16 +257,14 @@ impl<'de> Deserialize<'de> for ComponentInteractionDataKind { #[derive(Deserialize)] struct Json { component_type: ComponentType, - values: Option, + values: Option, } let json = Json::deserialize(deserializer)?; macro_rules! parse_values { () => { - serde_json::from_value( - json.values.ok_or_else(|| D::Error::missing_field("values"))?, - ) - .map_err(D::Error::custom)? + json::from_value(json.values.ok_or_else(|| D::Error::missing_field("values"))?) + .map_err(D::Error::custom)? }; } @@ -298,7 +297,7 @@ impl<'de> Deserialize<'de> for ComponentInteractionDataKind { impl Serialize for ComponentInteractionDataKind { fn serialize(&self, serializer: S) -> StdResult { - serde_json::json!({ + json!({ "component_type": match self { Self::Button { .. } => 2, Self::StringSelect { .. } => 3, @@ -309,12 +308,12 @@ impl Serialize for ComponentInteractionDataKind { 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(_) => serde_json::Value::Null, + 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) diff --git a/src/model/colour.rs b/src/model/colour.rs index c76f2b8470e..1c0c5715cd0 100644 --- a/src/model/colour.rs +++ b/src/model/colour.rs @@ -13,7 +13,7 @@ /// Passing in a role's colour, and then retrieving its green component via [`Self::g`]: /// /// ```rust -/// # use serde_json::{json, from_value}; +/// # use serenity::json::{json, from_value}; /// # use serenity::model::guild::Role; /// # use serenity::model::id::RoleId; /// # use serenity::model::id::GuildId; diff --git a/src/model/guild/audit_log/mod.rs b/src/model/guild/audit_log/mod.rs index 9bb7dc0564e..0acd81a6d01 100644 --- a/src/model/guild/audit_log/mod.rs +++ b/src/model/guild/audit_log/mod.rs @@ -418,7 +418,7 @@ mod tests { #[test] fn action_serde() { - use serde_json::json; + use crate::json::{self, json}; #[derive(Debug, Deserialize, Serialize)] struct T { @@ -429,7 +429,7 @@ mod tests { "action": 234, }); - let value = serde_json::from_value::(value).unwrap(); + let value = json::from_value::(value).unwrap(); assert_eq!(value.action.num(), 234); assert!(matches!(value.action, Action::Unknown(234))); diff --git a/src/model/guild/emoji.rs b/src/model/guild/emoji.rs index 1d6d1927f03..bc4a1781dc6 100644 --- a/src/model/guild/emoji.rs +++ b/src/model/guild/emoji.rs @@ -65,7 +65,7 @@ impl Emoji { /// Delete a given emoji: /// /// ```rust,no_run - /// # use serde_json::{json, from_value}; + /// # use serenity::json::{json, from_value}; /// # use serenity::framework::standard::{CommandResult, macros::command}; /// # use serenity::client::Context; /// # use serenity::model::prelude::{EmojiId, Emoji}; diff --git a/src/model/guild/guild_id.rs b/src/model/guild/guild_id.rs index 0b86008c93d..24cfab16cff 100644 --- a/src/model/guild/guild_id.rs +++ b/src/model/guild/guild_id.rs @@ -1205,9 +1205,10 @@ impl GuildId { "position": pos, }) }) - .collect::>(); + .collect::>() + .into(); - http.as_ref().edit_guild_channel_positions(self, &Value::from(items)).await + http.as_ref().edit_guild_channel_positions(self, &items).await } /// Returns a list of [`Member`]s in a [`Guild`] whose username or nickname starts with a diff --git a/src/model/invite.rs b/src/model/invite.rs index a7bee23d87e..9e725bdf126 100644 --- a/src/model/invite.rs +++ b/src/model/invite.rs @@ -153,7 +153,7 @@ impl Invite { /// Retrieve the URL for an invite with the code `WxZumR`: /// /// ```rust - /// # use serde_json::{json, from_value}; + /// # use serenity::json::{json, from_value}; /// # use serenity::model::prelude::*; /// # /// # fn main() { @@ -345,7 +345,7 @@ impl RichInvite { /// Retrieve the URL for an invite with the code `WxZumR`: /// /// ```rust - /// # use serde_json::{json, from_value}; + /// # use serenity::json::{json, from_value}; /// # use serenity::model::prelude::*; /// # /// # fn main() { diff --git a/src/utils/message_builder.rs b/src/utils/message_builder.rs index 15e87130e2d..34ea7b6adcd 100644 --- a/src/utils/message_builder.rs +++ b/src/utils/message_builder.rs @@ -132,7 +132,7 @@ impl MessageBuilder { /// Mention an emoji in a message's content: /// /// ```rust - /// # use serde_json::{json, from_value}; + /// # use serenity::json::{json, from_value}; /// # use serenity::model::guild::Emoji; /// # use serenity::model::id::EmojiId; /// # use serenity::utils::MessageBuilder;