telexide_fork/api/
response.rs1use crate::utils::result::{Result, TelegramError};
2use serde::{Deserialize, Serialize};
3
4#[derive(Serialize, Deserialize, Debug, Clone)]
6pub struct Response {
7 pub ok: bool,
8 pub description: Option<String>,
9 pub result: Option<serde_json::Value>,
10}
11
12impl<T> From<Response> for Result<T>
13where
14 T: serde::de::DeserializeOwned,
15{
16 fn from(resp: Response) -> Result<T> {
17 if resp.ok {
18 Ok(serde_json::from_value(resp.result.ok_or_else(|| {
19 TelegramError::Unknown("response had no result".to_owned())
20 })?)?)
21 } else if resp.description.is_some() {
22 Err(TelegramError::APIResponseError(
23 resp.description
24 .unwrap_or_else(|| "api error does not contain description".to_owned()),
25 )
26 .into())
27 } else {
28 Err(TelegramError::Unknown(
29 "got error without description from the telegram api".to_owned(),
30 )
31 .into())
32 }
33 }
34}