Merge branch 'master' into impl_payload_macro

This commit is contained in:
Waffle Lapkin 2020-10-24 21:16:05 +03:00 committed by GitHub
commit a917363da0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 430 additions and 507 deletions

View file

@ -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),
}

View file

@ -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),
}
}
}

View file

@ -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")
);
}
}