aboutsummaryrefslogtreecommitdiff
path: root/src/model/misc.rs
diff options
context:
space:
mode:
authorAustin Hellyer <[email protected]>2016-09-19 09:00:03 -0700
committerAustin Hellyer <[email protected]>2016-10-18 11:14:27 -0700
commit8fc8c81403c3daa187ba96a7d488a64db21463bf (patch)
tree81bc4890c28b08ce806f69084617066bce863c2d /src/model/misc.rs
downloadserenity-8fc8c81403c3daa187ba96a7d488a64db21463bf.tar.xz
serenity-8fc8c81403c3daa187ba96a7d488a64db21463bf.zip
Initial commit
Diffstat (limited to 'src/model/misc.rs')
-rw-r--r--src/model/misc.rs95
1 files changed, 95 insertions, 0 deletions
diff --git a/src/model/misc.rs b/src/model/misc.rs
new file mode 100644
index 0000000..6b8a90e
--- /dev/null
+++ b/src/model/misc.rs
@@ -0,0 +1,95 @@
+use std::fmt;
+use super::{
+ ChannelId,
+ Channel,
+ Emoji,
+ Member,
+ RoleId,
+ Role,
+ UserId,
+ User,
+ IncidentStatus
+};
+use ::prelude::*;
+
+pub trait Mentionable {
+ fn mention(&self) -> String;
+}
+
+impl Mentionable for ChannelId {
+ fn mention(&self) -> String {
+ format!("{}", self)
+ }
+}
+
+impl Mentionable for Channel {
+ fn mention(&self) -> String {
+ format!("{}", self)
+ }
+}
+
+impl Mentionable for Emoji {
+ fn mention(&self) -> String {
+ format!("{}", self)
+ }
+}
+
+impl Mentionable for Member {
+ fn mention(&self) -> String {
+ format!("{}", self.user)
+ }
+}
+
+impl Mentionable for RoleId {
+ fn mention(&self) -> String {
+ format!("{}", self)
+ }
+}
+
+impl Mentionable for Role {
+ fn mention(&self) -> String {
+ format!("{}", self)
+ }
+}
+
+impl Mentionable for UserId {
+ fn mention(&self) -> String {
+ format!("{}", self)
+ }
+}
+
+impl Mentionable for User {
+ fn mention(&self) -> String {
+ format!("{}", self)
+ }
+}
+
+/// A mention targeted at a certain model.
+///
+/// A mention can be created by calling `.mention()` on anything that is
+/// mentionable - or an item's Id - and can be formatted into a string using
+/// [`format!`]:
+///
+/// ```rust,ignore
+/// let message = format!("Mentioning {}", user.mention());
+/// ```
+///
+/// If a `String` is required, call `mention.to_string()`.
+pub struct Mention {
+ pub prefix: &'static str,
+ pub id: u64,
+}
+
+impl fmt::Display for Mention {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ try!(f.write_str(self.prefix));
+ try!(fmt::Display::fmt(&self.id, f));
+ fmt::Write::write_char(f, '>')
+ }
+}
+
+impl IncidentStatus {
+ pub fn decode(value: Value) -> Result<Self> {
+ Self::decode_str(value)
+ }
+}