matrix_sdk_base/
deserialized_responses.rs

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
// Copyright 2022 The Matrix.org Foundation C.I.C.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! SDK-specific variations of response types from Ruma.

use std::{collections::BTreeMap, fmt, hash::Hash, iter};

pub use matrix_sdk_common::deserialized_responses::*;
use once_cell::sync::Lazy;
use regex::Regex;
use ruma::{
    events::{
        room::{
            member::{MembershipState, RoomMemberEvent, RoomMemberEventContent},
            power_levels::{RoomPowerLevels, RoomPowerLevelsEventContent},
        },
        AnyStrippedStateEvent, AnySyncStateEvent, AnySyncTimelineEvent, EventContentFromType,
        PossiblyRedactedStateEventContent, RedactContent, RedactedStateEventContent,
        StateEventContent, StaticStateEventContent, StrippedStateEvent, SyncStateEvent,
    },
    serde::Raw,
    EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedRoomId, OwnedUserId, UserId,
};
use serde::Serialize;
use unicode_normalization::UnicodeNormalization;

/// A change in ambiguity of room members that an `m.room.member` event
/// triggers.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct AmbiguityChange {
    /// The user ID of the member that is contained in the state key of the
    /// `m.room.member` event.
    pub member_id: OwnedUserId,
    /// Is the member that is contained in the state key of the `m.room.member`
    /// event itself ambiguous because of the event.
    pub member_ambiguous: bool,
    /// Has another user been disambiguated because of this event.
    pub disambiguated_member: Option<OwnedUserId>,
    /// Has another user become ambiguous because of this event.
    pub ambiguated_member: Option<OwnedUserId>,
}

impl AmbiguityChange {
    /// Get an iterator over the user IDs listed in this `AmbiguityChange`.
    pub fn user_ids(&self) -> impl Iterator<Item = &UserId> {
        iter::once(&*self.member_id)
            .chain(self.disambiguated_member.as_deref())
            .chain(self.ambiguated_member.as_deref())
    }
}

/// Collection of ambiguity changes that room member events trigger.
#[derive(Clone, Debug, Default)]
#[non_exhaustive]
pub struct AmbiguityChanges {
    /// A map from room id to a map of an event id to the `AmbiguityChange` that
    /// the event with the given id caused.
    pub changes: BTreeMap<OwnedRoomId, BTreeMap<OwnedEventId, AmbiguityChange>>,
}

static MXID_REGEX: Lazy<Regex> = Lazy::new(|| {
    Regex::new(DisplayName::MXID_PATTERN)
        .expect("We should be able to create a regex from our static MXID pattern")
});
static LEFT_TO_RIGHT_REGEX: Lazy<Regex> = Lazy::new(|| {
    Regex::new(DisplayName::LEFT_TO_RIGHT_PATTERN)
        .expect("We should be able to create a regex from our static left-to-right pattern")
});
static HIDDEN_CHARACTERS_REGEX: Lazy<Regex> = Lazy::new(|| {
    Regex::new(DisplayName::HIDDEN_CHARACTERS_PATTERN)
        .expect("We should be able to create a regex from our static hidden characters pattern")
});

/// Regex to match `i` characters.
///
/// This is used to replace an `i` with a lowercase `l`, i.e. to mark "Hello"
/// and "HeIlo" as ambiguous. Decancer will lowercase an `I` for us.
static I_REGEX: Lazy<Regex> = Lazy::new(|| {
    Regex::new("[i]").expect("We should be able to create a regex from our uppercase I pattern")
});

/// Regex to match `0` characters.
///
/// This is used to replace an `0` with a lowercase `o`, i.e. to mark "HellO"
/// and "Hell0" as ambiguous. Decancer will lowercase an `O` for us.
static ZERO_REGEX: Lazy<Regex> = Lazy::new(|| {
    Regex::new("[0]").expect("We should be able to create a regex from our zero pattern")
});

/// Regex to match a couple of dot-like characters, also matches an actual dot.
///
/// This is used to replace a `.` with a `:`, i.e. to mark "@mxid.domain.tld" as
/// ambiguous.
static DOT_REGEX: Lazy<Regex> = Lazy::new(|| {
    Regex::new("[.\u{1d16d}]").expect("We should be able to create a regex from our dot pattern")
});

/// A high-level wrapper for strings representing display names.
///
/// This wrapper provides attempts to determine whether a display name
/// contains characters that could make it ambiguous or easily confused
/// with similar names.
///
///
/// # Examples
///
/// ```
/// use matrix_sdk_base::deserialized_responses::DisplayName;
///
/// let display_name = DisplayName::new("๐’ฎ๐’ถ๐’ฝ๐’ถ๐“ˆ๐“‡๐’ถ๐’ฝ๐“๐’ถ");
///
/// // The normalized and sanitized string will be returned by DisplayName.as_normalized_str().
/// assert_eq!(display_name.as_normalized_str(), Some("sahasrahla"));
/// ```
///
/// ```
/// # use matrix_sdk_base::deserialized_responses::DisplayName;
/// let display_name = DisplayName::new("@alice:localhost");
///
/// // The display name looks like an MXID, which makes it ambiguous.
/// assert!(display_name.is_inherently_ambiguous());
/// ```
#[derive(Debug, Clone, Eq)]
pub struct DisplayName {
    raw: String,
    decancered: Option<String>,
}

impl Hash for DisplayName {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        if let Some(decancered) = &self.decancered {
            decancered.hash(state);
        } else {
            self.raw.hash(state);
        }
    }
}

impl PartialEq for DisplayName {
    fn eq(&self, other: &Self) -> bool {
        match (self.decancered.as_deref(), other.decancered.as_deref()) {
            (None, None) => self.raw == other.raw,
            (None, Some(_)) | (Some(_), None) => false,
            (Some(this), Some(other)) => this == other,
        }
    }
}

impl DisplayName {
    /// Regex pattern matching an MXID.
    const MXID_PATTERN: &'static str = "@.+[:.].+";

    /// Regex pattern matching some left-to-right formatting marks:
    ///     * LTR and RTL marks U+200E and U+200F
    ///     * LTR/RTL and other directional formatting marks U+202A - U+202F
    const LEFT_TO_RIGHT_PATTERN: &'static str = "[\u{202a}-\u{202f}\u{200e}\u{200f}]";

    /// Regex pattern matching bunch of unicode control characters and otherwise
    /// misleading/invisible characters.
    ///
    /// This includes:
    ///     * various width spaces U+2000 - U+200D
    ///     * Combining characters U+0300 - U+036F
    ///     * Blank/invisible characters (U2800, U2062-U2063)
    ///     * Arabic Letter RTL mark U+061C
    ///     * Zero width no-break space (BOM) U+FEFF
    const HIDDEN_CHARACTERS_PATTERN: &'static str =
        "[\u{2000}-\u{200D}\u{300}-\u{036f}\u{2062}-\u{2063}\u{2800}\u{061c}\u{feff}]";

    /// Creates a new [`DisplayName`] from the given raw string.
    ///
    /// The raw display name is transformed into a Unicode-normalized form, with
    /// common confusable characters removed to reduce ambiguity.
    ///
    /// **Note**: If removing confusable characters fails,
    /// [`DisplayName::is_inherently_ambiguous`] will return `true`, and
    /// [`DisplayName::as_normalized_str()`] will return `None.
    pub fn new(raw: &str) -> Self {
        let normalized = raw.nfd().collect::<String>();
        let replaced = DOT_REGEX.replace_all(&normalized, ":");
        let replaced = HIDDEN_CHARACTERS_REGEX.replace_all(&replaced, "");

        let decancered = decancer::cure!(&replaced).ok().map(|cured| {
            let removed_left_to_right = LEFT_TO_RIGHT_REGEX.replace_all(cured.as_ref(), "");
            let replaced = I_REGEX.replace_all(&removed_left_to_right, "l");
            // We re-run the dot replacement because decancer normalized a lot of weird
            // characets into a `.`, it just doesn't do that for /u{1d16d}.
            let replaced = DOT_REGEX.replace_all(&replaced, ":");
            let replaced = ZERO_REGEX.replace_all(&replaced, "o");

            replaced.to_string()
        });

        Self { raw: raw.to_owned(), decancered }
    }

    /// Is this display name considered to be ambiguous?
    ///
    /// If the display name has cancer (i.e. fails normalisation or has a
    /// different normalised form) or looks like an MXID, then it's ambiguous.
    pub fn is_inherently_ambiguous(&self) -> bool {
        // If we look like an MXID or have hidden characters then we're ambiguous.
        self.looks_like_an_mxid() || self.has_hidden_characters() || self.decancered.is_none()
    }

    /// Returns the underlying raw and and unsanitized string of this
    /// [`DisplayName`].
    pub fn as_raw_str(&self) -> &str {
        &self.raw
    }

    /// Returns the underlying normalized and and sanitized string of this
    /// [`DisplayName`].
    ///
    /// Returns `None` if normalization failed during construction of this
    /// [`DisplayName`].
    pub fn as_normalized_str(&self) -> Option<&str> {
        self.decancered.as_deref()
    }

    fn has_hidden_characters(&self) -> bool {
        HIDDEN_CHARACTERS_REGEX.is_match(&self.raw)
    }

    fn looks_like_an_mxid(&self) -> bool {
        self.decancered
            .as_deref()
            .map(|d| MXID_REGEX.is_match(d))
            .unwrap_or_else(|| MXID_REGEX.is_match(&self.raw))
    }
}

/// A deserialized response for the rooms members API call.
///
/// [`GET /_matrix/client/r0/rooms/{roomId}/members`](https://spec.matrix.org/v1.5/client-server-api/#get_matrixclientv3roomsroomidmembers)
#[derive(Clone, Debug, Default)]
pub struct MembersResponse {
    /// The list of members events.
    pub chunk: Vec<RoomMemberEvent>,
    /// Collection of ambiguity changes that room member events trigger.
    pub ambiguity_changes: AmbiguityChanges,
}

/// Wrapper around both versions of any event received via sync.
#[derive(Clone, Debug, Serialize)]
#[serde(untagged)]
pub enum RawAnySyncOrStrippedTimelineEvent {
    /// An event from a room in joined or left state.
    Sync(Raw<AnySyncTimelineEvent>),
    /// An event from a room in invited state.
    Stripped(Raw<AnyStrippedStateEvent>),
}

/// Wrapper around both versions of any raw state event.
#[derive(Clone, Debug, Serialize)]
#[serde(untagged)]
pub enum RawAnySyncOrStrippedState {
    /// An event from a room in joined or left state.
    Sync(Raw<AnySyncStateEvent>),
    /// An event from a room in invited state.
    Stripped(Raw<AnyStrippedStateEvent>),
}

impl RawAnySyncOrStrippedState {
    /// Try to deserialize the inner JSON as the expected type.
    pub fn deserialize(&self) -> serde_json::Result<AnySyncOrStrippedState> {
        match self {
            Self::Sync(raw) => Ok(AnySyncOrStrippedState::Sync(raw.deserialize()?)),
            Self::Stripped(raw) => Ok(AnySyncOrStrippedState::Stripped(raw.deserialize()?)),
        }
    }

    /// Turns this `RawAnySyncOrStrippedState` into `RawSyncOrStrippedState<C>`
    /// without changing the underlying JSON.
    pub fn cast<C>(self) -> RawSyncOrStrippedState<C>
    where
        C: StaticStateEventContent + RedactContent,
        C::Redacted: RedactedStateEventContent,
    {
        match self {
            Self::Sync(raw) => RawSyncOrStrippedState::Sync(raw.cast()),
            Self::Stripped(raw) => RawSyncOrStrippedState::Stripped(raw.cast()),
        }
    }
}

/// Wrapper around both versions of any state event.
#[derive(Clone, Debug)]
pub enum AnySyncOrStrippedState {
    /// An event from a room in joined or left state.
    Sync(AnySyncStateEvent),
    /// An event from a room in invited state.
    Stripped(AnyStrippedStateEvent),
}

impl AnySyncOrStrippedState {
    /// If this is an `AnySyncStateEvent`, return a reference to the inner
    /// event.
    pub fn as_sync(&self) -> Option<&AnySyncStateEvent> {
        match self {
            Self::Sync(ev) => Some(ev),
            Self::Stripped(_) => None,
        }
    }

    /// If this is an `AnyStrippedStateEvent`, return a reference to the inner
    /// event.
    pub fn as_stripped(&self) -> Option<&AnyStrippedStateEvent> {
        match self {
            Self::Sync(_) => None,
            Self::Stripped(ev) => Some(ev),
        }
    }
}

/// Wrapper around both versions of a raw state event.
#[derive(Clone, Debug, Serialize)]
#[serde(untagged)]
pub enum RawSyncOrStrippedState<C>
where
    C: StaticStateEventContent + RedactContent,
    C::Redacted: RedactedStateEventContent,
{
    /// An event from a room in joined or left state.
    Sync(Raw<SyncStateEvent<C>>),
    /// An event from a room in invited state.
    Stripped(Raw<StrippedStateEvent<C::PossiblyRedacted>>),
}

impl<C> RawSyncOrStrippedState<C>
where
    C: StaticStateEventContent + RedactContent,
    C::Redacted: RedactedStateEventContent + fmt::Debug + Clone,
{
    /// Try to deserialize the inner JSON as the expected type.
    pub fn deserialize(&self) -> serde_json::Result<SyncOrStrippedState<C>>
    where
        C: StaticStateEventContent + EventContentFromType + RedactContent,
        C::Redacted: RedactedStateEventContent<StateKey = C::StateKey> + EventContentFromType,
        C::PossiblyRedacted: PossiblyRedactedStateEventContent + EventContentFromType,
    {
        match self {
            Self::Sync(ev) => Ok(SyncOrStrippedState::Sync(ev.deserialize()?)),
            Self::Stripped(ev) => Ok(SyncOrStrippedState::Stripped(ev.deserialize()?)),
        }
    }
}

/// Raw version of [`MemberEvent`].
pub type RawMemberEvent = RawSyncOrStrippedState<RoomMemberEventContent>;

/// Wrapper around both versions of a state event.
#[derive(Clone, Debug)]
pub enum SyncOrStrippedState<C>
where
    C: StaticStateEventContent + RedactContent,
    C::Redacted: RedactedStateEventContent + fmt::Debug + Clone,
{
    /// An event from a room in joined or left state.
    Sync(SyncStateEvent<C>),
    /// An event from a room in invited state.
    Stripped(StrippedStateEvent<C::PossiblyRedacted>),
}

impl<C> SyncOrStrippedState<C>
where
    C: StaticStateEventContent + RedactContent,
    C::Redacted: RedactedStateEventContent<StateKey = C::StateKey> + fmt::Debug + Clone,
    C::PossiblyRedacted: PossiblyRedactedStateEventContent<StateKey = C::StateKey>,
{
    /// If this is a `SyncStateEvent`, return a reference to the inner event.
    pub fn as_sync(&self) -> Option<&SyncStateEvent<C>> {
        match self {
            Self::Sync(ev) => Some(ev),
            Self::Stripped(_) => None,
        }
    }

    /// If this is a `StrippedStateEvent`, return a reference to the inner
    /// event.
    pub fn as_stripped(&self) -> Option<&StrippedStateEvent<C::PossiblyRedacted>> {
        match self {
            Self::Sync(_) => None,
            Self::Stripped(ev) => Some(ev),
        }
    }

    /// The sender of this event.
    pub fn sender(&self) -> &UserId {
        match self {
            Self::Sync(e) => e.sender(),
            Self::Stripped(e) => &e.sender,
        }
    }

    /// The ID of this event.
    pub fn event_id(&self) -> Option<&EventId> {
        match self {
            Self::Sync(e) => Some(e.event_id()),
            Self::Stripped(_) => None,
        }
    }

    /// The server timestamp of this event.
    pub fn origin_server_ts(&self) -> Option<MilliSecondsSinceUnixEpoch> {
        match self {
            Self::Sync(e) => Some(e.origin_server_ts()),
            Self::Stripped(_) => None,
        }
    }

    /// The state key associated to this state event.
    pub fn state_key(&self) -> &C::StateKey {
        match self {
            Self::Sync(e) => e.state_key(),
            Self::Stripped(e) => &e.state_key,
        }
    }
}

impl<C> SyncOrStrippedState<C>
where
    C: StaticStateEventContent<PossiblyRedacted = C>
        + RedactContent
        + PossiblyRedactedStateEventContent,
    C::Redacted: RedactedStateEventContent<StateKey = <C as StateEventContent>::StateKey>
        + fmt::Debug
        + Clone,
{
    /// The inner content of the wrapped event.
    pub fn original_content(&self) -> Option<&C> {
        match self {
            Self::Sync(e) => e.as_original().map(|e| &e.content),
            Self::Stripped(e) => Some(&e.content),
        }
    }
}

/// Wrapper around both MemberEvent-Types
pub type MemberEvent = SyncOrStrippedState<RoomMemberEventContent>;

impl MemberEvent {
    /// The membership state of the user.
    pub fn membership(&self) -> &MembershipState {
        match self {
            MemberEvent::Sync(e) => e.membership(),
            MemberEvent::Stripped(e) => &e.content.membership,
        }
    }

    /// The user id associated to this member event.
    pub fn user_id(&self) -> &UserId {
        self.state_key()
    }

    /// The name that should be displayed for this member event.
    ///
    /// It there is no `displayname` in the event's content, the localpart or
    /// the user ID is returned.
    pub fn display_name(&self) -> DisplayName {
        DisplayName::new(
            self.original_content()
                .and_then(|c| c.displayname.as_deref())
                .unwrap_or_else(|| self.user_id().localpart()),
        )
    }
}

impl SyncOrStrippedState<RoomPowerLevelsEventContent> {
    /// The power levels of the event.
    pub fn power_levels(&self) -> RoomPowerLevels {
        match self {
            Self::Sync(e) => e.power_levels(),
            Self::Stripped(e) => e.power_levels(),
        }
    }
}

#[cfg(test)]
mod test {
    macro_rules! assert_display_name_eq {
        ($left:expr, $right:expr $(, $desc:expr)?) => {{
            let left = crate::deserialized_responses::DisplayName::new($left);
            let right = crate::deserialized_responses::DisplayName::new($right);

            similar_asserts::assert_eq!(
                left,
                right
                $(, $desc)?
            );
        }};
    }

    macro_rules! assert_display_name_ne {
        ($left:expr, $right:expr $(, $desc:expr)?) => {{
            let left = crate::deserialized_responses::DisplayName::new($left);
            let right = crate::deserialized_responses::DisplayName::new($right);

            assert_ne!(
                left,
                right
                $(, $desc)?
            );
        }};
    }

    macro_rules! assert_ambiguous {
        ($name:expr) => {
            let name = crate::deserialized_responses::DisplayName::new($name);

            assert!(
                name.is_inherently_ambiguous(),
                "The display {:?} should be considered amgibuous",
                name
            );
        };
    }

    macro_rules! assert_not_ambiguous {
        ($name:expr) => {
            let name = crate::deserialized_responses::DisplayName::new($name);

            assert!(
                !name.is_inherently_ambiguous(),
                "The display {:?} should not be considered amgibuous",
                name
            );
        };
    }

    #[test]
    fn test_display_name_inherently_ambiguous() {
        // These should not be inherently ambiguous, only if another similarly looking
        // display name appears should they be considered to be ambiguous.
        assert_not_ambiguous!("Alice");
        assert_not_ambiguous!("Carol");
        assert_not_ambiguous!("Car0l");
        assert_not_ambiguous!("Ivan");
        assert_not_ambiguous!("๐’ฎ๐’ถ๐’ฝ๐’ถ๐“ˆ๐“‡๐’ถ๐’ฝ๐“๐’ถ");
        assert_not_ambiguous!("โ“ˆโ“โ“—โ“โ“ขโ“กโ“โ“—โ“›โ“");
        assert_not_ambiguous!("๐Ÿ…‚๐Ÿ„ฐ๐Ÿ„ท๐Ÿ„ฐ๐Ÿ…‚๐Ÿ…๐Ÿ„ฐ๐Ÿ„ท๐Ÿ„ป๐Ÿ„ฐ");
        assert_not_ambiguous!("๏ผณ๏ฝ๏ฝˆ๏ฝ๏ฝ“๏ฝ’๏ฝ๏ฝˆ๏ฝŒ๏ฝ");
        // Left to right is fine, if it's the only one in the room.
        assert_not_ambiguous!("\u{202e}alharsahas");

        // These on the other hand contain invisible chars.
        assert_ambiguous!("Saฬดhasrahla");
        assert_ambiguous!("Sahas\u{200D}rahla");
    }

    #[test]
    fn test_display_name_equality_capitalization() {
        // Display name with different capitalization
        assert_display_name_eq!("Alice", "alice");
    }

    #[test]
    fn test_display_name_equality_different_names() {
        // Different display names
        assert_display_name_ne!("Alice", "Carol");
    }

    #[test]
    fn test_display_name_equality_capital_l() {
        // Different display names
        assert_display_name_eq!("Hello", "HeIlo");
    }

    #[test]
    fn test_display_name_equality_confusable_zero() {
        // Different display names
        assert_display_name_eq!("Carol", "Car0l");
    }

    #[test]
    fn test_display_name_equality_cyrilic() {
        // Display name with scritpure symbols
        assert_display_name_eq!("alice", "ะฐlice");
    }

    #[test]
    fn test_display_name_equality_scriptures() {
        // Display name with scritpure symbols
        assert_display_name_eq!("Sahasrahla", "๐’ฎ๐’ถ๐’ฝ๐’ถ๐“ˆ๐“‡๐’ถ๐’ฝ๐“๐’ถ");
    }

    #[test]
    fn test_display_name_equality_frakturs() {
        // Display name with fraktur symbols
        assert_display_name_eq!("Sahasrahla", "๐”–๐”ž๐”ฅ๐”ž๐”ฐ๐”ฏ๐”ž๐”ฅ๐”ฉ๐”ž");
    }

    #[test]
    fn test_display_name_equality_circled() {
        // Display name with circled symbols
        assert_display_name_eq!("Sahasrahla", "โ“ˆโ“โ“—โ“โ“ขโ“กโ“โ“—โ“›โ“");
    }

    #[test]
    fn test_display_name_equality_squared() {
        // Display name with squared symbols
        assert_display_name_eq!("Sahasrahla", "๐Ÿ…‚๐Ÿ„ฐ๐Ÿ„ท๐Ÿ„ฐ๐Ÿ…‚๐Ÿ…๐Ÿ„ฐ๐Ÿ„ท๐Ÿ„ป๐Ÿ„ฐ");
    }

    #[test]
    fn test_display_name_equality_big_unicode() {
        // Display name with big unicode letters
        assert_display_name_eq!("Sahasrahla", "๏ผณ๏ฝ๏ฝˆ๏ฝ๏ฝ“๏ฝ’๏ฝ๏ฝˆ๏ฝŒ๏ฝ");
    }

    #[test]
    fn test_display_name_equality_left_to_right() {
        // Display name with a left-to-right character
        assert_display_name_eq!("Sahasrahla", "\u{202e}alharsahas");
    }

    #[test]
    fn test_display_name_equality_diacritical() {
        // Display name with a diacritical mark.
        assert_display_name_eq!("Sahasrahla", "Saฬดhasrahla");
    }

    #[test]
    fn test_display_name_equality_zero_width_joiner() {
        // Display name with a zero-width joiner
        assert_display_name_eq!("Sahasrahla", "Sahas\u{200B}rahla");
    }

    #[test]
    fn test_display_name_equality_zero_width_space() {
        // Display name with zero-width space.
        assert_display_name_eq!("Sahasrahla", "Sahas\u{200D}rahla");
    }

    #[test]
    fn test_display_name_equality_ligatures() {
        // Display name with a ligature.
        assert_display_name_eq!("ff", "\u{FB00}");
    }

    #[test]
    fn test_display_name_confusable_mxid_colon() {
        assert_display_name_eq!("@mxid:domain.tld", "@mxid\u{0589}domain.tld");
        assert_display_name_eq!("@mxid:domain.tld", "@mxid\u{05c3}domain.tld");
        assert_display_name_eq!("@mxid:domain.tld", "@mxid\u{0703}domain.tld");
        assert_display_name_eq!("@mxid:domain.tld", "@mxid\u{0a83}domain.tld");
        assert_display_name_eq!("@mxid:domain.tld", "@mxid\u{16ec}domain.tld");
        assert_display_name_eq!("@mxid:domain.tld", "@mxid\u{205a}domain.tld");
        assert_display_name_eq!("@mxid:domain.tld", "@mxid\u{2236}domain.tld");
        assert_display_name_eq!("@mxid:domain.tld", "@mxid\u{fe13}domain.tld");
        assert_display_name_eq!("@mxid:domain.tld", "@mxid\u{fe52}domain.tld");
        assert_display_name_eq!("@mxid:domain.tld", "@mxid\u{fe30}domain.tld");
        assert_display_name_eq!("@mxid:domain.tld", "@mxid\u{ff1a}domain.tld");

        // Additionally these should be considered to be ambiguous on their own.
        assert_ambiguous!("@mxid\u{0589}domain.tld");
        assert_ambiguous!("@mxid\u{05c3}domain.tld");
        assert_ambiguous!("@mxid\u{0703}domain.tld");
        assert_ambiguous!("@mxid\u{0a83}domain.tld");
        assert_ambiguous!("@mxid\u{16ec}domain.tld");
        assert_ambiguous!("@mxid\u{205a}domain.tld");
        assert_ambiguous!("@mxid\u{2236}domain.tld");
        assert_ambiguous!("@mxid\u{fe13}domain.tld");
        assert_ambiguous!("@mxid\u{fe52}domain.tld");
        assert_ambiguous!("@mxid\u{fe30}domain.tld");
        assert_ambiguous!("@mxid\u{ff1a}domain.tld");
    }

    #[test]
    fn test_display_name_confusable_mxid_dot() {
        assert_display_name_eq!("@mxid:domain.tld", "@mxid:domain\u{0701}tld");
        assert_display_name_eq!("@mxid:domain.tld", "@mxid:domain\u{0702}tld");
        assert_display_name_eq!("@mxid:domain.tld", "@mxid:domain\u{2024}tld");
        assert_display_name_eq!("@mxid:domain.tld", "@mxid:domain\u{fe52}tld");
        assert_display_name_eq!("@mxid:domain.tld", "@mxid:domain\u{ff0e}tld");
        assert_display_name_eq!("@mxid:domain.tld", "@mxid:domain\u{1d16d}tld");

        // Additionally these should be considered to be ambiguous on their own.
        assert_ambiguous!("@mxid:domain\u{0701}tld");
        assert_ambiguous!("@mxid:domain\u{0702}tld");
        assert_ambiguous!("@mxid:domain\u{2024}tld");
        assert_ambiguous!("@mxid:domain\u{fe52}tld");
        assert_ambiguous!("@mxid:domain\u{ff0e}tld");
        assert_ambiguous!("@mxid:domain\u{1d16d}tld");
    }

    #[test]
    fn test_display_name_confusable_mxid_replacing_a() {
        assert_display_name_eq!("@mxid:domain.tld", "@mxid:dom\u{1d44e}in.tld");
        assert_display_name_eq!("@mxid:domain.tld", "@mxid:dom\u{0430}in.tld");

        // Additionally these should be considered to be ambiguous on their own.
        assert_ambiguous!("@mxid:dom\u{1d44e}in.tld");
        assert_ambiguous!("@mxid:dom\u{0430}in.tld");
    }

    #[test]
    fn test_display_name_confusable_mxid_replacing_l() {
        assert_display_name_eq!("@mxid:domain.tld", "@mxid:domain.tId");
        assert_display_name_eq!("mxid:domain.tld", "mxid:domain.t\u{217c}d");
        assert_display_name_eq!("mxid:domain.tld", "mxid:domain.t\u{ff4c}d");
        assert_display_name_eq!("mxid:domain.tld", "mxid:domain.t\u{1d5f9}d");
        assert_display_name_eq!("mxid:domain.tld", "mxid:domain.t\u{1d695}d");
        assert_display_name_eq!("mxid:domain.tld", "mxid:domain.t\u{2223}d");

        // Additionally these should be considered to be ambiguous on their own.
        assert_ambiguous!("@mxid:domain.tId");
        assert_ambiguous!("@mxid:domain.t\u{217c}d");
        assert_ambiguous!("@mxid:domain.t\u{ff4c}d");
        assert_ambiguous!("@mxid:domain.t\u{1d5f9}d");
        assert_ambiguous!("@mxid:domain.t\u{1d695}d");
        assert_ambiguous!("@mxid:domain.t\u{2223}d");
    }
}