Normalize serde_json imports
This commit is contained in:
		
							parent
							
								
									f4b9841590
								
							
						
					
					
						commit
						a761d59a4c
					
				| @ -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 | ||||
| 
 | ||||
|  | ||||
| @ -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 | ||||
|  | ||||
| @ -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<Vec<u8>>, | ||||
|     ) -> Result<Self, ResponseDeserializationError> { | ||||
|         match serde_json::from_slice::<ErrorBody>(response.body()) { | ||||
|         match from_json_slice::<ErrorBody>(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<Error> for http::Response<Vec<u8>> { | ||||
|         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() | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -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<String, Value>, | ||||
|     pub custom_capabilities: BTreeMap<String, JsonValue>, | ||||
| } | ||||
| 
 | ||||
| /// Information about the m.change_password capability
 | ||||
|  | ||||
| @ -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.
 | ||||
|  | ||||
| @ -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.
 | ||||
|  | ||||
| @ -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::<String, Value>()? { | ||||
|         while let Some((key, value)) = access.next_entry::<String, JsonValue>()? { | ||||
|             match key.as_str() { | ||||
|                 "include_all_networks" => { | ||||
|                     include_all_networks = match value.as_bool() { | ||||
|  | ||||
| @ -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::<LazyLoadOptions>(json).unwrap(), | ||||
|             from_json_value::<LazyLoadOptions>(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::<LazyLoadOptions>(json).unwrap(), | ||||
|             from_json_value::<LazyLoadOptions>(json).unwrap(), | ||||
|             LazyLoadOptions::Disabled, | ||||
|         ); | ||||
|     } | ||||
|  | ||||
| @ -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<String, Value>, | ||||
|         pub failures: BTreeMap<String, JsonValue>, | ||||
| 
 | ||||
|         /// One-time keys for the queried devices.
 | ||||
|         pub one_time_keys: BTreeMap<UserId, BTreeMap<DeviceId, BTreeMap<AlgorithmAndDeviceId, OneTimeKey>>>, | ||||
|  | ||||
| @ -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<String, Value>, | ||||
|         pub failures: BTreeMap<String, JsonValue>, | ||||
| 
 | ||||
|         /// Information on the queried devices.
 | ||||
|         pub device_keys: BTreeMap<UserId, BTreeMap<DeviceId, DeviceKeys>>, | ||||
|  | ||||
| @ -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<Value>, | ||||
|         pub data: Option<JsonValue>, | ||||
|     } | ||||
| 
 | ||||
|     error: crate::Error | ||||
|  | ||||
| @ -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::<InvitationRecipient>(r#" { "user_id": "@carl:example.org" } "#) | ||||
|             from_json_value::<InvitationRecipient>(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::<InvitationRecipient>( | ||||
|             r#" | ||||
|                 { | ||||
|         let incoming = from_json_value::<InvitationRecipient>(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(), | ||||
|  | ||||
| @ -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<String>, | ||||
|     /// JSON content of the state event.
 | ||||
|     pub content: Value, | ||||
|     pub content: JsonValue, | ||||
| } | ||||
|  | ||||
| @ -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::<LoginType>(r#" {"type": "m.login.password"} "#).unwrap(), | ||||
|             from_json_value::<LoginType>(json!({ "type": "m.login.password" })).unwrap(), | ||||
|             LoginType::Password, | ||||
|         ); | ||||
|     } | ||||
|  | ||||
| @ -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::<LoginInfo>( | ||||
|                 r#" | ||||
|                 { | ||||
|             from_json_value::<LoginInfo>(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::<LoginInfo>( | ||||
|                 r#" | ||||
|                 { | ||||
|             from_json_value::<LoginInfo>(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::<UserInfo>( | ||||
|                 r#" | ||||
|                 { | ||||
|             from_json_value::<UserInfo>(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!({ | ||||
|  | ||||
| @ -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 { | ||||
|  | ||||
| @ -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 { | ||||
|  | ||||
| @ -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 | ||||
|  | ||||
| @ -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 | ||||
|  | ||||
| @ -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<Vec<u8>>, | ||||
|     ) -> Result<Self, ResponseDeserializationError> { | ||||
|         if response.status() == http::StatusCode::UNAUTHORIZED { | ||||
|             if let Ok(authentication_info) = serde_json::from_slice::<UiaaInfo>(response.body()) { | ||||
|             if let Ok(authentication_info) = from_json_slice::<UiaaInfo>(response.body()) { | ||||
|                 return Ok(UiaaResponse::AuthResponse(authentication_info)); | ||||
|             } | ||||
|         } | ||||
| @ -100,7 +100,7 @@ impl From<UiaaResponse> for http::Response<Vec<u8>> { | ||||
|             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::<UiaaInfo>(uiaa_response.body()).unwrap(), | ||||
|             from_json_slice::<UiaaInfo>(uiaa_response.body()).unwrap(), | ||||
|             uiaa_info, | ||||
|         ); | ||||
|         assert_eq!( | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user