From 1c79703acbc29cea0bfbb35c5d933f5ecb5c4af2 Mon Sep 17 00:00:00 2001 From: Volodymyr Kostyrko Date: Mon, 19 Aug 2024 11:50:10 +0300 Subject: [PATCH] make `from` optional in message as it should be expose ErrorKind --- README.md | 6 +++++- lib/examples/features.rs | 38 +++++++++++++++++++++++++------------- lib/examples/send.rs | 4 +++- lib/examples/simple.rs | 18 +++++++++++------- lib/examples/tracing.rs | 12 +++++++----- lib/src/lib.rs | 2 +- raw/src/types/message.rs | 7 ++----- 7 files changed, 54 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 890af2e54d..b1340ee4ee 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,11 @@ async fn main() -> Result<(), Error> { if let UpdateKind::Message(message) = update.kind { if let MessageKind::Text { ref data, .. } = message.kind { // Print received text message to stdout. - println!("<{}>: {}", &message.from.first_name, data); + if let Some(from) = &message.from { + println!("<{}>: {}", from.first_name, data); + } else { + println!(": {}", data); + } // Answer message with "Hi". api.send(message.text_reply(format!( diff --git a/lib/examples/features.rs b/lib/examples/features.rs index b9e38b200a..1e147a61f1 100644 --- a/lib/examples/features.rs +++ b/lib/examples/features.rs @@ -32,14 +32,18 @@ async fn test_reply(api: Api, message: Message) -> Result<(), Error> { api.send(message.text_reply("Reply to message")).await?; api.send(message.chat.text("Text to message chat")).await?; - api.send(message.from.text("Private text")).await?; + if let Some(from) = &message.from { + api.send(from.text("Private text")).await?; + } Ok(()) } async fn test_forward(api: Api, message: Message) -> Result<(), Error> { api.send(message.forward(&message.chat)).await?; - api.send(message.forward(&message.from)).await?; + if let Some(from) = &message.from { + api.send(message.forward(from)).await?; + } Ok(()) } @@ -82,20 +86,28 @@ async fn test_get_chat_members_count(api: Api, message: Message) -> Result<(), E } async fn test_get_chat_member(api: Api, message: Message) -> Result<(), Error> { - let member = api.send(message.chat.get_member(&message.from)).await?; - let first_name = member.user.first_name.clone(); - let status = member.status; - api.send(message.text_reply(format!("Member {}, status {:?}", first_name, status))) - .await?; - Ok(()) + if let Some(from) = &message.from { + let member = api.send(message.chat.get_member(from)).await?; + let first_name = member.user.first_name.clone(); + let status = member.status; + api.send(message.text_reply(format!("Member {}, status {:?}", first_name, status))) + .await?; + Ok(()) + } else { + panic!("missing 'from' field") + } } async fn test_get_user_profile_photos(api: Api, message: Message) -> Result<(), Error> { - let photos = api.send(message.from.get_user_profile_photos()).await?; - - api.send(message.text_reply(format!("Found photos: {}", photos.total_count))) - .await?; - Ok(()) + if let Some(from) = &message.from { + let photos = api.send(from.get_user_profile_photos()).await?; + + api.send(message.text_reply(format!("Found photos: {}", photos.total_count))) + .await?; + Ok(()) + } else { + panic!("missing 'from' field") + } } async fn test_leave(api: Api, message: Message) -> Result<(), Error> { diff --git a/lib/examples/send.rs b/lib/examples/send.rs index 122e7e44b5..fdfbe71b75 100644 --- a/lib/examples/send.rs +++ b/lib/examples/send.rs @@ -16,8 +16,10 @@ async fn run_test(api: Api, message: Message) -> Result<(), Error> { .await?; api.send(chat.document(&file).caption("Direct to chat")) .await?; - api.send(message.from.document(&file).caption("Send to user")) + if let Some(from) = &message.from { + api.send(from.document(&file).caption("Send to user")) .await?; + } // With custom thumbnail api.send( diff --git a/lib/examples/simple.rs b/lib/examples/simple.rs index 13ccf0a644..0ecabd7919 100644 --- a/lib/examples/simple.rs +++ b/lib/examples/simple.rs @@ -16,14 +16,18 @@ async fn main() -> Result<(), Error> { if let UpdateKind::Message(message) = update.kind { if let MessageKind::Text { ref data, .. } = message.kind { // Print received text message to stdout. - println!("<{}>: {}", &message.from.first_name, data); + if let Some(from) = &message.from { + println!("<{}>: {}", from.first_name, data); - // Answer message with "Hi". - api.send(message.text_reply(format!( - "Hi, {}! You just wrote '{}'", - &message.from.first_name, data - ))) - .await?; + // Answer message with "Hi". + api.send(message.text_reply(format!( + "Hi, {}! You just wrote '{}'", + from.first_name, data + ))) + .await?; + } else { + println!(": {}", data); + } } } } diff --git a/lib/examples/tracing.rs b/lib/examples/tracing.rs index 0b539b9aa9..617552bd15 100644 --- a/lib/examples/tracing.rs +++ b/lib/examples/tracing.rs @@ -20,11 +20,13 @@ async fn main() -> Result<(), Error> { let update = update?; if let UpdateKind::Message(message) = update.kind { if let MessageKind::Text { ref data, .. } = message.kind { - api.send(message.text_reply(format!( - "Hi, {}! You just wrote '{}'", - &message.from.first_name, data - ))) - .await?; + if let Some(from) = &message.from { + api.send(message.text_reply(format!( + "Hi, {}! You just wrote '{}'", + from.first_name, data + ))) + .await?; + } } } } diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 615fa97439..e7eeff69a6 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -12,7 +12,7 @@ pub mod types; pub mod util; pub use self::api::Api; -pub use self::errors::Error; +pub use self::errors::{Error, ErrorKind}; pub use prelude::*; pub use stream::UpdatesStream; pub use types::*; diff --git a/raw/src/types/message.rs b/raw/src/types/message.rs index ed826286a3..dae301143a 100644 --- a/raw/src/types/message.rs +++ b/raw/src/types/message.rs @@ -16,7 +16,7 @@ pub struct Message { /// Unique message identifier inside this chat. pub id: MessageId, /// Sender, can be empty for messages sent to channels. - pub from: User, + pub from: Option, /// Date the message was sent in Unix time. pub date: Integer, /// Conversation the message belongs to. @@ -221,10 +221,7 @@ pub enum MessageKind { impl Message { fn from_raw_message(raw: RawMessage) -> Result { let id = raw.message_id; - let from = match raw.from.clone() { - Some(from) => from, - None => return Err("Missing `from` field for Message".into()), - }; + let from = raw.from.clone(); let date = raw.date; let chat = match raw.chat.clone() { Chat::Private(x) => MessageChat::Private(x),