mirror of
https://github.com/teloxide/teloxide.git
synced 2025-01-11 12:31:14 +01:00
Merge branch 'master' into impl_payload_macro
This commit is contained in:
commit
a917363da0
3 changed files with 430 additions and 507 deletions
|
@ -57,22 +57,10 @@ pub enum RequestError {
|
|||
Io(#[source] io::Error),
|
||||
}
|
||||
|
||||
serde_or_unknown! {
|
||||
#[unknown_mod = api_error_internals]
|
||||
#[unknown_kind = Kind]
|
||||
#[unknown_path = "api_error_internals::Kind"]
|
||||
|
||||
/// A kind of an API error.
|
||||
#[derive(Debug, Deserialize, PartialEq, Hash, Eq, Clone)]
|
||||
pub enum ApiError {
|
||||
#[unknown]
|
||||
/// Error which is not known to `teloxide`.
|
||||
///
|
||||
/// If you've received this error, please [open an issue] with the description of the error.
|
||||
///
|
||||
/// [open an issue]: https://github.com/teloxide/teloxide/issues/new
|
||||
Unknown(String),
|
||||
|
||||
/// A kind of an API error.
|
||||
#[derive(Debug, Deserialize, PartialEq, Hash, Eq, Clone)]
|
||||
#[serde(field_identifier)]
|
||||
pub enum ApiError {
|
||||
/// Occurs when the bot tries to send message to user who blocked the bot.
|
||||
#[serde(rename = "Forbidden: bot was blocked by the user")]
|
||||
BotBlocked,
|
||||
|
@ -532,5 +520,12 @@ serde_or_unknown! {
|
|||
/// [`GetFile`]: crate::requests::GetFile
|
||||
#[serde(rename = "Bad Request: invalid file id")]
|
||||
FileIdInvalid,
|
||||
}
|
||||
|
||||
/// Error which is not known to `teloxide`.
|
||||
///
|
||||
/// If you've received this error, please [open an issue] with the
|
||||
/// description of the error.
|
||||
///
|
||||
/// [open an issue]: https://github.com/teloxide/teloxide/issues/new
|
||||
Unknown(String),
|
||||
}
|
||||
|
|
|
@ -307,85 +307,3 @@ macro_rules! impl_payload {
|
|||
$T
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_use]
|
||||
macro_rules! serde_or_unknown {
|
||||
(
|
||||
#[unknown_mod = $mod_:ident]
|
||||
#[unknown_kind = $Kind:ident]
|
||||
#[unknown_path = $path:literal]
|
||||
$(
|
||||
#[ $($meta:tt)* ]
|
||||
)*
|
||||
$v:vis enum $Name:ident {
|
||||
#[unknown]
|
||||
$(
|
||||
#[ $($unknown_meta:tt)* ]
|
||||
)*
|
||||
$Unknown:ident(String)
|
||||
|
||||
$(
|
||||
,
|
||||
$(
|
||||
#[ $($var_meta:tt)* ]
|
||||
)*
|
||||
$Var:ident
|
||||
)*
|
||||
$(,)?
|
||||
}
|
||||
) => {
|
||||
mod $mod_ {
|
||||
#[allow(unused_imports)]
|
||||
use super::{*, $Name as _};
|
||||
|
||||
$(
|
||||
#[ $($meta)* ]
|
||||
)*
|
||||
$v enum $Name {
|
||||
$(
|
||||
$(
|
||||
#[ $($var_meta)* ]
|
||||
)*
|
||||
$Var,
|
||||
)*
|
||||
}
|
||||
|
||||
#[derive(::serde::Deserialize)]
|
||||
#[serde(untagged)]
|
||||
$v enum $Kind {
|
||||
Known($Name),
|
||||
Unknown(String),
|
||||
}
|
||||
|
||||
impl ::core::convert::From<$Kind> for super::$Name {
|
||||
fn from(kind: $Kind) -> Self {
|
||||
match kind {
|
||||
$Kind::Unknown(string) => Self::Unknown(string),
|
||||
$Kind::Known(known) => match known {
|
||||
$(
|
||||
$Name::$Var => Self::$Var,
|
||||
)*
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$(
|
||||
#[ $($meta)* ]
|
||||
)*
|
||||
#[serde(from = $path)]
|
||||
$v enum $Name {
|
||||
$(
|
||||
$(
|
||||
#[ $($var_meta)* ]
|
||||
)*
|
||||
$Var,
|
||||
)*
|
||||
$(
|
||||
#[ $($unknown_meta)* ]
|
||||
)*
|
||||
$Unknown(String),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -66,4 +66,14 @@ mod tests {
|
|||
matches!(val, TelegramResponse::Err { error: ApiError::TerminatedByOtherGetUpdates, .. })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_unknown() {
|
||||
let s = r#"{"ok":false,"error_code":111,"description":"Unknown description that won't match anything"}"#;
|
||||
let val = serde_json::from_str::<TelegramResponse<Update>>(s).unwrap();
|
||||
|
||||
assert!(
|
||||
matches!(val, TelegramResponse::Err { error: ApiError::Unknown(s), .. } if s == "Unknown description that won't match anything")
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue