Skip to content

Commit

Permalink
Rename utils::parse_{username,role,channel} methods (#2564)
Browse files Browse the repository at this point in the history
This renames the methods to `parse_{user,role,channel}_mention` while leaving the old names as deprecated aliases.
  • Loading branch information
mkrasnitski committed Oct 17, 2023
1 parent 5cba385 commit ea928f4
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 21 deletions.
6 changes: 3 additions & 3 deletions src/model/mention.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@ impl FromStr for Mention {
type Err = MentionParseError;

fn from_str(s: &str) -> StdResult<Self, Self::Err> {
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);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/argument_convert/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async fn lookup_channel_global(
guild_id: Option<GuildId>,
s: &str,
) -> Result<Channel, ChannelParseError> {
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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils/argument_convert/member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/argument_convert/role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
4 changes: 2 additions & 2 deletions src/utils/argument_convert/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn lookup_by_global_cache(ctx: impl CacheHttp, s: &str) -> Option<User> {

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)?;
Expand Down Expand Up @@ -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 {
Expand Down
38 changes: 25 additions & 13 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,8 @@ pub fn parse_user_tag(s: &str) -> Option<(&str, Option<NonZeroU16>)> {
/// ```
///
/// [`User`]: crate::model::user::User
pub fn parse_username(mention: impl AsRef<str>) -> Option<UserId> {
let mention = mention.as_ref();

#[must_use]
pub fn parse_user_mention(mention: &str) -> Option<UserId> {
if mention.len() < 4 {
return None;
}
Expand All @@ -147,6 +146,11 @@ pub fn parse_username(mention: impl AsRef<str>) -> Option<UserId> {
}
}

#[deprecated = "use `utils::parse_user_mention` instead"]
pub fn parse_username(mention: impl AsRef<str>) -> Option<UserId> {
parse_user_mention(mention.as_ref())
}

/// Retrieves an Id from a role mention.
///
/// If the mention is invalid, then [`None`] is returned.
Expand All @@ -171,9 +175,8 @@ pub fn parse_username(mention: impl AsRef<str>) -> Option<UserId> {
/// ```
///
/// [`Role`]: crate::model::guild::Role
pub fn parse_role(mention: impl AsRef<str>) -> Option<RoleId> {
let mention = mention.as_ref();

#[must_use]
pub fn parse_role_mention(mention: &str) -> Option<RoleId> {
if mention.len() < 4 {
return None;
}
Expand All @@ -186,6 +189,11 @@ pub fn parse_role(mention: impl AsRef<str>) -> Option<RoleId> {
}
}

#[deprecated = "use `utils::parse_role_mention` instead"]
pub fn parse_role(mention: impl AsRef<str>) -> Option<RoleId> {
parse_role_mention(mention.as_ref())
}

/// Retrieves an Id from a channel mention.
///
/// If the channel mention is invalid, then [`None`] is returned.
Expand All @@ -211,9 +219,8 @@ pub fn parse_role(mention: impl AsRef<str>) -> Option<RoleId> {
/// ```
///
/// [`Channel`]: crate::model::channel::Channel
pub fn parse_channel(mention: impl AsRef<str>) -> Option<ChannelId> {
let mention = mention.as_ref();

#[must_use]
pub fn parse_channel_mention(mention: &str) -> Option<ChannelId> {
if mention.len() < 4 {
return None;
}
Expand All @@ -226,6 +233,11 @@ pub fn parse_channel(mention: impl AsRef<str>) -> Option<ChannelId> {
}
}

#[deprecated = "use `utils::parse_channel_mention` instead"]
pub fn parse_channel(mention: impl AsRef<str>) -> Option<ChannelId> {
parse_channel_mention(mention.as_ref())
}

/// Retrieves the animated state, name and Id from an emoji mention, in the form of an
/// [`EmojiIdentifier`].
///
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit ea928f4

Please sign in to comment.