From ea928f49724ab50a644c05e6ceb50880001ff6c6 Mon Sep 17 00:00:00 2001 From: Michael Krasnitski <42564254+mkrasnitski@users.noreply.github.com> Date: Sun, 15 Oct 2023 12:33:29 -0400 Subject: [PATCH] Rename `utils::parse_{username,role,channel}` methods (#2564) This renames the methods to `parse_{user,role,channel}_mention` while leaving the old names as deprecated aliases. --- src/model/mention.rs | 6 ++--- src/utils/argument_convert/channel.rs | 2 +- src/utils/argument_convert/member.rs | 2 +- src/utils/argument_convert/role.rs | 2 +- src/utils/argument_convert/user.rs | 4 +-- src/utils/mod.rs | 38 ++++++++++++++++++--------- 6 files changed, 33 insertions(+), 21 deletions(-) diff --git a/src/model/mention.rs b/src/model/mention.rs index ab11c24dc5a..543f64b72aa 100644 --- a/src/model/mention.rs +++ b/src/model/mention.rs @@ -136,11 +136,11 @@ impl FromStr for Mention { type Err = MentionParseError; fn from_str(s: &str) -> StdResult { - let m = if let Some(id) = utils::parse_channel(s) { + let m = if let Some(id) = utils::parse_channel_mention(s) { id.mention() - } else if let Some(id) = utils::parse_role(s) { + } else if let Some(id) = utils::parse_role_mention(s) { id.mention() - } else if let Some(id) = utils::parse_username(s) { + } else if let Some(id) = utils::parse_user_mention(s) { id.mention() } else { return Err(MentionParseError::InvalidMention); diff --git a/src/utils/argument_convert/channel.rs b/src/utils/argument_convert/channel.rs index 0ea6dfc276c..e06770a8dda 100644 --- a/src/utils/argument_convert/channel.rs +++ b/src/utils/argument_convert/channel.rs @@ -45,7 +45,7 @@ async fn lookup_channel_global( guild_id: Option, s: &str, ) -> Result { - if let Some(channel_id) = s.parse().ok().or_else(|| crate::utils::parse_channel(s)) { + if let Some(channel_id) = s.parse().ok().or_else(|| crate::utils::parse_channel_mention(s)) { return channel_id.to_channel(&ctx).await.map_err(ChannelParseError::Http); } diff --git a/src/utils/argument_convert/member.rs b/src/utils/argument_convert/member.rs index afd635c05d1..bcc6988e768 100644 --- a/src/utils/argument_convert/member.rs +++ b/src/utils/argument_convert/member.rs @@ -54,7 +54,7 @@ impl ArgumentConvert for Member { // DON'T use guild.members: it's only populated when guild presences intent is enabled! // If string is a raw user ID or a mention - if let Some(user_id) = s.parse().ok().or_else(|| crate::utils::parse_username(s)) { + if let Some(user_id) = s.parse().ok().or_else(|| crate::utils::parse_user_mention(s)) { if let Ok(member) = guild_id.member(&ctx, user_id).await { return Ok(member); } diff --git a/src/utils/argument_convert/role.rs b/src/utils/argument_convert/role.rs index 505ed27ea56..7446f03a412 100644 --- a/src/utils/argument_convert/role.rs +++ b/src/utils/argument_convert/role.rs @@ -65,7 +65,7 @@ impl ArgumentConvert for Role { #[cfg(not(feature = "cache"))] let roles = ctx.http().get_guild_roles(guild_id).await.map_err(RoleParseError::Http)?; - if let Some(role_id) = s.parse().ok().or_else(|| crate::utils::parse_role(s)) { + if let Some(role_id) = s.parse().ok().or_else(|| crate::utils::parse_role_mention(s)) { #[cfg(feature = "cache")] if let Some(role) = roles.get(&role_id) { return Ok(role.clone()); diff --git a/src/utils/argument_convert/user.rs b/src/utils/argument_convert/user.rs index 06e7a245fc3..b99ab60e779 100644 --- a/src/utils/argument_convert/user.rs +++ b/src/utils/argument_convert/user.rs @@ -29,7 +29,7 @@ fn lookup_by_global_cache(ctx: impl CacheHttp, s: &str) -> Option { let lookup_by_id = || users.get(&s.parse().ok()?).map(|u| u.clone()); - let lookup_by_mention = || users.get(&crate::utils::parse_username(s)?).map(|u| u.clone()); + let lookup_by_mention = || users.get(&crate::utils::parse_user_mention(s)?).map(|u| u.clone()); let lookup_by_name_and_discrim = || { let (name, discrim) = crate::utils::parse_user_tag(s)?; @@ -84,7 +84,7 @@ impl ArgumentConvert for User { } // If string is a raw user ID or a mention - if let Some(user_id) = s.parse().ok().or_else(|| crate::utils::parse_username(s)) { + if let Some(user_id) = s.parse().ok().or_else(|| crate::utils::parse_user_mention(s)) { // Now, we can still try UserId::to_user because it works for all users from all guilds // the bot is joined if let Ok(user) = user_id.to_user(&ctx).await { diff --git a/src/utils/mod.rs b/src/utils/mod.rs index ffca48126f3..4e0bcc85e85 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -130,9 +130,8 @@ pub fn parse_user_tag(s: &str) -> Option<(&str, Option)> { /// ``` /// /// [`User`]: crate::model::user::User -pub fn parse_username(mention: impl AsRef) -> Option { - let mention = mention.as_ref(); - +#[must_use] +pub fn parse_user_mention(mention: &str) -> Option { if mention.len() < 4 { return None; } @@ -147,6 +146,11 @@ pub fn parse_username(mention: impl AsRef) -> Option { } } +#[deprecated = "use `utils::parse_user_mention` instead"] +pub fn parse_username(mention: impl AsRef) -> Option { + parse_user_mention(mention.as_ref()) +} + /// Retrieves an Id from a role mention. /// /// If the mention is invalid, then [`None`] is returned. @@ -171,9 +175,8 @@ pub fn parse_username(mention: impl AsRef) -> Option { /// ``` /// /// [`Role`]: crate::model::guild::Role -pub fn parse_role(mention: impl AsRef) -> Option { - let mention = mention.as_ref(); - +#[must_use] +pub fn parse_role_mention(mention: &str) -> Option { if mention.len() < 4 { return None; } @@ -186,6 +189,11 @@ pub fn parse_role(mention: impl AsRef) -> Option { } } +#[deprecated = "use `utils::parse_role_mention` instead"] +pub fn parse_role(mention: impl AsRef) -> Option { + parse_role_mention(mention.as_ref()) +} + /// Retrieves an Id from a channel mention. /// /// If the channel mention is invalid, then [`None`] is returned. @@ -211,9 +219,8 @@ pub fn parse_role(mention: impl AsRef) -> Option { /// ``` /// /// [`Channel`]: crate::model::channel::Channel -pub fn parse_channel(mention: impl AsRef) -> Option { - let mention = mention.as_ref(); - +#[must_use] +pub fn parse_channel_mention(mention: &str) -> Option { if mention.len() < 4 { return None; } @@ -226,6 +233,11 @@ pub fn parse_channel(mention: impl AsRef) -> Option { } } +#[deprecated = "use `utils::parse_channel_mention` instead"] +pub fn parse_channel(mention: impl AsRef) -> Option { + parse_channel_mention(mention.as_ref()) +} + /// Retrieves the animated state, name and Id from an emoji mention, in the form of an /// [`EmojiIdentifier`]. /// @@ -525,18 +537,18 @@ mod test { #[test] fn test_username_parser() { - assert_eq!(parse_username("<@12345>").unwrap(), 12_345); - assert_eq!(parse_username("<@!12345>").unwrap(), 12_345); + assert_eq!(parse_user_mention("<@12345>").unwrap(), 12_345); + assert_eq!(parse_user_mention("<@!12345>").unwrap(), 12_345); } #[test] fn role_parser() { - assert_eq!(parse_role("<@&12345>").unwrap(), 12_345); + assert_eq!(parse_role_mention("<@&12345>").unwrap(), 12_345); } #[test] fn test_channel_parser() { - assert_eq!(parse_channel("<#12345>").unwrap(), 12_345); + assert_eq!(parse_channel_mention("<#12345>").unwrap(), 12_345); } #[test]