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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
use crate::timestamp::{Timestamp, TimestampStyle};

use super::{MentionIter, MentionType, ParseMentionError, ParseMentionErrorType};
use crate::fmt::CommandMention;
use std::{num::NonZeroU64, str::Chars};
use twilight_model::id::marker::CommandMarker;
use twilight_model::id::{
    marker::{ChannelMarker, EmojiMarker, RoleMarker, UserMarker},
    Id,
};

/// Parse mentions out of buffers.
///
/// 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.
///
/// **Note** that this trait is sealed and is not meant to be manually
/// implemented.
pub trait ParseMention: private::Sealed {
    /// Leading sigil(s) of the mention after the leading arrow (`<`).
    ///
    /// In a channel mention, the sigil is `#`. In the case of a user mention,
    /// the sigil would be `@`.
    const SIGILS: &'static [&'static str];

    /// Parse a mention out of a buffer.
    ///
    /// This will not search the buffer for a mention and will instead treat the
    /// entire buffer as a mention.
    ///
    /// # Examples
    ///
    /// ```
    /// use twilight_mention::ParseMention;
    /// use twilight_model::id::{
    ///     marker::{ChannelMarker, UserMarker},
    ///     Id,
    /// };
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// assert_eq!(Id::<ChannelMarker>::new(123), Id::parse("<#123>")?,);
    /// assert_eq!(Id::<UserMarker>::new(456), Id::parse("<@456>")?,);
    /// assert!(Id::<ChannelMarker>::parse("not a mention").is_err());
    /// # Ok(()) }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns a [`ParseMentionErrorType::LeadingArrow`] error type if the
    /// leading arrow is not present.
    ///
    /// Returns a [`ParseMentionErrorType::Sigil`] error type if the mention
    /// type's sigil is not present after the leading arrow.
    ///
    /// Returns a [`ParseMentionErrorType::TrailingArrow`] error type if the
    /// trailing arrow is not present after the ID.
    fn parse(buf: &str) -> Result<Self, ParseMentionError<'_>>
    where
        Self: Sized;

    /// Search a buffer for mentions and parse out any that are encountered.
    ///
    /// Unlike [`parse`], this will not error if anything that is indicative of
    /// a mention is encountered but did not successfully parse, such as a `<`
    /// but with no trailing mention sigil.
    ///
    /// [`parse`]: Self::parse
    #[must_use = "you must use the iterator to lazily parse mentions"]
    fn iter(buf: &str) -> MentionIter<'_, Self>
    where
        Self: Sized,
    {
        MentionIter::new(buf)
    }
}

impl ParseMention for Id<ChannelMarker> {
    const SIGILS: &'static [&'static str] = &["#"];

    fn parse(buf: &str) -> Result<Self, ParseMentionError<'_>>
    where
        Self: Sized,
    {
        parse_mention(buf, Self::SIGILS).map(|(id, _, _)| Id::from(id))
    }
}

impl ParseMention for CommandMention {
    const SIGILS: &'static [&'static str] = &["/"];

    fn parse(buf: &str) -> Result<Self, ParseMentionError<'_>>
    where
        Self: Sized,
    {
        // Can't use `parse_mention` due to significant pattern differences for command mentions.

        let mut echars = buf.chars().enumerate();

        let c = echars.next();
        if c.map_or(true, |(_, c)| c != '<') {
            return Err(ParseMentionError {
                kind: ParseMentionErrorType::LeadingArrow {
                    found: c.map(|(_, c)| c),
                },
                source: None,
            });
        }

        let c = echars.next();
        if c.map_or(true, |(_, c)| c != '/') {
            return Err(ParseMentionError {
                kind: ParseMentionErrorType::Sigil {
                    expected: Self::SIGILS,
                    found: c.map(|(_, c)| c),
                },
                source: None,
            });
        }

        let mut segments: Vec<&str> = Vec::new();
        let mut current_segment: usize = 2;
        let id_sep = loop {
            match echars.next() {
                None => {
                    if !&buf[current_segment..].trim().is_empty() {
                        segments.push(&buf[current_segment..]);
                    }

                    let (expected, found) = match segments.len() {
                        // no segment found, require name and id
                        0 => (2, 0),
                        // only found name, needs id
                        1 => (2, 1),
                        // only found name and subcommand, needs id
                        2 => (3, 2),
                        // only found name, subcommand and subcommand group, needs id
                        3 => (4, 3),
                        // too many segments
                        _ => {
                            return Err(ParseMentionError {
                                kind: ParseMentionErrorType::ExtraneousPart {
                                    found: &buf[current_segment..],
                                },
                                source: None,
                            })
                        }
                    };

                    return Err(ParseMentionError {
                        kind: ParseMentionErrorType::PartMissing { expected, found },
                        source: None,
                    });
                }

                Some((i, ':')) => {
                    if !&buf[current_segment..i].trim().is_empty() {
                        segments.push(&buf[current_segment..i]);
                    }
                    break i;
                }

                Some((i, ' ')) => {
                    if !buf[current_segment..i].trim().is_empty() {
                        segments.push(&buf[current_segment..i]);
                    }

                    current_segment = i + 1;
                }

                Some(_) => continue,
            }
        };

        let id = loop {
            match echars.next() {
                None => {
                    return Err(ParseMentionError {
                        kind: ParseMentionErrorType::TrailingArrow { found: None },
                        source: None,
                    })
                }
                Some((i, '>')) => break &buf[(id_sep + 1)..i],
                Some((_, c)) if !c.is_numeric() => {
                    return Err(ParseMentionError {
                        kind: ParseMentionErrorType::TrailingArrow { found: Some(c) },
                        source: None,
                    })
                }
                _ => continue,
            }
        };

        let id: Id<CommandMarker> = match id.parse() {
            Ok(id) => id,
            Err(e) => {
                return Err(ParseMentionError {
                    kind: ParseMentionErrorType::IdNotU64 { found: id },
                    source: Some(Box::new(e)),
                })
            }
        };

        let mut segments = segments.into_iter();
        match_command_mention_from_segments(
            id,
            segments.next(),
            segments.next(),
            segments.next(),
            segments.next(),
        )
    }
}

impl ParseMention for Id<EmojiMarker> {
    const SIGILS: &'static [&'static str] = &[":"];

    fn parse(buf: &str) -> Result<Self, ParseMentionError<'_>>
    where
        Self: Sized,
    {
        parse_mention(buf, Self::SIGILS).map(|(id, _, _)| Id::from(id))
    }
}

impl ParseMention for MentionType {
    /// Sigils for any type of mention.
    ///
    /// Contains all of the sigils of every other type of mention.
    const SIGILS: &'static [&'static str] = &["#", ":", "@&", "@", "t:"];

    /// Parse a mention from a string slice.
    ///
    /// # Examples
    ///
    /// Returns [`ParseMentionErrorType::TimestampStyleInvalid`] if a timestamp
    /// style value is invalid.
    ///
    /// [`ParseMentionError::TimestampStyleInvalid`]: super::error::ParseMentionErrorType::TimestampStyleInvalid
    fn parse(buf: &str) -> Result<Self, ParseMentionError<'_>>
    where
        Self: Sized,
    {
        let (id, maybe_modifier, found) = parse_mention(buf, Self::SIGILS)?;

        for sigil in Id::<ChannelMarker>::SIGILS {
            if *sigil == found {
                return Ok(MentionType::Channel(Id::from(id)));
            }
        }

        for sigil in Id::<EmojiMarker>::SIGILS {
            if *sigil == found {
                return Ok(MentionType::Emoji(Id::from(id)));
            }
        }

        for sigil in Id::<RoleMarker>::SIGILS {
            if *sigil == found {
                return Ok(MentionType::Role(Id::from(id)));
            }
        }

        for sigil in Timestamp::SIGILS {
            if *sigil == found {
                let maybe_style = parse_maybe_style(maybe_modifier)?;

                return Ok(MentionType::Timestamp(Timestamp::new(
                    id.get(),
                    maybe_style,
                )));
            }
        }

        for sigil in Id::<UserMarker>::SIGILS {
            if *sigil == found {
                return Ok(MentionType::User(Id::from(id)));
            }
        }

        unreachable!("mention type must have been found");
    }
}

impl ParseMention for Id<RoleMarker> {
    const SIGILS: &'static [&'static str] = &["@&"];

    fn parse(buf: &str) -> Result<Self, ParseMentionError<'_>>
    where
        Self: Sized,
    {
        parse_mention(buf, Self::SIGILS).map(|(id, _, _)| Id::from(id))
    }
}

impl ParseMention for Timestamp {
    const SIGILS: &'static [&'static str] = &["t:"];

    /// Parse a timestamp from a string slice.
    ///
    /// # Examples
    ///
    /// Returns [`ParseMentionErrorType::TimestampStyleInvalid`] if the
    /// timestamp style value is invalid.
    ///
    /// [`ParseMentionError::TimestampStyleInvalid`]: super::error::ParseMentionErrorType::TimestampStyleInvalid
    fn parse(buf: &str) -> Result<Self, ParseMentionError<'_>>
    where
        Self: Sized,
    {
        let (unix, maybe_modifier, _) = parse_mention(buf, Self::SIGILS)?;

        Ok(Timestamp::new(
            unix.get(),
            parse_maybe_style(maybe_modifier)?,
        ))
    }
}

impl ParseMention for Id<UserMarker> {
    /// Sigil for User ID mentions.
    const SIGILS: &'static [&'static str] = &["@"];

    fn parse(buf: &str) -> Result<Self, ParseMentionError<'_>>
    where
        Self: Sized,
    {
        parse_mention(buf, Self::SIGILS).map(|(id, _, _)| Id::from(id))
    }
}

/// Matches the four segments of [`CommandMention::parse`] into the final `Result` for it.
#[inline]
fn match_command_mention_from_segments<'s>(
    id: Id<CommandMarker>,
    first: Option<&'s str>,
    second: Option<&'s str>,
    third: Option<&'s str>,
    fourth: Option<&'s str>,
) -> Result<CommandMention, ParseMentionError<'s>> {
    match (first, second, third, fourth) {
        (_, _, _, Some(extra)) => Err(ParseMentionError {
            kind: ParseMentionErrorType::ExtraneousPart { found: extra },
            source: None,
        }),
        (None, _, _, _) => {
            Err(ParseMentionError {
                kind: ParseMentionErrorType::PartMissing {
                    // at least two for command
                    expected: 2,
                    // we found the id until now
                    found: 1,
                },
                source: None,
            })
        }
        (Some(name), None, None, None) => Ok(CommandMention::Command {
            name: name.to_owned(),
            id,
        }),
        (Some(name), Some(sub_command), None, None) => Ok(CommandMention::SubCommand {
            name: name.to_owned(),
            sub_command: sub_command.to_owned(),
            id,
        }),
        (Some(name), Some(sub_command_group), Some(sub_command), None) => {
            Ok(CommandMention::SubCommandGroup {
                name: name.to_owned(),
                sub_command: sub_command.to_owned(),
                sub_command_group: sub_command_group.to_owned(),
                id,
            })
        }
        _ => unreachable!(),
    }
}

/// Parse a possible style value string slice into a [`TimestampStyle`].
///
/// # Errors
///
/// Returns [`ParseMentionErrorType::TimestampStyleInvalid`] if the timestamp
/// style value is invalid.
fn parse_maybe_style(value: Option<&str>) -> Result<Option<TimestampStyle>, ParseMentionError<'_>> {
    Ok(if let Some(modifier) = value {
        Some(
            TimestampStyle::try_from(modifier).map_err(|source| ParseMentionError {
                kind: ParseMentionErrorType::TimestampStyleInvalid { found: modifier },
                source: Some(Box::new(source)),
            })?,
        )
    } else {
        None
    })
}

/// # Errors
///
/// Returns [`ParseMentionErrorType::LeadingArrow`] if the leading arrow is not
/// present.
///
/// Returns [`ParseMentionErrorType::Sigil`] if the mention type's sigil is not
/// present after the leading arrow.
///
/// Returns [`ParseMentionErrorType::TrailingArrow`] if the trailing arrow is
/// not present after the ID.
fn parse_mention<'a>(
    buf: &'a str,
    sigils: &'a [&'a str],
) -> Result<(NonZeroU64, Option<&'a str>, &'a str), ParseMentionError<'a>> {
    let mut chars = buf.chars();

    let c = chars.next();

    if c.map_or(true, |c| c != '<') {
        return Err(ParseMentionError {
            kind: ParseMentionErrorType::LeadingArrow { found: c },
            source: None,
        });
    }

    let maybe_sigil = sigils.iter().find(|sigil| {
        if chars.as_str().starts_with(*sigil) {
            for _ in 0..sigil.chars().count() {
                chars.next();
            }

            return true;
        }

        false
    });

    let sigil = if let Some(sigil) = maybe_sigil {
        *sigil
    } else {
        return Err(ParseMentionError {
            kind: ParseMentionErrorType::Sigil {
                expected: sigils,
                found: chars.next(),
            },
            source: None,
        });
    };

    if sigil == ":" && !separator_sigil_present(&mut chars) {
        return Err(ParseMentionError {
            kind: ParseMentionErrorType::PartMissing {
                found: 1,
                expected: 2,
            },
            source: None,
        });
    }

    let end_position = chars
        .as_str()
        .find('>')
        .ok_or_else(|| ParseMentionError::trailing_arrow(None))?;
    let maybe_split_position = chars.as_str().find(':');

    let end_of_id_position = maybe_split_position.unwrap_or(end_position);

    let remaining = chars
        .as_str()
        .get(..end_of_id_position)
        .ok_or_else(|| ParseMentionError::trailing_arrow(None))?;

    let num = remaining.parse().map_err(|source| ParseMentionError {
        kind: ParseMentionErrorType::IdNotU64 { found: remaining },
        source: Some(Box::new(source)),
    })?;

    // If additional information - like a timestamp style - is present then we
    // can just get a subslice of the string via the split and ending positions.
    let style = maybe_split_position.and_then(|split_position| {
        chars.next();

        // We need to remove 1 so we don't catch the `>` in it.
        let style_end_position = end_position - 1;

        chars.as_str().get(split_position..style_end_position)
    });

    Ok((num, style, sigil))
}

// Don't use `Iterator::skip_while` so we can mutate `chars` in-place;
// `skip_while` is consuming.
fn separator_sigil_present(chars: &mut Chars<'_>) -> bool {
    for c in chars {
        if c == ':' {
            return true;
        }
    }

    false
}

/// Rust doesn't allow leaking private implementations, but if we make the trait
/// public in a private scope then it gets by the restriction and doesn't allow
/// Sealed to be named.
///
/// Yes, this is the correct way of sealing a trait:
///
/// <https://rust-lang.github.io/api-guidelines/future-proofing.html>
mod private {
    use super::super::MentionType;
    use crate::fmt::CommandMention;
    use crate::timestamp::Timestamp;
    use twilight_model::id::{
        marker::{ChannelMarker, EmojiMarker, RoleMarker, UserMarker},
        Id,
    };

    pub trait Sealed {}

    impl Sealed for Id<ChannelMarker> {}
    impl Sealed for CommandMention {}
    impl Sealed for Id<EmojiMarker> {}
    impl Sealed for MentionType {}
    impl Sealed for Id<RoleMarker> {}
    impl Sealed for Timestamp {}
    impl Sealed for Id<UserMarker> {}
}

#[cfg(test)]
mod tests {
    use super::{
        super::{MentionType, ParseMentionErrorType},
        private::Sealed,
        ParseMention,
    };
    use crate::fmt::CommandMention;
    use crate::{
        parse::ParseMentionError,
        timestamp::{Timestamp, TimestampStyle},
    };
    use static_assertions::assert_impl_all;
    use twilight_model::id::{
        marker::{ChannelMarker, EmojiMarker, RoleMarker, UserMarker},
        Id,
    };

    assert_impl_all!(Id<ChannelMarker>: ParseMention, Sealed);
    assert_impl_all!(CommandMention: ParseMention, Sealed);
    assert_impl_all!(Id<EmojiMarker>: ParseMention, Sealed);
    assert_impl_all!(MentionType: ParseMention, Sealed);
    assert_impl_all!(Id<RoleMarker>: ParseMention, Sealed);
    assert_impl_all!(Id<UserMarker>: ParseMention, Sealed);

    #[test]
    fn sigils() {
        assert_eq!(&["#"], Id::<ChannelMarker>::SIGILS);
        assert_eq!(&["/"], CommandMention::SIGILS);
        assert_eq!(&[":"], Id::<EmojiMarker>::SIGILS);
        assert_eq!(&["#", ":", "@&", "@", "t:"], MentionType::SIGILS);
        assert_eq!(&["@&"], Id::<RoleMarker>::SIGILS);
        assert_eq!(&["@"], Id::<UserMarker>::SIGILS);
    }

    #[test]
    fn parse_channel_id() {
        assert_eq!(Id::<ChannelMarker>::new(123), Id::parse("<#123>").unwrap());
        assert_eq!(
            &ParseMentionErrorType::Sigil {
                expected: &["#"],
                found: Some('@'),
            },
            Id::<ChannelMarker>::parse("<@123>").unwrap_err().kind(),
        );
    }

    #[test]
    fn parse_command_mention() {
        assert_eq!(
            &ParseMentionErrorType::PartMissing {
                expected: 2,
                found: 1,
            },
            CommandMention::parse("</ :123>").unwrap_err().kind()
        );

        assert_eq!(
            CommandMention::Command {
                name: "command".to_owned(),
                id: Id::new(123)
            },
            CommandMention::parse("</command:123>").unwrap()
        );

        assert_eq!(
            CommandMention::SubCommand {
                name: "command".to_owned(),
                sub_command: "subcommand".to_owned(),
                id: Id::new(123)
            },
            CommandMention::parse("</command subcommand:123>").unwrap()
        );

        // this is more relaxed than the discord client
        assert_eq!(
            CommandMention::SubCommand {
                name: "command".to_owned(),
                sub_command: "subcommand".to_owned(),
                id: Id::new(123)
            },
            CommandMention::parse("</command  subcommand:123>").unwrap()
        );

        assert_eq!(
            CommandMention::SubCommandGroup {
                name: "command".to_owned(),
                sub_command: "subcommand".to_owned(),
                sub_command_group: "subcommand_group".to_owned(),
                id: Id::new(123)
            },
            CommandMention::parse("</command subcommand_group subcommand:123>").unwrap()
        );

        assert_eq!(
            &ParseMentionErrorType::ExtraneousPart { found: "d" },
            CommandMention::parse("</a b c d:123>").unwrap_err().kind()
        );

        assert_eq!(
            &ParseMentionErrorType::IdNotU64 { found: "0" },
            CommandMention::parse("</a:0>").unwrap_err().kind()
        );

        assert_eq!(
            &ParseMentionErrorType::TrailingArrow { found: Some('b') },
            CommandMention::parse("</a:b>").unwrap_err().kind()
        );

        assert_eq!(
            &ParseMentionErrorType::TrailingArrow { found: None },
            CommandMention::parse("</a:123").unwrap_err().kind()
        );

        for (input, expected, found) in [
            ("</", 2, 0),
            ("</a", 2, 1),
            ("</a b", 3, 2),
            ("</a b c", 4, 3),
        ] {
            assert_eq!(
                &ParseMentionErrorType::PartMissing { expected, found },
                CommandMention::parse(input).unwrap_err().kind()
            );
        }

        assert_eq!(
            &ParseMentionErrorType::ExtraneousPart { found: "d" },
            CommandMention::parse("</a b c d").unwrap_err().kind()
        );
    }

    #[test]
    fn parse_emoji_id() {
        assert_eq!(
            Id::<EmojiMarker>::new(123),
            Id::parse("<:name:123>").unwrap()
        );
        assert_eq!(
            &ParseMentionErrorType::Sigil {
                expected: &[":"],
                found: Some('@'),
            },
            Id::<EmojiMarker>::parse("<@123>").unwrap_err().kind(),
        );
    }

    #[test]
    fn parse_mention_type() {
        assert_eq!(
            MentionType::Channel(Id::new(123)),
            MentionType::parse("<#123>").unwrap()
        );
        assert_eq!(
            MentionType::Emoji(Id::new(123)),
            MentionType::parse("<:name:123>").unwrap()
        );
        assert_eq!(
            MentionType::Role(Id::new(123)),
            MentionType::parse("<@&123>").unwrap()
        );
        assert_eq!(
            MentionType::User(Id::new(123)),
            MentionType::parse("<@123>").unwrap()
        );
        assert_eq!(
            &ParseMentionErrorType::Sigil {
                expected: &["#", ":", "@&", "@", "t:"],
                found: Some(';'),
            },
            MentionType::parse("<;123>").unwrap_err().kind(),
        );
    }

    #[test]
    fn parse_role_id() {
        assert_eq!(Id::<RoleMarker>::new(123), Id::parse("<@&123>").unwrap());
        assert_eq!(
            &ParseMentionErrorType::Sigil {
                expected: &["@&"],
                found: Some('@'),
            },
            Id::<RoleMarker>::parse("<@123>").unwrap_err().kind(),
        );
    }

    #[test]
    fn parse_timestamp() -> Result<(), ParseMentionError<'static>> {
        assert_eq!(Timestamp::new(123, None), Timestamp::parse("<t:123>")?);
        assert_eq!(
            Timestamp::new(123, Some(TimestampStyle::RelativeTime)),
            Timestamp::parse("<t:123:R>")?
        );
        assert_eq!(
            &ParseMentionErrorType::TimestampStyleInvalid { found: "?" },
            Timestamp::parse("<t:123:?>").unwrap_err().kind(),
        );

        Ok(())
    }

    #[test]
    fn parse_user_id() {
        assert_eq!(Id::<UserMarker>::new(123), Id::parse("<@123>").unwrap());
        assert_eq!(
            &ParseMentionErrorType::IdNotU64 { found: "&123" },
            Id::<UserMarker>::parse("<@&123>").unwrap_err().kind(),
        );
    }

    #[test]
    fn parse_id_wrong_sigil() {
        assert_eq!(
            &ParseMentionErrorType::Sigil {
                expected: &["@"],
                found: Some('#'),
            },
            super::parse_mention("<#123>", &["@"]).unwrap_err().kind(),
        );
        assert_eq!(
            &ParseMentionErrorType::Sigil {
                expected: &["#"],
                found: None,
            },
            super::parse_mention("<", &["#"]).unwrap_err().kind(),
        );
        assert_eq!(
            &ParseMentionErrorType::Sigil {
                expected: &["#"],
                found: None,
            },
            super::parse_mention("<", &["#"]).unwrap_err().kind(),
        );
    }
}