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
pub use langtag::{InvalidLangTag, LangTag, LangTagBuf};
use std::{borrow::Borrow, fmt, hash::Hash, ops::Deref};

use crate::utils::{case_insensitive_cmp, case_insensitive_eq, case_insensitive_hash};

/// Language tag that may not be well-formed.
#[derive(Debug)]
pub struct LenientLangTag(str);

impl LenientLangTag {
	pub fn new(s: &str) -> (&Self, Option<InvalidLangTag<&str>>) {
		let err = LangTag::new(s).err();
		(unsafe { std::mem::transmute(s) }, err)
	}

	pub fn as_bytes(&self) -> &[u8] {
		self.0.as_bytes()
	}

	pub fn as_str(&self) -> &str {
		&self.0
	}

	pub fn is_well_formed(&self) -> bool {
		LangTag::new(self.as_str()).is_ok()
	}

	pub fn as_well_formed(&self) -> Option<&LangTag> {
		LangTag::new(self.as_str()).ok()
	}
}

impl PartialEq for LenientLangTag {
	fn eq(&self, other: &Self) -> bool {
		case_insensitive_eq(self.as_bytes(), other.as_bytes())
	}
}

impl Eq for LenientLangTag {}

impl PartialOrd for LenientLangTag {
	fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
		Some(self.cmp(other))
	}
}

impl Ord for LenientLangTag {
	fn cmp(&self, other: &Self) -> std::cmp::Ordering {
		case_insensitive_cmp(self.as_bytes(), other.as_bytes())
	}
}

impl Hash for LenientLangTag {
	fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
		case_insensitive_hash(self.as_bytes(), state)
	}
}

impl ToOwned for LenientLangTag {
	type Owned = LenientLangTagBuf;

	fn to_owned(&self) -> Self::Owned {
		LenientLangTagBuf(self.0.to_owned())
	}
}

impl Borrow<str> for LenientLangTag {
	fn borrow(&self) -> &str {
		self.as_str()
	}
}

impl AsRef<str> for LenientLangTag {
	fn as_ref(&self) -> &str {
		self.as_str()
	}
}

impl fmt::Display for LenientLangTag {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		self.0.fmt(f)
	}
}

/// Owned language tag that may not be well-formed.
#[derive(Debug, Clone)]
pub struct LenientLangTagBuf(String);

impl LenientLangTagBuf {
	pub fn new(s: String) -> (Self, Option<InvalidLangTag<String>>) {
		let err = LangTag::new(s.as_str())
			.err()
			.map(|InvalidLangTag(s)| InvalidLangTag(s.to_owned()));
		(Self(s), err)
	}

	pub fn as_lenient_lang_tag_ref(&self) -> &LenientLangTag {
		unsafe { std::mem::transmute(self.0.as_str()) }
	}

	pub fn into_string(self) -> String {
		self.0
	}

	pub fn into_well_formed(self) -> Result<LangTagBuf, InvalidLangTag<String>> {
		LangTagBuf::new(self.0)
	}
}

impl Deref for LenientLangTagBuf {
	type Target = LenientLangTag;

	fn deref(&self) -> &Self::Target {
		self.as_lenient_lang_tag_ref()
	}
}

impl PartialEq for LenientLangTagBuf {
	fn eq(&self, other: &Self) -> bool {
		self.as_lenient_lang_tag_ref()
			.eq(other.as_lenient_lang_tag_ref())
	}
}

impl Eq for LenientLangTagBuf {}

impl PartialOrd for LenientLangTagBuf {
	fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
		Some(self.cmp(other))
	}
}

impl Ord for LenientLangTagBuf {
	fn cmp(&self, other: &Self) -> std::cmp::Ordering {
		self.as_lenient_lang_tag_ref()
			.cmp(other.as_lenient_lang_tag_ref())
	}
}

impl Hash for LenientLangTagBuf {
	fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
		self.as_lenient_lang_tag_ref().hash(state)
	}
}

impl Borrow<LenientLangTag> for LenientLangTagBuf {
	fn borrow(&self) -> &LenientLangTag {
		self.as_lenient_lang_tag_ref()
	}
}

impl AsRef<LenientLangTag> for LenientLangTagBuf {
	fn as_ref(&self) -> &LenientLangTag {
		self.as_lenient_lang_tag_ref()
	}
}

impl From<LangTagBuf> for LenientLangTagBuf {
	fn from(tag: LangTagBuf) -> Self {
		Self(tag.into_string())
	}
}

impl From<String> for LenientLangTagBuf {
	fn from(tag: String) -> Self {
		Self(tag)
	}
}

impl fmt::Display for LenientLangTagBuf {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		self.0.fmt(f)
	}
}

#[cfg(feature = "serde")]
impl serde::Serialize for LenientLangTagBuf {
	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
	where
		S: serde::Serializer,
	{
		self.as_str().serialize(serializer)
	}
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for LenientLangTagBuf {
	fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
	where
		D: serde::Deserializer<'de>,
	{
		Ok(Self(String::deserialize(deserializer)?))
	}
}