|
| 1 | +use bstr::{BString, ByteSlice}; |
| 2 | +use std::cmp::Ordering; |
| 3 | +use std::ops::Deref; |
| 4 | + |
| 5 | +#[cfg_attr(test, derive(Debug))] |
| 6 | +#[derive(Clone)] |
| 7 | +enum EncodedString { |
| 8 | + Utf8(String), |
| 9 | + Unknown(BString), |
| 10 | +} |
| 11 | + |
| 12 | +impl Eq for EncodedString {} |
| 13 | + |
| 14 | +impl PartialEq<Self> for EncodedString { |
| 15 | + fn eq(&self, other: &Self) -> bool { |
| 16 | + self.cmp(other) == Ordering::Equal |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +impl PartialOrd<Self> for EncodedString { |
| 21 | + fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
| 22 | + self.cmp(other).into() |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +impl Ord for EncodedString { |
| 27 | + fn cmp(&self, other: &Self) -> Ordering { |
| 28 | + use EncodedString::*; |
| 29 | + match (self, other) { |
| 30 | + (Utf8(a), Utf8(b)) => { |
| 31 | + let a = a.chars().map(|c| c.to_ascii_lowercase()); |
| 32 | + let b = b.chars().map(|c| c.to_ascii_lowercase()); |
| 33 | + a.cmp(b) |
| 34 | + } |
| 35 | + (Unknown(a), Unknown(b)) => a.cmp(b), |
| 36 | + (Utf8(a), Unknown(b)) => a.as_bytes().cmp(b.as_ref()), |
| 37 | + (Unknown(a), Utf8(b)) => a.deref().as_bytes().cmp(b.as_bytes()), |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +#[derive(Clone)] |
| 43 | +pub(crate) struct NameEntry { |
| 44 | + new_name: Option<BString>, |
| 45 | + new_email: Option<BString>, |
| 46 | + old_name: EncodedString, |
| 47 | +} |
| 48 | + |
| 49 | +#[derive(Clone)] |
| 50 | +pub(crate) struct EmailEntry { |
| 51 | + new_name: Option<BString>, |
| 52 | + new_email: Option<BString>, |
| 53 | + old_email: EncodedString, |
| 54 | + |
| 55 | + entries_by_old_name: Vec<NameEntry>, |
| 56 | +} |
| 57 | + |
| 58 | +#[cfg(test)] |
| 59 | +mod encoded_string { |
| 60 | + use crate::snapshot::EncodedString; |
| 61 | + |
| 62 | + #[test] |
| 63 | + fn basic_ascii_case_folding() { |
| 64 | + assert_eq!( |
| 65 | + EncodedString::Utf8("FooBar".into()), |
| 66 | + EncodedString::Utf8("foobar".into()) |
| 67 | + ) |
| 68 | + } |
| 69 | + |
| 70 | + #[test] |
| 71 | + fn no_advanced_unicode_folding() { |
| 72 | + assert_ne!(EncodedString::Utf8("Masse".into()), EncodedString::Utf8("Maße".into())) |
| 73 | + } |
| 74 | + |
| 75 | + #[test] |
| 76 | + fn unknown_encoding_pairs_do_not_try_to_ignore_cases() { |
| 77 | + assert_ne!(EncodedString::Utf8("Foo".into()), EncodedString::Unknown("foo".into())); |
| 78 | + assert_ne!(EncodedString::Unknown("Foo".into()), EncodedString::Utf8("foo".into())); |
| 79 | + assert_ne!( |
| 80 | + EncodedString::Unknown("Foo".into()), |
| 81 | + EncodedString::Unknown("foo".into()) |
| 82 | + ); |
| 83 | + } |
| 84 | +} |
0 commit comments