From 53c044e5ac3b94c53e48aceeeb52f066be70a924 Mon Sep 17 00:00:00 2001 From: EdJoPaTo Date: Thu, 16 Jun 2022 07:48:01 +0200 Subject: [PATCH] refactor: adopt breaking lints (#72) This is a breaking change but it improves efficiency or readability - https://rust-lang.github.io/rust-clippy/master/index.html#vec_box - https://rust-lang.github.io/rust-clippy/master/index.html#enum_variant_names - https://rust-lang.github.io/rust-clippy/master/index.html#trivially_copy_pass_by_ref --- src/api.rs | 8 ++++---- src/api/async_telegram_api_impl.rs | 23 ++++++++++------------- src/api/telegram_api_impl.rs | 19 +++++++++---------- src/objects.rs | 10 +++++----- src/parse_mode.rs | 2 +- 5 files changed, 29 insertions(+), 33 deletions(-) diff --git a/src/api.rs b/src/api.rs index 230807e..90f2c05 100644 --- a/src/api.rs +++ b/src/api.rs @@ -17,13 +17,13 @@ pub static BASE_API_URL: &str = "https://api.telegram.org/bot"; #[serde(untagged)] pub enum Error { #[error("{0}")] - HttpError(HttpError), + Http(HttpError), #[error("Api Error {0:?}")] - ApiError(ErrorResponse), + Api(ErrorResponse), #[error("Decode Error {0}")] - DecodeError(String), + Decode(String), #[error("Encode Error {0}")] - EncodeError(String), + Encode(String), } #[derive(Debug, Serialize, Deserialize, PartialEq, Eq, thiserror::Error)] diff --git a/src/api/async_telegram_api_impl.rs b/src/api/async_telegram_api_impl.rs index 672790a..42a5672 100644 --- a/src/api/async_telegram_api_impl.rs +++ b/src/api/async_telegram_api_impl.rs @@ -33,8 +33,7 @@ impl AsyncApi { pub fn encode_params( params: &T, ) -> Result { - serde_json::to_string(params) - .map_err(|e| Error::EncodeError(format!("{:?} : {:?}", e, params))) + serde_json::to_string(params).map_err(|e| Error::Encode(format!("{:?} : {:?}", e, params))) } pub async fn decode_response( @@ -49,10 +48,10 @@ impl AsyncApi { } let error_response: ErrorResponse = Self::parse_json(&message)?; - Err(Error::ApiError(error_response)) + Err(Error::Api(error_response)) } Err(e) => { - let err = Error::DecodeError(format!("Failed to decode response: {:?}", e)); + let err = Error::Decode(format!("Failed to decode response: {:?}", e)); Err(err) } } @@ -65,7 +64,7 @@ impl AsyncApi { Ok(result) => Ok(result), Err(e) => { - let err = Error::DecodeError(format!("{:?} : {:?}", e, body)); + let err = Error::Decode(format!("{:?} : {:?}", e, body)); Err(err) } } @@ -75,14 +74,12 @@ impl AsyncApi { impl From for Error { fn from(error: reqwest::Error) -> Self { let message = error.to_string(); - let code = if let Some(status_code) = error.status() { - status_code.as_u16() - } else { - 500 - }; + let code = error + .status() + .map_or(500, |status_code| status_code.as_u16()); let error = HttpError { code, message }; - Self::HttpError(error) + Self::Http(error) } } @@ -90,7 +87,7 @@ impl From for Error { fn from(error: std::io::Error) -> Self { let message = error.to_string(); - Self::EncodeError(message) + Self::Encode(message) } } @@ -219,7 +216,7 @@ mod async_tests { .create(); let api = AsyncApi::new_url(mockito::server_url()); - if let Err(Error::ApiError(ErrorResponse { + if let Err(Error::Api(ErrorResponse { ok: false, description, error_code: 400, diff --git a/src/api/telegram_api_impl.rs b/src/api/telegram_api_impl.rs index e0cdc24..031e5ea 100644 --- a/src/api/telegram_api_impl.rs +++ b/src/api/telegram_api_impl.rs @@ -41,8 +41,7 @@ impl Api { pub fn encode_params( params: &T, ) -> Result { - serde_json::to_string(params) - .map_err(|e| Error::EncodeError(format!("{:?} : {:?}", e, params))) + serde_json::to_string(params).map_err(|e| Error::Encode(format!("{:?} : {:?}", e, params))) } pub fn decode_response(response: Response) -> Result { @@ -53,13 +52,13 @@ impl Api { match json_result { Ok(result) => Ok(result), Err(e) => { - let err = Error::DecodeError(format!("{:?} : {:?}", e, &message)); + let err = Error::Decode(format!("{:?} : {:?}", e, &message)); Err(err) } } } Err(e) => { - let err = Error::DecodeError(format!("Failed to decode response: {:?}", e)); + let err = Error::Decode(format!("Failed to decode response: {:?}", e)); Err(err) } } @@ -75,23 +74,23 @@ impl From for Error { serde_json::from_str(&message); match json_result { - Ok(result) => Self::ApiError(result), + Ok(result) => Self::Api(result), Err(_) => { let error = HttpError { code, message }; - Self::HttpError(error) + Self::Http(error) } } } Err(_) => { let message = "Failed to decode response".to_string(); let error = HttpError { code, message }; - Self::HttpError(error) + Self::Http(error) } }, ureq::Error::Transport(transport_error) => { let message = format!("{:?}", transport_error); let error = HttpError { message, code: 500 }; - Self::HttpError(error) + Self::Http(error) } } } @@ -324,7 +323,7 @@ mod tests { .create(); let api = Api::new_url(mockito::server_url()); - if let Err(Error::ApiError(ErrorResponse { + if let Err(Error::Api(ErrorResponse { ok: false, description, error_code: 400, @@ -1709,7 +1708,7 @@ mod tests { let response = api.get_updates(¶ms); assert_eq!( - Err(Error::DecodeError("Error(\"key must be a string\", line: 1, column: 2) : \"{hey this json is invalid}\"".to_string())), + Err(Error::Decode("Error(\"key must be a string\", line: 1, column: 2) : \"{hey this json is invalid}\"".to_string())), response ); } diff --git a/src/objects.rs b/src/objects.rs index 0302de1..45a2833 100644 --- a/src/objects.rs +++ b/src/objects.rs @@ -545,7 +545,7 @@ pub struct Message { #[serde(skip_serializing_if = "Option::is_none")] #[builder(setter(into, strip_option), default)] - pub entities: Option>>, + pub entities: Option>, #[serde(skip_serializing_if = "Option::is_none")] #[builder(setter(into, strip_option), default)] @@ -561,7 +561,7 @@ pub struct Message { #[serde(skip_serializing_if = "Option::is_none")] #[builder(setter(into, strip_option), default)] - pub photo: Option>>, + pub photo: Option>, #[serde(skip_serializing_if = "Option::is_none")] #[builder(setter(into, strip_option), default)] @@ -585,7 +585,7 @@ pub struct Message { #[serde(skip_serializing_if = "Option::is_none")] #[builder(setter(into, strip_option), default)] - pub caption_entities: Option>>, + pub caption_entities: Option>, #[serde(skip_serializing_if = "Option::is_none")] #[builder(setter(into, strip_option), default)] @@ -613,7 +613,7 @@ pub struct Message { #[serde(skip_serializing_if = "Option::is_none")] #[builder(setter(into, strip_option), default)] - pub new_chat_members: Option>>, + pub new_chat_members: Option>, #[serde(skip_serializing_if = "Option::is_none")] #[builder(setter(into, strip_option), default)] @@ -625,7 +625,7 @@ pub struct Message { #[serde(skip_serializing_if = "Option::is_none")] #[builder(setter(into, strip_option), default)] - pub new_chat_photo: Option>>, + pub new_chat_photo: Option>, #[serde(skip_serializing_if = "Option::is_none")] #[builder(setter(into, strip_option), default)] diff --git a/src/parse_mode.rs b/src/parse_mode.rs index 09c15ed..0b2f7a1 100644 --- a/src/parse_mode.rs +++ b/src/parse_mode.rs @@ -28,7 +28,7 @@ impl FromStr for ParseMode { } impl ParseMode { - pub const fn to_str(&self) -> &'static str { + pub const fn to_str(self) -> &'static str { match self { ParseMode::Html => "HTML", ParseMode::MarkdownV2 => "MarkdownV2",