Skip to content

Commit

Permalink
refactor(json)!: generalize encoding/decoding
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Api::encode_params is gone

Its was likely only meant for internal use anyway
  • Loading branch information
EdJoPaTo committed Sep 10, 2024
1 parent 65ce914 commit 6827b9a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 36 deletions.
19 changes: 4 additions & 15 deletions src/api/async_telegram_api_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ impl AsyncApi {
Self::builder().api_url(api_url).build()
}

pub fn encode_params<Params>(params: &Params) -> Result<String, Error>
where
Params: serde::ser::Serialize + std::fmt::Debug,
{
serde_json::to_string(params).map_err(|err| Error::Encode(format!("{err:?} : {params:?}")))
}

pub async fn decode_response<Output>(response: reqwest::Response) -> Result<Output, Error>
where
Output: serde::de::DeserializeOwned,
Expand All @@ -45,18 +38,14 @@ impl AsyncApi {
match response.text().await {
Ok(message) => {
if status_code == 200 {
Ok(Self::parse_json(&message)?)
Ok(crate::json::decode(&message)?)
} else {
Err(Error::Api(Self::parse_json(&message)?))
Err(Error::Api(crate::json::decode(&message)?))
}
}
Err(error) => Err(Error::Decode(format!("{error:?}"))),
}
}

fn parse_json<Output: serde::de::DeserializeOwned>(body: &str) -> Result<Output, Error> {
serde_json::from_str(body).map_err(|error| Error::Decode(format!("{error:?} : {body:?}")))
}
}

impl From<reqwest::Error> for Error {
Expand Down Expand Up @@ -88,7 +77,7 @@ impl AsyncTelegramApi for AsyncApi {
.post(url)
.header("Content-Type", "application/json");
if let Some(params) = params {
let json_string = Self::encode_params(&params)?;
let json_string = crate::json::encode(&params)?;
prepared_request = prepared_request.body(json_string);
};
let response = prepared_request.send().await?;
Expand All @@ -105,7 +94,7 @@ impl AsyncTelegramApi for AsyncApi {
Params: serde::ser::Serialize + std::fmt::Debug + std::marker::Send,
Output: serde::de::DeserializeOwned,
{
let json_string = Self::encode_params(&params)?;
let json_string = crate::json::encode(&params)?;
let json_struct: Value = serde_json::from_str(&json_string).unwrap();
let file_keys: Vec<&str> = files.iter().map(|(key, _)| *key).collect();
let files_with_paths: Vec<(String, &str, String)> = files
Expand Down
11 changes: 2 additions & 9 deletions src/api/telegram_api_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@ impl Api {
Self::builder().api_url(api_url).build()
}

pub fn encode_params<Params>(params: &Params) -> Result<String, Error>
where
Params: serde::ser::Serialize + std::fmt::Debug,
{
serde_json::to_string(params).map_err(|err| Error::Encode(format!("{err:?} : {params:?}")))
}

pub fn decode_response<Output>(response: Response) -> Result<Output, Error>
where
Output: serde::de::DeserializeOwned,
Expand Down Expand Up @@ -83,7 +76,7 @@ impl TelegramApi for Api {
let response = match params {
None => prepared_request.call()?,
Some(data) => {
let json = Self::encode_params(&data)?;
let json = crate::json::encode(&data)?;
prepared_request.send_string(&json)?
}
};
Expand All @@ -100,7 +93,7 @@ impl TelegramApi for Api {
Params: serde::ser::Serialize + std::fmt::Debug,
Output: serde::de::DeserializeOwned,
{
let json_string = Self::encode_params(&params)?;
let json_string = crate::json::encode(&params)?;
let json_struct: Value = serde_json::from_str(&json_string).unwrap();
let file_keys: Vec<&str> = files.iter().map(|(key, _)| *key).collect();
let files_with_names: Vec<(&str, Option<&str>, PathBuf)> = files
Expand Down
15 changes: 15 additions & 0 deletions src/json.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use crate::Error;

pub fn decode<T>(string: &str) -> Result<T, Error>
where
T: serde::de::DeserializeOwned,
{
serde_json::from_str(string).map_err(|error| Error::Decode(format!("{error:?} : {string:?}")))
}

pub fn encode<T>(value: &T) -> Result<String, Error>
where
T: serde::ser::Serialize + std::fmt::Debug,
{
serde_json::to_string(value).map_err(|error| Error::Encode(format!("{error:?} : {value:?}")))
}
17 changes: 5 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
#[cfg(any(feature = "http-client", feature = "async-http-client"))]
pub mod api;

#[cfg(any(feature = "telegram-trait", feature = "async-telegram-trait"))]
pub mod api_traits;

#[cfg(any(feature = "http-client", feature = "async-http-client"))]
pub use api::*;

#[cfg(any(feature = "telegram-trait", feature = "async-telegram-trait"))]
pub use api_traits::*;

#[doc(hidden)]
#[cfg(feature = "async-http-client")]
pub use reqwest;
Expand All @@ -18,10 +6,15 @@ pub use reqwest;
#[cfg(feature = "http-client")]
pub use ureq;

pub mod api;
pub mod api_params;
pub mod api_traits;
mod json;
pub mod objects;
mod parse_mode;

pub use api::*;
pub use api_params::*;
pub use api_traits::*;
pub use objects::*;
pub use parse_mode::*;

0 comments on commit 6827b9a

Please sign in to comment.