twilight_model/channel/message/
mention.rsuse crate::{
guild::PartialMember,
id::{marker::UserMarker, Id},
user::{self, DiscriminatorDisplay, UserFlags},
util::image_hash::ImageHash,
};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub struct Mention {
pub avatar: Option<ImageHash>,
#[serde(default)]
pub bot: bool,
#[serde(with = "user::discriminator")]
pub discriminator: u16,
pub id: Id<UserMarker>,
#[serde(skip_serializing_if = "Option::is_none")]
pub member: Option<PartialMember>,
#[serde(rename = "username")]
pub name: String,
pub public_flags: UserFlags,
}
impl Mention {
pub const fn discriminator(&self) -> DiscriminatorDisplay {
DiscriminatorDisplay::new(self.discriminator)
}
}
#[cfg(test)]
mod tests {
use super::{Mention, PartialMember, UserFlags};
use crate::{
guild::MemberFlags,
id::Id,
util::datetime::{Timestamp, TimestampParseError},
};
use serde_test::Token;
use std::str::FromStr;
#[test]
fn mention_without_member() {
let value = Mention {
avatar: None,
bot: false,
discriminator: 1,
id: Id::new(1),
member: None,
name: "foo".to_owned(),
public_flags: UserFlags::empty(),
};
serde_test::assert_tokens(
&value,
&[
Token::Struct {
name: "Mention",
len: 6,
},
Token::Str("avatar"),
Token::None,
Token::Str("bot"),
Token::Bool(false),
Token::Str("discriminator"),
Token::Str("0001"),
Token::Str("id"),
Token::NewtypeStruct { name: "Id" },
Token::Str("1"),
Token::Str("username"),
Token::Str("foo"),
Token::Str("public_flags"),
Token::U64(0),
Token::StructEnd,
],
);
}
#[test]
fn mention_with_member() -> Result<(), TimestampParseError> {
let joined_at = Some(Timestamp::from_str("2015-04-26T06:26:56.936000+00:00")?);
let flags = MemberFlags::BYPASSES_VERIFICATION | MemberFlags::DID_REJOIN;
let value = Mention {
avatar: None,
bot: false,
discriminator: 1,
id: Id::new(1),
member: Some(PartialMember {
avatar: None,
communication_disabled_until: None,
deaf: false,
flags,
joined_at,
mute: true,
nick: Some("bar".to_owned()),
permissions: None,
premium_since: None,
roles: Vec::new(),
user: None,
}),
name: "foo".to_owned(),
public_flags: UserFlags::empty(),
};
serde_test::assert_tokens(
&value,
&[
Token::Struct {
name: "Mention",
len: 7,
},
Token::Str("avatar"),
Token::None,
Token::Str("bot"),
Token::Bool(false),
Token::Str("discriminator"),
Token::Str("0001"),
Token::Str("id"),
Token::NewtypeStruct { name: "Id" },
Token::Str("1"),
Token::Str("member"),
Token::Some,
Token::Struct {
name: "PartialMember",
len: 8,
},
Token::Str("communication_disabled_until"),
Token::None,
Token::Str("deaf"),
Token::Bool(false),
Token::Str("flags"),
Token::U64(flags.bits()),
Token::Str("joined_at"),
Token::Some,
Token::Str("2015-04-26T06:26:56.936000+00:00"),
Token::Str("mute"),
Token::Bool(true),
Token::Str("nick"),
Token::Some,
Token::Str("bar"),
Token::Str("roles"),
Token::Seq { len: Some(0) },
Token::SeqEnd,
Token::Str("user"),
Token::None,
Token::StructEnd,
Token::Str("username"),
Token::Str("foo"),
Token::Str("public_flags"),
Token::U64(0),
Token::StructEnd,
],
);
Ok(())
}
}