Merge pull request #61 from teloxide/small-changes

Fix SemiparsedVec
This commit is contained in:
Hirrolot 2021-03-13 21:45:37 +06:00 committed by GitHub
commit 68913d7d48
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 2 deletions

View file

@ -37,6 +37,7 @@ tokio-util = "0.6.0"
pin-project = "1.0.3"
bytes = "1.0.0"
reqwest = { version = "0.11.0", features = ["json", "stream", "multipart"] }
log = "0.4"
serde = { version = "1.0.114", features = ["derive"] }
serde_json = "1.0.55"

View file

@ -188,10 +188,10 @@ mod passport_data;
mod passport_element_error;
mod passport_file;
pub use non_telegram_types::{country_code::*, currency::*, non_strict_vec::*};
pub use non_telegram_types::{country_code::*, currency::*, semiparsed_vec::*};
mod non_telegram_types {
pub(super) mod country_code;
pub(super) mod currency;
pub(crate) mod mime;
pub(super) mod non_strict_vec;
pub(super) mod semiparsed_vec;
}

View file

@ -6,6 +6,7 @@ use crate::types::{
CallbackQuery, Chat, ChosenInlineResult, InlineQuery, Message, Poll, PollAnswer,
PreCheckoutQuery, ShippingQuery, User,
};
use serde_json::Value;
/// This [object] represents an incoming update.
///
@ -28,6 +29,27 @@ pub struct Update {
pub kind: UpdateKind,
}
impl Update {
/// Tries to parse `value` into `Update`, logging an error on failure.
///
/// It is used to implement update listeners.
pub fn try_parse(value: &Value) -> Result<Self, serde_json::Error> {
match serde_json::from_value(value.clone()) {
Ok(update) => Ok(update),
Err(error) => {
log::error!(
"Cannot parse an update.\nError: {:?}\nValue: {}\n\
This is a bug in teloxide-core, please open an issue here: \
https://github.com/teloxide/teloxide-core/issues.",
error,
value
);
Err(error)
}
}
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum UpdateKind {