Skip to content

Commit

Permalink
refactor: adopt breaking lints (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
EdJoPaTo authored Jun 16, 2022
1 parent 237a1e3 commit 53c044e
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 33 deletions.
8 changes: 4 additions & 4 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
23 changes: 10 additions & 13 deletions src/api/async_telegram_api_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ impl AsyncApi {
pub fn encode_params<T: serde::ser::Serialize + std::fmt::Debug>(
params: &T,
) -> Result<String, Error> {
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<T: serde::de::DeserializeOwned>(
Expand All @@ -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)
}
}
Expand All @@ -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)
}
}
Expand All @@ -75,22 +74,20 @@ impl AsyncApi {
impl From<reqwest::Error> 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)
}
}

impl From<std::io::Error> for Error {
fn from(error: std::io::Error) -> Self {
let message = error.to_string();

Self::EncodeError(message)
Self::Encode(message)
}
}

Expand Down Expand Up @@ -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,
Expand Down
19 changes: 9 additions & 10 deletions src/api/telegram_api_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ impl Api {
pub fn encode_params<T: serde::ser::Serialize + std::fmt::Debug>(
params: &T,
) -> Result<String, Error> {
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<T: serde::de::DeserializeOwned>(response: Response) -> Result<T, Error> {
Expand All @@ -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)
}
}
Expand All @@ -75,23 +74,23 @@ impl From<ureq::Error> 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)
}
}
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -1709,7 +1708,7 @@ mod tests {
let response = api.get_updates(&params);

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
);
}
Expand Down
10 changes: 5 additions & 5 deletions src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ pub struct Message {

#[serde(skip_serializing_if = "Option::is_none")]
#[builder(setter(into, strip_option), default)]
pub entities: Option<Vec<Box<MessageEntity>>>,
pub entities: Option<Vec<MessageEntity>>,

#[serde(skip_serializing_if = "Option::is_none")]
#[builder(setter(into, strip_option), default)]
Expand All @@ -561,7 +561,7 @@ pub struct Message {

#[serde(skip_serializing_if = "Option::is_none")]
#[builder(setter(into, strip_option), default)]
pub photo: Option<Vec<Box<PhotoSize>>>,
pub photo: Option<Vec<PhotoSize>>,

#[serde(skip_serializing_if = "Option::is_none")]
#[builder(setter(into, strip_option), default)]
Expand All @@ -585,7 +585,7 @@ pub struct Message {

#[serde(skip_serializing_if = "Option::is_none")]
#[builder(setter(into, strip_option), default)]
pub caption_entities: Option<Vec<Box<MessageEntity>>>,
pub caption_entities: Option<Vec<MessageEntity>>,

#[serde(skip_serializing_if = "Option::is_none")]
#[builder(setter(into, strip_option), default)]
Expand Down Expand Up @@ -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<Vec<Box<User>>>,
pub new_chat_members: Option<Vec<User>>,

#[serde(skip_serializing_if = "Option::is_none")]
#[builder(setter(into, strip_option), default)]
Expand All @@ -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<Vec<Box<PhotoSize>>>,
pub new_chat_photo: Option<Vec<PhotoSize>>,

#[serde(skip_serializing_if = "Option::is_none")]
#[builder(setter(into, strip_option), default)]
Expand Down
2 changes: 1 addition & 1 deletion src/parse_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 53c044e

Please sign in to comment.