diff options
| author | Illia <[email protected]> | 2016-12-07 20:09:04 +0200 |
|---|---|---|
| committer | zeyla <[email protected]> | 2016-12-07 10:09:04 -0800 |
| commit | 626ffb25af35f5b91a76fdccf6788382a1c39455 (patch) | |
| tree | da45ae770e06e68ea12910367a46b2d11aa90987 /src/model | |
| parent | Improve Mentions, fix MessageBuilder (diff) | |
| download | serenity-626ffb25af35f5b91a76fdccf6788382a1c39455.tar.xz serenity-626ffb25af35f5b91a76fdccf6788382a1c39455.zip | |
Allow mentionable structs to be used as command arguments
Add EmojiIdentifier, allow User, UserId, Role, RoleId, EmojiIdentifier,
Channel and ChannelId to be used as arguments for commands and add more
parsing functions to utils
Diffstat (limited to 'src/model')
| -rw-r--r-- | src/model/id.rs | 8 | ||||
| -rw-r--r-- | src/model/misc.rs | 82 |
2 files changed, 89 insertions, 1 deletions
diff --git a/src/model/id.rs b/src/model/id.rs index 032e722..cbbd796 100644 --- a/src/model/id.rs +++ b/src/model/id.rs @@ -172,6 +172,14 @@ impl RoleId { } } +impl UserId { + /// Search the cache for the channel with the Id. + #[cfg(all(feature = "cache", feature = "methods"))] + pub fn find(&self) -> Option<User> { + CACHE.read().unwrap().get_user(*self).map(|x| x.clone()) + } +} + impl From<CurrentUser> for UserId { /// Gets the Id of a `CurrentUser` struct. fn from(current_user: CurrentUser) -> UserId { diff --git a/src/model/misc.rs b/src/model/misc.rs index 6e208bf..e078bac 100644 --- a/src/model/misc.rs +++ b/src/model/misc.rs @@ -7,9 +7,13 @@ use super::{ Role, UserId, User, - IncidentStatus + IncidentStatus, + EmojiIdentifier }; use ::internal::prelude::*; +use std::str::FromStr; +use std::result::Result as StdResult; +use ::utils; /// Allows something - such as a channel or role - to be mentioned in a message. pub trait Mentionable { @@ -74,6 +78,82 @@ impl Mentionable for User { } } +#[cfg(feature = "cache")] +impl FromStr for User { + type Err = (); + fn from_str(s: &str) -> StdResult<Self, ()> { + match utils::parse_username(s) { + Some(x) => { + match UserId(x as u64).find() { + Some(user) => Ok(user), + _ => Err(()) + } + }, + _ => Err(()) + } + } +} + +impl FromStr for UserId { + type Err = (); + fn from_str(s: &str) -> StdResult<Self, ()> { + utils::parse_username(s).ok_or_else(|| ()).map(|x| UserId(x)) + } +} + +#[cfg(feature = "cache")] +impl FromStr for Role { + type Err = (); + fn from_str(s: &str) -> StdResult<Self, ()> { + match utils::parse_role(s) { + Some(x) => { + match RoleId(x).find() { + Some(user) => Ok(user), + _ => Err(()) + } + }, + _ => Err(()) + } + } +} + +impl FromStr for RoleId { + type Err = (); + fn from_str(s: &str) -> StdResult<Self, ()> { + utils::parse_role(s).ok_or_else(|| ()).map(|x| RoleId(x)) + } +} + +impl FromStr for EmojiIdentifier { + type Err = (); + fn from_str(s: &str) -> StdResult<Self, ()> { + utils::parse_emoji(s).ok_or_else(|| ()) + } +} + +impl FromStr for ChannelId { + type Err = (); + fn from_str(s: &str) -> StdResult<Self, ()> { + utils::parse_channel(s).ok_or_else(|| ()).map(|x| ChannelId(x)) + } +} + +#[cfg(feature = "cache")] +impl FromStr for Channel { + type Err = (); + fn from_str(s: &str) -> StdResult<Self, ()> { + match utils::parse_channel(s) { + Some(x) => { + match ChannelId(x).find() { + Some(channel) => Ok(channel), + _ => Err(()) + } + }, + _ => Err(()) + } + } +} + impl IncidentStatus { #[doc(hidden)] pub fn decode(value: Value) -> Result<Self> { |