Skip to main content

langtag/normal/
region.rs

1use core::hash::Hash;
2
3use crate::utils;
4
5/// Region subtag.
6///
7/// Region subtags are used to indicate linguistic variations associated
8/// with or appropriate to a specific country, territory, or region.
9/// Typically, a region subtag is used to indicate variations such as
10/// regional dialects or usage, or region-specific spelling conventions.
11/// It can also be used to indicate that content is expressed in a way
12/// that is appropriate for use throughout a region, for instance,
13/// Spanish content tailored to be useful throughout Latin America.
14#[derive(static_automata::Validate, str_newtype::StrNewType)]
15#[automaton(crate::grammar::Region)]
16#[newtype(
17	no_deref,
18	ord([u8], &[u8], str, &str)
19)]
20#[cfg_attr(
21	feature = "std",
22	newtype(ord(Vec<u8>, String), owned(RegionBuf, derive(PartialEq, Eq, PartialOrd, Ord, Hash)))
23)]
24#[cfg_attr(feature = "serde", newtype(serde))]
25pub struct Region(str);
26
27impl PartialEq for Region {
28	fn eq(&self, other: &Self) -> bool {
29		utils::case_insensitive_eq(self.as_bytes(), other.as_bytes())
30	}
31}
32
33impl Eq for Region {}
34
35impl PartialOrd for Region {
36	fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
37		Some(self.cmp(other))
38	}
39}
40
41impl Ord for Region {
42	fn cmp(&self, other: &Self) -> core::cmp::Ordering {
43		utils::case_insensitive_cmp(self.as_bytes(), other.as_bytes())
44	}
45}
46
47impl Hash for Region {
48	fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
49		utils::case_insensitive_hash(self.as_bytes(), state)
50	}
51}