Module twilight_mention::parse

source ·
Expand description

Parse mentions out of strings.

Included is a trait over select IDs that can be mentioned and an iterator to lazily parse mentions.

There is also the MentionType: it’s an enum wrapping all possible types of mentions and works just like the individual IDs and Timestamp.

While the syntax of mentions will be validated and the IDs within them parsed, they won’t be validated as being proper snowflakes or as real IDs in use.

§Examples

Parse IDs out of strings that you know is just a mention:

use twilight_mention::ParseMention;
use twilight_model::id::{
    marker::{ChannelMarker, EmojiMarker, RoleMarker},
    Id,
};

assert_eq!(Id::<EmojiMarker>::new(123), Id::parse("<:name:123>")?);
assert_eq!(Id::<RoleMarker>::new(456), Id::parse("<@&456>")?);
assert!(Id::<ChannelMarker>::parse("<#notamention>").is_err());

Iterate over the user mentions in a buffer:

use twilight_mention::ParseMention;
use twilight_model::id::{marker::UserMarker, Id};

let mut iter = Id::<UserMarker>::iter("these <@123> are <#456> mentions <@789>");
assert!(matches!(iter.next(), Some((user, _, _)) if user.get() == 123));
assert!(matches!(iter.next(), Some((user, _, _)) if user.get() == 789));
assert!(iter.next().is_none());

Parse a timestamp:

use twilight_mention::{
    parse::ParseMention,
    timestamp::{Timestamp, TimestampStyle},
};

let expected_timestamp = Timestamp::new(1_600_000_000, Some(TimestampStyle::RelativeTime));
assert_eq!(expected_timestamp, Timestamp::parse("<t:1600000000:R>")?);

Structs§

Enums§

Traits§