diff --git a/CHANGELOG.md b/CHANGELOG.md index fe4295a1..c14b1f66 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ Improvements: * Add types for User-Interactive Authentication API: `r0::uiaa::{AuthFlow, UiaaInfo, UiaaResponse}` * Add missing serde attributes to `get_content_thumbnail` query parameters * Add missing `state` response field to `r0::message::get_message_events` +* Normalize `serde_json` imports # 0.7.2 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 6de82d34..3095dfbe 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -99,6 +99,23 @@ use std::convert::TryFrom; use std::fmt::{Debug, Display, Error as FmtError, Formatter}; ``` +### Serde Imports + +When importing methods and types from `serde_json`, methods should be such as +`serde_json::{from,to}_{slice,string,value,vec}` should be imported as +`{from,to}_json_{slice,string,value,vec}`. + +For example: + +```rust +use serde_json::{ + from_value as from_json_value, + to_str as to_json_str, +}; +``` + +Also, `serde_json::Value` should be imported as `JsonValue`. + ### Code Formatting and Linting Use `rustfmt` to format your code and `clippy` to lint your code. Before diff --git a/src/error.rs b/src/error.rs index 1fb19521..102467ce 100644 --- a/src/error.rs +++ b/src/error.rs @@ -4,6 +4,7 @@ use std::fmt::{self, Display, Formatter}; use ruma_api::{error::ResponseDeserializationError, EndpointError}; use serde::{Deserialize, Serialize}; +use serde_json::{from_slice as from_json_slice, to_vec as to_json_vec}; use strum::{AsRefStr, Display, EnumString}; /// An enum for the error kind. Items may contain additional information. @@ -160,7 +161,7 @@ impl EndpointError for Error { fn try_from_response( response: http::Response>, ) -> Result { - match serde_json::from_slice::(response.body()) { + match from_json_slice::(response.body()) { Ok(error_body) => Ok(error_body.into_error(response.status())), Err(de_error) => Err(ResponseDeserializationError::new(de_error, response)), } @@ -206,7 +207,7 @@ impl From for http::Response> { http::Response::builder() .header(http::header::CONTENT_TYPE, "application/json") .status(error.status_code) - .body(serde_json::to_vec(&ErrorBody::from(error)).unwrap()) + .body(to_json_vec(&ErrorBody::from(error)).unwrap()) .unwrap() } } diff --git a/src/r0/capabilities/get_capabilities.rs b/src/r0/capabilities/get_capabilities.rs index d0d97cca..43f42ff0 100644 --- a/src/r0/capabilities/get_capabilities.rs +++ b/src/r0/capabilities/get_capabilities.rs @@ -2,7 +2,7 @@ use ruma_api::ruma_api; use serde::{Deserialize, Serialize}; -use serde_json::Value; +use serde_json::Value as JsonValue; use std::collections::BTreeMap; ruma_api! { @@ -39,7 +39,7 @@ pub struct Capabilities { /// Any other custom capabilities that the server supports outside of the specification, /// labeled using the Java package naming convention and stored as arbitrary JSON values. #[serde(flatten)] - pub custom_capabilities: BTreeMap, + pub custom_capabilities: BTreeMap, } /// Information about the m.change_password capability diff --git a/src/r0/config/set_global_account_data.rs b/src/r0/config/set_global_account_data.rs index 363f7f6d..adc202d4 100644 --- a/src/r0/config/set_global_account_data.rs +++ b/src/r0/config/set_global_account_data.rs @@ -2,7 +2,7 @@ use ruma_api::ruma_api; use ruma_identifiers::UserId; -use serde_json::Value; +use serde_json::Value as JsonValue; ruma_api! { metadata { @@ -17,7 +17,7 @@ ruma_api! { request { /// Arbitrary JSON to store as config data. #[ruma_api(body)] - pub data: Value, + pub data: JsonValue, /// The event type of the account_data to set. /// /// Custom types should be namespaced to avoid clashes. diff --git a/src/r0/config/set_room_account_data.rs b/src/r0/config/set_room_account_data.rs index 82cc64c4..ff2f78bf 100644 --- a/src/r0/config/set_room_account_data.rs +++ b/src/r0/config/set_room_account_data.rs @@ -2,7 +2,7 @@ use ruma_api::ruma_api; use ruma_identifiers::{RoomId, UserId}; -use serde_json::Value; +use serde_json::Value as JsonValue; ruma_api! { metadata { @@ -17,7 +17,7 @@ ruma_api! { request { /// Arbitrary JSON to store as config data. #[ruma_api(body)] - pub data: Value, + pub data: JsonValue, /// The event type of the account_data to set. /// /// Custom types should be namespaced to avoid clashes. diff --git a/src/r0/directory/get_public_rooms_filtered.rs b/src/r0/directory/get_public_rooms_filtered.rs index 3deb6565..716df562 100644 --- a/src/r0/directory/get_public_rooms_filtered.rs +++ b/src/r0/directory/get_public_rooms_filtered.rs @@ -9,7 +9,8 @@ use serde::{ ser::SerializeStruct, Deserialize, Deserializer, Serialize, Serializer, }; -use serde_json::Value; + +use serde_json::Value as JsonValue; use super::PublicRoomsChunk; @@ -130,7 +131,7 @@ impl<'de> Visitor<'de> for RoomNetworkVisitor { { let mut include_all_networks = false; let mut third_party_instance_id = None; - while let Some((key, value)) = access.next_entry::()? { + while let Some((key, value)) = access.next_entry::()? { match key.as_str() { "include_all_networks" => { include_all_networks = match value.as_bool() { diff --git a/src/r0/filter.rs b/src/r0/filter.rs index 2b8de854..21a1649e 100644 --- a/src/r0/filter.rs +++ b/src/r0/filter.rs @@ -305,14 +305,14 @@ impl<'de> Deserialize<'de> for LazyLoadOptions { #[cfg(test)] mod tests { - use serde_json::json; + use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; use super::LazyLoadOptions; #[test] fn test_serializing_disabled_lazy_load() { let lazy_load_options = LazyLoadOptions::Disabled; - assert_eq!(serde_json::to_value(lazy_load_options).unwrap(), json!({})); + assert_eq!(to_json_value(lazy_load_options).unwrap(), json!({})); } #[test] @@ -321,7 +321,7 @@ mod tests { include_redundant_members: false, }; assert_eq!( - serde_json::to_value(lazy_load_options).unwrap(), + to_json_value(lazy_load_options).unwrap(), json!({ "lazy_load_members": true }) ); } @@ -332,7 +332,7 @@ mod tests { include_redundant_members: true, }; assert_eq!( - serde_json::to_value(lazy_load_options).unwrap(), + to_json_value(lazy_load_options).unwrap(), json!({ "lazy_load_members": true, "include_redundant_members": true }) ); } @@ -341,7 +341,7 @@ mod tests { fn test_deserializing_no_lazy_load() { let json = json!({}); assert_eq!( - serde_json::from_value::(json).unwrap(), + from_json_value::(json).unwrap(), LazyLoadOptions::Disabled, ); } @@ -350,7 +350,7 @@ mod tests { fn test_deserializing_ignore_redundant_members_when_no_lazy_load() { let json = json!({ "include_redundant_members": true }); assert_eq!( - serde_json::from_value::(json).unwrap(), + from_json_value::(json).unwrap(), LazyLoadOptions::Disabled, ); } diff --git a/src/r0/keys/claim_keys.rs b/src/r0/keys/claim_keys.rs index 8a2d7cd2..d894d400 100644 --- a/src/r0/keys/claim_keys.rs +++ b/src/r0/keys/claim_keys.rs @@ -6,7 +6,7 @@ use std::time::Duration; use ruma_api::ruma_api; use ruma_identifiers::{DeviceId, UserId}; -use serde_json::Value; +use serde_json::Value as JsonValue; use super::{AlgorithmAndDeviceId, KeyAlgorithm, OneTimeKey}; @@ -37,7 +37,7 @@ ruma_api! { response { /// If any remote homeservers could not be reached, they are recorded here. /// The names of the properties are the names of the unreachable servers. - pub failures: BTreeMap, + pub failures: BTreeMap, /// One-time keys for the queried devices. pub one_time_keys: BTreeMap>>, diff --git a/src/r0/keys/get_keys.rs b/src/r0/keys/get_keys.rs index 64032e30..74194f38 100644 --- a/src/r0/keys/get_keys.rs +++ b/src/r0/keys/get_keys.rs @@ -4,7 +4,7 @@ use std::{collections::BTreeMap, time::Duration}; use ruma_api::ruma_api; use ruma_identifiers::{DeviceId, UserId}; -use serde_json::Value; +use serde_json::Value as JsonValue; use super::DeviceKeys; @@ -41,7 +41,7 @@ ruma_api! { response { /// If any remote homeservers could not be reached, they are recorded here. /// The names of the properties are the names of the unreachable servers. - pub failures: BTreeMap, + pub failures: BTreeMap, /// Information on the queried devices. pub device_keys: BTreeMap>, diff --git a/src/r0/media/get_media_preview.rs b/src/r0/media/get_media_preview.rs index b517d21d..be407264 100644 --- a/src/r0/media/get_media_preview.rs +++ b/src/r0/media/get_media_preview.rs @@ -3,7 +3,7 @@ use std::time::SystemTime; use ruma_api::ruma_api; -use serde_json::Value; +use serde_json::Value as JsonValue; ruma_api! { metadata { @@ -31,7 +31,7 @@ ruma_api! { /// Differences from OpenGraph: the image size in bytes is added to the `matrix:image:size` /// field, and `og:image` returns the MXC URI to the image, if any. #[ruma_api(body)] - pub data: Option, + pub data: Option, } error: crate::Error diff --git a/src/r0/membership/invite_user.rs b/src/r0/membership/invite_user.rs index 0d0de7bf..9febe0d7 100644 --- a/src/r0/membership/invite_user.rs +++ b/src/r0/membership/invite_user.rs @@ -51,14 +51,17 @@ pub enum InvitationRecipient { #[cfg(test)] mod tests { + use std::convert::TryFrom; + + use ruma_identifiers::UserId; + use serde_json::{from_value as from_json_value, json}; + use super::InvitationRecipient; use crate::r0::{membership::Invite3pid, thirdparty::Medium}; - use ruma_identifiers::UserId; - use std::convert::TryFrom; #[test] fn deserialize_invite_by_user_id() { let incoming = - serde_json::from_str::(r#" { "user_id": "@carl:example.org" } "#) + from_json_value::(json!({ "user_id": "@carl:example.org" })) .unwrap(); let user_id = UserId::try_from("@carl:example.org").unwrap(); let recipient = InvitationRecipient::UserId { user_id }; @@ -67,16 +70,12 @@ mod tests { #[test] fn deserialize_invite_by_3pid() { - let incoming = serde_json::from_str::( - r#" - { - "id_server": "example.org", - "id_access_token": "abcdefghijklmnop", - "medium": "email", - "address": "carl@example.org" - } - "#, - ) + let incoming = from_json_value::(json!({ + "id_server": "example.org", + "id_access_token": "abcdefghijklmnop", + "medium": "email", + "address": "carl@example.org" + })) .unwrap(); let recipient = InvitationRecipient::ThirdPartyId(Invite3pid { id_server: "example.org".to_string(), diff --git a/src/r0/room/create_room.rs b/src/r0/room/create_room.rs index c29cc996..89ca0dfd 100644 --- a/src/r0/room/create_room.rs +++ b/src/r0/room/create_room.rs @@ -4,7 +4,7 @@ use ruma_api::ruma_api; use ruma_events::{room::power_levels::PowerLevelsEventContent, EventJson}; use ruma_identifiers::{RoomId, UserId}; use serde::{Deserialize, Serialize}; -use serde_json::Value; +use serde_json::Value as JsonValue; use super::Visibility; use crate::r0::membership::Invite3pid; @@ -106,5 +106,5 @@ pub struct InitialStateEvent { /// `state_key` of the event to be sent. pub state_key: Option, /// JSON content of the state event. - pub content: Value, + pub content: JsonValue, } diff --git a/src/r0/session/get_login_types.rs b/src/r0/session/get_login_types.rs index 8e323a08..a07da00c 100644 --- a/src/r0/session/get_login_types.rs +++ b/src/r0/session/get_login_types.rs @@ -37,12 +37,14 @@ pub enum LoginType { #[cfg(test)] mod tests { + use serde_json::{from_value as from_json_value, json}; + use super::LoginType; #[test] fn deserialize_login_type() { assert_eq!( - serde_json::from_str::(r#" {"type": "m.login.password"} "#).unwrap(), + from_json_value::(json!({ "type": "m.login.password" })).unwrap(), LoginType::Password, ); } diff --git a/src/r0/session/login.rs b/src/r0/session/login.rs index 0028318f..f9444bf9 100644 --- a/src/r0/session/login.rs +++ b/src/r0/session/login.rs @@ -130,21 +130,17 @@ mod user_serde; mod tests { use std::convert::TryInto; - use serde_json::json; + use serde_json::{from_value as from_json_value, json, Value as JsonValue}; use super::{LoginInfo, Medium, Request, UserInfo}; #[test] fn deserialize_login_type() { assert_eq!( - serde_json::from_str::( - r#" - { - "type": "m.login.password", - "password": "ilovebananas" - } - "#, - ) + from_json_value::(json!({ + "type": "m.login.password", + "password": "ilovebananas" + }),) .unwrap(), LoginInfo::Password { password: "ilovebananas".into() @@ -152,14 +148,10 @@ mod tests { ); assert_eq!( - serde_json::from_str::( - r#" - { - "type": "m.login.token", - "token": "1234567890abcdef" - } - "#, - ) + from_json_value::(json!({ + "type": "m.login.token", + "token": "1234567890abcdef" + }),) .unwrap(), LoginInfo::Token { token: "1234567890abcdef".into() @@ -170,16 +162,12 @@ mod tests { #[test] fn deserialize_user() { assert_eq!( - serde_json::from_str::( - r#" - { - "identifier": { - "type": "m.id.user", - "user": "cheeky_monkey" - } + from_json_value::(json!({ + "identifier": { + "type": "m.id.user", + "user": "cheeky_monkey" } - "#, - ) + })) .unwrap(), UserInfo::MatrixId("cheeky_monkey".into()) ); @@ -201,7 +189,7 @@ mod tests { .try_into() .unwrap(); - let req_body_value: serde_json::Value = serde_json::from_slice(req.body()).unwrap(); + let req_body_value: JsonValue = serde_json::from_slice(req.body()).unwrap(); assert_eq!( req_body_value, json!({ diff --git a/src/r0/state/create_state_event_for_empty_key.rs b/src/r0/state/create_state_event_for_empty_key.rs index e0c0decb..edee60d5 100644 --- a/src/r0/state/create_state_event_for_empty_key.rs +++ b/src/r0/state/create_state_event_for_empty_key.rs @@ -3,7 +3,7 @@ use ruma_api::ruma_api; use ruma_events::EventType; use ruma_identifiers::{EventId, RoomId}; -use serde_json::Value; +use serde_json::Value as JsonValue; ruma_api! { metadata { @@ -24,7 +24,7 @@ ruma_api! { pub event_type: EventType, /// The event's content. #[ruma_api(body)] - pub data: Value, + pub data: JsonValue, } response { diff --git a/src/r0/state/create_state_event_for_key.rs b/src/r0/state/create_state_event_for_key.rs index d377cb98..c52d4915 100644 --- a/src/r0/state/create_state_event_for_key.rs +++ b/src/r0/state/create_state_event_for_key.rs @@ -3,7 +3,7 @@ use ruma_api::ruma_api; use ruma_events::EventType; use ruma_identifiers::{EventId, RoomId}; -use serde_json::Value; +use serde_json::Value as JsonValue; ruma_api! { metadata { @@ -27,7 +27,7 @@ ruma_api! { pub state_key: String, /// The event's content. #[ruma_api(body)] - pub data: Value, + pub data: JsonValue, } response { diff --git a/src/r0/state/get_state_events_for_empty_key.rs b/src/r0/state/get_state_events_for_empty_key.rs index ae8a3619..30747c36 100644 --- a/src/r0/state/get_state_events_for_empty_key.rs +++ b/src/r0/state/get_state_events_for_empty_key.rs @@ -3,7 +3,7 @@ use ruma_api::ruma_api; use ruma_events::EventType; use ruma_identifiers::RoomId; -use serde_json::Value; +use serde_json::Value as JsonValue; ruma_api! { metadata { @@ -27,7 +27,7 @@ ruma_api! { response { /// The content of the state event. #[ruma_api(body)] - pub content: Value, + pub content: JsonValue, } error: crate::Error diff --git a/src/r0/state/get_state_events_for_key.rs b/src/r0/state/get_state_events_for_key.rs index 815980b3..5317be65 100644 --- a/src/r0/state/get_state_events_for_key.rs +++ b/src/r0/state/get_state_events_for_key.rs @@ -3,7 +3,7 @@ use ruma_api::ruma_api; use ruma_events::EventType; use ruma_identifiers::RoomId; -use serde_json::Value; +use serde_json::Value as JsonValue; ruma_api! { metadata { @@ -30,7 +30,7 @@ ruma_api! { response { /// The content of the state event. #[ruma_api(body)] - pub content: Value, + pub content: JsonValue, } error: crate::Error diff --git a/src/r0/uiaa.rs b/src/r0/uiaa.rs index bc6ac20b..3caa8afa 100644 --- a/src/r0/uiaa.rs +++ b/src/r0/uiaa.rs @@ -4,7 +4,7 @@ use std::collections::BTreeMap; use ruma_api::{error::ResponseDeserializationError, EndpointError}; use serde::{Deserialize, Serialize}; -use serde_json::Value as JsonValue; +use serde_json::{from_slice as from_json_slice, to_vec as to_json_vec, Value as JsonValue}; use crate::error::{Error as MatrixError, ErrorBody}; @@ -85,7 +85,7 @@ impl EndpointError for UiaaResponse { response: http::Response>, ) -> Result { if response.status() == http::StatusCode::UNAUTHORIZED { - if let Ok(authentication_info) = serde_json::from_slice::(response.body()) { + if let Ok(authentication_info) = from_json_slice::(response.body()) { return Ok(UiaaResponse::AuthResponse(authentication_info)); } } @@ -100,7 +100,7 @@ impl From for http::Response> { UiaaResponse::AuthResponse(authentication_info) => http::Response::builder() .header(http::header::CONTENT_TYPE, "application/json") .status(&http::StatusCode::UNAUTHORIZED) - .body(serde_json::to_vec(&authentication_info).unwrap()) + .body(to_json_vec(&authentication_info).unwrap()) .unwrap(), UiaaResponse::MatrixError(error) => http::Response::from(error), } @@ -113,7 +113,8 @@ mod tests { use ruma_api::EndpointError; use serde_json::{ - from_value as from_json_value, json, to_value as to_json_value, Value as JsonValue, + from_slice as from_json_slice, from_value as from_json_value, json, + to_value as to_json_value, Value as JsonValue, }; use super::{AuthData, AuthFlow, UiaaInfo, UiaaResponse}; @@ -286,7 +287,7 @@ mod tests { UiaaResponse::AuthResponse(uiaa_info.clone()).into(); assert_eq!( - serde_json::from_slice::(uiaa_response.body()).unwrap(), + from_json_slice::(uiaa_response.body()).unwrap(), uiaa_info, ); assert_eq!(