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
use langtag::{LanguageTag, LanguageTagBuf};
use locspan_derive::StrippedPartialEq;
use std::fmt;

/// Language tag buffer that may not be well-formed.
#[derive(Clone, PartialEq, StrippedPartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
pub enum LenientLanguageTagBuf {
	WellFormed(#[locspan(stripped)] LanguageTagBuf),
	Malformed(#[locspan(stripped)] String),
}

impl LenientLanguageTagBuf {
	pub fn new(s: String) -> (Self, Option<langtag::Error>) {
		match LanguageTagBuf::new(s.into_bytes()) {
			Ok(lang) => (Self::WellFormed(lang), None),
			Err((err, lang)) => (
				Self::Malformed(unsafe { String::from_utf8_unchecked(lang) }),
				Some(err),
			),
		}
	}

	pub fn is_well_formed(&self) -> bool {
		matches!(self, Self::WellFormed(_))
	}

	pub fn as_ref(&self) -> LenientLanguageTag<'_> {
		match self {
			Self::WellFormed(tag) => LenientLanguageTag::WellFormed(tag.as_ref()),
			Self::Malformed(tag) => LenientLanguageTag::Malformed(tag.as_ref()),
		}
	}

	pub fn as_language_tag(&self) -> Option<LanguageTag<'_>> {
		match self {
			Self::WellFormed(tag) => Some(tag.as_ref()),
			_ => None,
		}
	}

	pub fn as_str(&self) -> &str {
		match self {
			Self::WellFormed(tag) => tag.as_str(),
			Self::Malformed(tag) => tag.as_str(),
		}
	}

	pub fn into_string(self) -> String {
		match self {
			Self::WellFormed(LanguageTagBuf::Normal(n)) => unsafe {
				String::from_utf8_unchecked(n.into_inner())
			},
			Self::WellFormed(LanguageTagBuf::PrivateUse(p)) => unsafe {
				String::from_utf8_unchecked(p.into_inner())
			},
			Self::WellFormed(LanguageTagBuf::Grandfathered(g)) => g.to_string(),
			Self::Malformed(s) => s,
		}
	}
}

impl From<LanguageTagBuf> for LenientLanguageTagBuf {
	fn from(tag: LanguageTagBuf) -> Self {
		Self::WellFormed(tag)
	}
}

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

impl fmt::Display for LenientLanguageTagBuf {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		match self {
			Self::WellFormed(tag) => tag.fmt(f),
			Self::Malformed(tag) => tag.fmt(f),
		}
	}
}

/// Language tag that may not be well-formed.
#[derive(Clone, Copy, PartialEq, StrippedPartialEq, Eq, Hash, Debug)]
pub enum LenientLanguageTag<'a> {
	WellFormed(#[locspan(stripped)] LanguageTag<'a>),
	Malformed(#[locspan(stripped)] &'a str),
}

impl<'a> LenientLanguageTag<'a> {
	pub fn is_well_formed(&self) -> bool {
		matches!(self, Self::WellFormed(_))
	}

	pub fn as_language_tag(&self) -> Option<LanguageTag<'a>> {
		match self {
			Self::WellFormed(tag) => Some(*tag),
			_ => None,
		}
	}

	pub fn as_str(&self) -> &str {
		match self {
			Self::WellFormed(tag) => tag.as_str(),
			Self::Malformed(tag) => tag,
		}
	}

	pub fn to_owned(self) -> LenientLanguageTagBuf {
		match self {
			Self::WellFormed(tag) => LenientLanguageTagBuf::WellFormed(tag.cloned()),
			Self::Malformed(tag) => LenientLanguageTagBuf::Malformed(tag.to_string()),
		}
	}
}

impl<'a> fmt::Display for LenientLanguageTag<'a> {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		match self {
			Self::WellFormed(tag) => tag.fmt(f),
			Self::Malformed(tag) => tag.fmt(f),
		}
	}
}