aboutsummaryrefslogtreecommitdiff
path: root/src/model/misc.rs
blob: f3632a5d2afdfeefa84e3294f9390ef55a0a1eb7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use super::{
    ChannelId,
    Channel,
    Emoji,
    Member,
    RoleId,
    Role,
    UserId,
    User,
    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 {
    fn mention(&self) -> String;
}

impl Mentionable for ChannelId {
    fn mention(&self) -> String {
        format!("<#{}>", self.0)
    }
}

impl Mentionable for Channel {
    fn mention(&self) -> String {
        match *self {
            Channel::Guild(ref x) => {
                format!("<#{}>", x.id.0)
            },
            Channel::Private(ref x) => {
                format!("<#{}>", x.id.0)
            },
            Channel::Group(ref x) => {
                format!("<#{}>", x.channel_id.0)
            }
        }
    }
}

impl Mentionable for Emoji {
    fn mention(&self) -> String {
        format!("<:{}:{}>", self.name, self.id.0)
    }
}

impl Mentionable for Member {
    fn mention(&self) -> String {
        format!("<@{}>", self.user.id.0)
    }
}

impl Mentionable for RoleId {
    fn mention(&self) -> String {
        format!("<@&{}>", self.0)
    }
}

impl Mentionable for Role {
    fn mention(&self) -> String {
        format!("<@&{}>", self.id.0)
    }
}

impl Mentionable for UserId {
    fn mention(&self) -> String {
        format!("<@{}>", self.0)
    }
}

impl Mentionable for User {
    fn mention(&self) -> String {
        format!("<@{}>", self.id.0)
    }
}

#[cfg(all(feature="cache", feature="methods"))]
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(UserId)
    }
}

#[cfg(all(feature="cache", feature="methods"))]
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(RoleId)
    }
}

impl EmojiIdentifier {
    /// Generates a URL to the emoji's image.
    #[cfg(feature="methods")]
    #[inline]
    pub fn url(&self) -> String {
        format!(cdn!("/emojis/{}.png"), self.id)
    }
}

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(ChannelId)
    }
}

#[cfg(all(feature="cache", feature="methods"))]
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> {
        Self::decode_str(value)
    }
}