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
//! This crate provides an implementation of *language tags* defined by
//! [RFC5646] ([BCP47]).
//!
//! [RFC5646]: <https://tools.ietf.org/html/rfc5646>
//! [BCP47]: <https://tools.ietf.org/html/bcp47>
//!
//! ## Usage
//!
//! You can easily parse new language from any string:
//! ```rust
//! use langtag::LangTag;
//!
//! fn main() -> Result<(), langtag::InvalidLangTag<&'static str>> {
//!   let tag = LangTag::new("fr-FR")?;
//!   assert_eq!(tag.language().unwrap().primary(), "fr");
//!   assert!(tag == "Fr-fr"); // comparison is case-insensitive.
//!   Ok(())
//! }
//! ```
//!
//! Note that [`LangTag::new`] does *not* copy the data it is given,
//! but only borrows it. The [`LangTagBuf`] type allows you to own the language
//! tag. Once parsed, you can explore every component of the language tag using
//! the provided functions.
//!
//! [`LangTag::new`]: crate::LangTag::new
//! [`LangTagBuf`]: crate::LangTagBuf
use std::hash::Hash;

use static_regular_grammar::RegularGrammar;

mod grandfathered;
mod normal;
mod private_use;
mod utils;

pub use grandfathered::*;
pub use normal::*;
pub use private_use::*;
use utils::str_eq;

/// Any language tag (normal, private use or grandfathered).
#[derive(RegularGrammar)]
#[grammar(file = "src/grammar.abnf", cache = "automata/langtag.aut.cbor")]
#[grammar(sized(
	LangTagBuf,
	derive(Debug, Display, PartialEq, Eq, PartialOrd, Ord, Hash)
))]
#[cfg_attr(feature = "serde", grammar(serde))]
pub struct LangTag(str);

impl LangTag {
	/// Returns the language subtags, if any.
	///
	/// Only normal language tags and regular grandfathered tags have language
	/// subtags.
	pub fn language(&self) -> Option<&Language> {
		match NormalLangTag::new(&self.0) {
			Ok(t) => Some(t.language()),
			Err(_) => match GrandfatheredLangTag::new(&self.0) {
				Ok(t) => t.language(),
				Err(_) => None,
			},
		}
	}

	/// Returns the script subtag, if any.
	pub fn script(&self) -> Option<&Script> {
		self.as_normal().and_then(NormalLangTag::script)
	}

	/// Returns the region subtag, if any.
	pub fn region(&self) -> Option<&Region> {
		self.as_normal().and_then(NormalLangTag::region)
	}

	/// Returns the variant subtags, if any.
	pub fn variants(&self) -> &Variants {
		self.as_normal()
			.map(NormalLangTag::variants)
			.unwrap_or(Variants::EMPTY)
	}

	/// Returns the extension subtags, if any.
	pub fn extensions(&self) -> &Extensions {
		self.as_normal()
			.map(NormalLangTag::extensions)
			.unwrap_or(Extensions::EMPTY)
	}

	/// Returns the private use subtag, if any.
	pub fn private_use(&self) -> Option<&PrivateUse> {
		self.as_normal().and_then(NormalLangTag::private_use)
	}

	/// Returns an iterator over the private use subtag subtags.
	pub fn private_use_subtags(&self) -> PrivateUseIter {
		self.private_use()
			.map(PrivateUse::iter)
			.unwrap_or(PrivateUseIter::empty())
	}

	/// Returns wether or not this language tag is a normal language tag.
	pub fn is_normal(&self) -> bool {
		self.as_normal().is_some()
	}

	/// Returns wether or not this language tag is a private use tag.
	pub fn is_private_use(&self) -> bool {
		self.as_private_use().is_some()
	}

	/// Returns wether or not this language tag is a grandfathered tag.
	pub fn is_grandfathered(&self) -> bool {
		self.as_grandfathered().is_some()
	}

	/// Returns this language tag as a normal tag, if it is one.
	pub fn as_normal(&self) -> Option<&NormalLangTag> {
		NormalLangTag::new(&self.0).ok()
	}

	/// Returns this language tag as a private use tag, if it is one.
	pub fn as_private_use(&self) -> Option<&PrivateUseLangTag> {
		PrivateUseLangTag::new(&self.0).ok()
	}

	/// Returns this language tag as a grandfathered tag, if it is one.
	pub fn as_grandfathered(&self) -> Option<GrandfatheredLangTag> {
		GrandfatheredLangTag::new(&self.0).ok()
	}

	/// Returns the kind of the tag (normal, private use or grandfathered).
	pub fn kind(&self) -> Kind {
		self.as_typed().kind()
	}

	/// Find out what kind of language tag `self` is.
	pub fn as_typed(&self) -> TypedLangTag {
		match NormalLangTag::new(&self.0) {
			Ok(t) => TypedLangTag::Normal(t),
			Err(_) => match PrivateUseLangTag::new(&self.0) {
				Ok(t) => TypedLangTag::PrivateUse(t),
				Err(_) => TypedLangTag::Grandfathered(GrandfatheredLangTag::new(&self.0).unwrap()),
			},
		}
	}
}

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

impl Eq for LangTag {}

str_eq!(LangTag);
str_eq!(LangTagBuf);

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

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

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

/// Language tag with type information (normal, private use or grandfathered).
pub enum TypedLangTag<'a> {
	Normal(&'a NormalLangTag),
	PrivateUse(&'a PrivateUseLangTag),
	Grandfathered(GrandfatheredLangTag),
}

impl<'a> TypedLangTag<'a> {
	/// Returns the language subtags, if any.
	///
	/// Only normal language tags and regular grandfathered tags have language
	/// subtags.
	pub fn language(&self) -> Option<&Language> {
		match self {
			Self::Normal(n) => Some(n.language()),
			Self::PrivateUse(_) => None,
			Self::Grandfathered(g) => g.language(),
		}
	}

	/// Returns this tag's kind (normal, private use or grandfathered).
	pub fn kind(&self) -> Kind {
		match self {
			Self::Normal(_) => Kind::Normal,
			Self::PrivateUse(_) => Kind::PrivateUse,
			Self::Grandfathered(_) => Kind::Grandfathered,
		}
	}

	/// Returns wether or not this language tag is a normal language tag.
	pub fn is_normal(&self) -> bool {
		matches!(self, Self::Normal(_))
	}

	/// Returns wether or not this language tag is a private use tag.
	pub fn is_private_use(&self) -> bool {
		matches!(self, Self::PrivateUse(_))
	}

	/// Returns wether or not this language tag is a grandfathered tag.
	pub fn is_grandfathered(&self) -> bool {
		matches!(self, Self::Grandfathered(_))
	}

	/// Returns this language tag as a normal tag, if it is one.
	pub fn as_normal(&self) -> Option<&'a NormalLangTag> {
		match self {
			Self::Normal(n) => Some(*n),
			_ => None,
		}
	}

	/// Returns this language tag as a private use tag, if it is one.
	pub fn as_private_use(&self) -> Option<&'a PrivateUseLangTag> {
		match self {
			Self::PrivateUse(p) => Some(*p),
			_ => None,
		}
	}

	/// Returns this language tag as a grandfathered tag, if it is one.
	pub fn as_grandfathered(&self) -> Option<GrandfatheredLangTag> {
		match self {
			Self::Grandfathered(g) => Some(*g),
			_ => None,
		}
	}
}

/// Language tag kind (normal, private use or grandfathered).
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Kind {
	Normal,
	PrivateUse,
	Grandfathered,
}

impl Kind {
	/// Returns wether or not this is the normal language tag kind.
	pub fn is_normal(&self) -> bool {
		matches!(self, Self::Normal)
	}

	/// Returns wether or not this is the private use tag kind.
	pub fn is_private_use(&self) -> bool {
		matches!(self, Self::PrivateUse)
	}

	/// Returns wether or not this is the grandfathered tag kind.
	pub fn is_grandfathered(&self) -> bool {
		matches!(self, Self::Grandfathered)
	}
}