Skip to content

Commit

Permalink
Replace sentinel values with proper Option in names (#292)
Browse files Browse the repository at this point in the history
  • Loading branch information
rsxrwscjpzdzwpxaujrr authored Nov 23, 2024
1 parent 848064a commit b733d70
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 24 deletions.
2 changes: 1 addition & 1 deletion lib/grammers-client/examples/dialogs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ async fn async_main() -> Result<()> {
println!("Showing up to {} dialogs:", dialogs.total().await?);
while let Some(dialog) = dialogs.next().await? {
let chat = dialog.chat();
println!("- {: >10} {}", chat.id(), chat.name());
println!("- {: >10} {}", chat.id(), chat.name().unwrap_or_default());
}

if sign_out {
Expand Down
5 changes: 4 additions & 1 deletion lib/grammers-client/examples/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ async fn handle_update(client: Client, update: Update) -> Result {
match update {
Update::NewMessage(message) if !message.outgoing() => {
let chat = message.chat();
println!("Responding to {}", chat.name());
println!(
"Responding to {}",
chat.name().unwrap_or(&format!("id {}", chat.id()))
);
client.send_message(&chat, message.text()).await?;
}
_ => {}
Expand Down
13 changes: 11 additions & 2 deletions lib/grammers-client/src/client/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,12 @@ impl Client {
/// }
/// };
///
/// println!("Signed in as {}!", user.first_name());
/// if let Some(first_name) = user.first_name() {
/// println!("Signed in as {}!", first_name);
/// } else {
/// println!("Signed in!");
/// }
///
/// # Ok(())
/// # }
/// ```
Expand Down Expand Up @@ -286,7 +291,11 @@ impl Client {
/// }
/// };
///
/// println!("Signed in as {}!", user.first_name());
/// if let Some(first_name) = user.first_name() {
/// println!("Signed in as {}!", first_name);
/// } else {
/// println!("Signed in!");
/// }
/// # Ok(())
/// # }
/// ```
Expand Down
8 changes: 6 additions & 2 deletions lib/grammers-client/src/client/chats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,11 @@ impl Client {
/// let mut participants = client.iter_participants(&chat);
///
/// while let Some(participant) = participants.next().await? {
/// println!("{} has role {:?}", participant.user.first_name(), participant.role);
/// println!(
/// "{} has role {:?}",
/// participant.user.first_name().unwrap_or(&participant.user.id().to_string()),
/// participant.role
/// );
/// }
/// # Ok(())
/// # }
Expand Down Expand Up @@ -633,7 +637,7 @@ impl Client {
/// # async fn f(packed_chat: grammers_client::types::chat::PackedChat, client: grammers_client::Client) -> Result<(), Box<dyn std::error::Error>> {
/// let chat = client.unpack_chat(packed_chat).await?;
///
/// println!("Found chat: {}", chat.name());
/// println!("Found chat: {}", chat.name().unwrap_or(&chat.id().to_string()));
/// # Ok(())
/// # }
/// ```
Expand Down
2 changes: 1 addition & 1 deletion lib/grammers-client/src/client/dialogs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ impl Client {
///
/// while let Some(dialog) = dialogs.next().await? {
/// let chat = dialog.chat();
/// println!("{} ({})", chat.name(), chat.id());
/// println!("{} ({})", chat.name().unwrap_or_default(), chat.id());
/// }
/// # Ok(())
/// # }
Expand Down
11 changes: 7 additions & 4 deletions lib/grammers-client/src/client/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,8 @@ impl Client {
/// let mut messages = client.search_messages(&chat).query("grammers is cool");
///
/// while let Some(message) = messages.next().await? {
/// println!("{}", message.sender().unwrap().name());
/// let sender = message.sender().unwrap();
/// println!("{}", sender.name().unwrap_or(&sender.id().to_string()));
/// }
/// # Ok(())
/// # }
Expand All @@ -963,7 +964,7 @@ impl Client {
/// let mut messages = client.search_all_messages().query("grammers is cool");
///
/// while let Some(message) = messages.next().await? {
/// println!("{}", message.chat().name());
/// println!("{}", message.chat().name().unwrap_or(&message.chat().id().to_string()));
/// }
/// # Ok(())
/// # }
Expand Down Expand Up @@ -1036,10 +1037,12 @@ impl Client {
///
/// ```
/// # async fn f(chat: grammers_client::types::Chat, client: grammers_client::Client) -> Result<(), Box<dyn std::error::Error>> {
/// let name = chat.name().map_or(chat.id().to_string(), |name| name.to_owned());
///
/// if let Some(message) = client.get_pinned_message(&chat).await? {
/// println!("There is a message pinned in {}: {}", chat.name(), message.text());
/// println!("There is a message pinned in {}: {}", name.to_owned(), message.text());
/// } else {
/// println!("There are no messages pinned in {}", chat.name());
/// println!("There are no messages pinned in {}", name.to_owned());
/// }
/// # Ok(())
/// # }
Expand Down
12 changes: 6 additions & 6 deletions lib/grammers-client/src/types/chat/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,15 @@ impl Group {
/// Return the title of this group.
///
/// The title may be the empty string if the group is not accessible.
pub fn title(&self) -> &str {
pub fn title(&self) -> Option<&str> {
use tl::enums::Chat;

match &self.raw {
Chat::Empty(_) => "",
Chat::Chat(chat) => chat.title.as_str(),
Chat::Forbidden(chat) => chat.title.as_str(),
Chat::Channel(chat) => chat.title.as_str(),
Chat::ChannelForbidden(chat) => chat.title.as_str(),
Chat::Empty(_) => None,
Chat::Chat(chat) => Some(chat.title.as_str()),
Chat::Forbidden(chat) => Some(chat.title.as_str()),
Chat::Channel(chat) => Some(chat.title.as_str()),
Chat::ChannelForbidden(chat) => Some(chat.title.as_str()),
}
}

Expand Down
7 changes: 4 additions & 3 deletions lib/grammers-client/src/types/chat/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,13 @@ impl Chat {
/// For private conversations (users), this is their first name. For groups and channels,
/// this is their title.
///
/// The name may be empty if the chat is inaccessible or if the account was deleted.
pub fn name(&self) -> &str {
/// The name will be `None` if the chat is inaccessible or if the account was deleted. It may
/// also be `None` if you received it previously.
pub fn name(&self) -> Option<&str> {
match self {
Self::User(user) => user.first_name(),
Self::Group(group) => group.title(),
Self::Channel(channel) => channel.title(),
Self::Channel(channel) => Some(channel.title()),
}
}

Expand Down
9 changes: 5 additions & 4 deletions lib/grammers-client/src/types/chat/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,10 @@ impl User {

/// Return the first name of this user.
///
/// If the account was deleted, the returned string will be empty.
pub fn first_name(&self) -> &str {
self.raw.first_name.as_deref().unwrap_or("")
/// The name will be `None` if the account was deleted. It may also be `None` if you received
/// it previously.
pub fn first_name(&self) -> Option<&str> {
self.raw.first_name.as_deref()
}

/// Return the last name of this user, if any.
Expand All @@ -176,7 +177,7 @@ impl User {
/// This is equal to the user's first name concatenated with the user's last name, if this
/// is not empty. Otherwise, it equals the user's first name.
pub fn full_name(&self) -> String {
let first_name = self.first_name();
let first_name = self.first_name().unwrap_or_default();
if let Some(last_name) = self.last_name() {
let mut name = String::with_capacity(first_name.len() + 1 + last_name.len());
name.push_str(first_name);
Expand Down

0 comments on commit b733d70

Please sign in to comment.