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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
use std::convert::TryFrom;
use serde::{Deserialize, Serialize};
use typed_builder::TypedBuilder;
use crate::error::{Error, ErrorKind};
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Default, Serialize, Deserialize, TypedBuilder)]
#[serde(rename_all = "camelCase")]
#[builder(field_defaults(setter(into)))]
#[non_exhaustive]
pub struct Collation {
pub locale: String,
#[builder(default)]
pub strength: Option<CollationStrength>,
#[builder(default)]
pub case_level: Option<bool>,
#[builder(default)]
pub case_first: Option<CollationCaseFirst>,
#[builder(default)]
pub numeric_ordering: Option<bool>,
#[builder(default)]
pub alternate: Option<CollationAlternate>,
#[builder(default)]
pub max_variable: Option<CollationMaxVariable>,
#[builder(default)]
pub normalization: Option<bool>,
#[builder(default)]
pub backwards: Option<bool>,
}
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub enum CollationStrength {
Primary,
Secondary,
Tertiary,
Quaternary,
Identical,
}
impl From<CollationStrength> for u32 {
fn from(strength: CollationStrength) -> Self {
match strength {
CollationStrength::Primary => 1,
CollationStrength::Secondary => 2,
CollationStrength::Tertiary => 3,
CollationStrength::Quaternary => 4,
CollationStrength::Identical => 5,
}
}
}
impl TryFrom<u32> for CollationStrength {
type Error = Error;
fn try_from(level: u32) -> Result<Self, Self::Error> {
Ok(match level {
1 => CollationStrength::Primary,
2 => CollationStrength::Secondary,
3 => CollationStrength::Tertiary,
4 => CollationStrength::Quaternary,
5 => CollationStrength::Identical,
_ => {
return Err(ErrorKind::InvalidArgument {
message: (format!("invalid collation strength: {}", level)),
}
.into())
}
})
}
}
impl Serialize for CollationStrength {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let level = u32::from(*self);
serializer.serialize_i32(level as i32)
}
}
impl<'de> Deserialize<'de> for CollationStrength {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let level = u32::deserialize(deserializer)?;
Self::try_from(level).map_err(serde::de::Error::custom)
}
}
impl std::fmt::Display for CollationStrength {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(&u32::from(*self), f)
}
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
pub enum CollationCaseFirst {
Upper,
Lower,
Off,
}
impl std::str::FromStr for CollationCaseFirst {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"upper" => Ok(CollationCaseFirst::Upper),
"lower" => Ok(CollationCaseFirst::Lower),
"off" => Ok(CollationCaseFirst::Off),
_ => Err(ErrorKind::InvalidArgument {
message: format!("invalid CollationCaseFirst: {}", s),
}
.into()),
}
}
}
impl CollationCaseFirst {
pub fn as_str(&self) -> &'static str {
match self {
CollationCaseFirst::Upper => "upper",
CollationCaseFirst::Lower => "lower",
CollationCaseFirst::Off => "off",
}
}
}
impl std::fmt::Display for CollationCaseFirst {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self.as_str(), f)
}
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
pub enum CollationAlternate {
NonIgnorable,
Shifted,
}
impl std::str::FromStr for CollationAlternate {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"non-ignorable" => Ok(CollationAlternate::NonIgnorable),
"shifted" => Ok(CollationAlternate::Shifted),
_ => Err(ErrorKind::InvalidArgument {
message: format!("invalid collation alternate: {}", s),
}
.into()),
}
}
}
impl CollationAlternate {
pub fn as_str(&self) -> &'static str {
match self {
CollationAlternate::NonIgnorable => "non-ignorable",
CollationAlternate::Shifted => "shifted",
}
}
}
impl std::fmt::Display for CollationAlternate {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self.as_str(), f)
}
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
pub enum CollationMaxVariable {
Punct,
Space,
}
impl std::str::FromStr for CollationMaxVariable {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"punct" => Ok(CollationMaxVariable::Punct),
"space" => Ok(CollationMaxVariable::Space),
_ => Err(ErrorKind::InvalidArgument {
message: format!("invalid collation max variable: {}", s),
}
.into()),
}
}
}
impl CollationMaxVariable {
pub fn as_str(&self) -> &'static str {
match self {
CollationMaxVariable::Punct => "punct",
CollationMaxVariable::Space => "space",
}
}
}
impl std::fmt::Display for CollationMaxVariable {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self.as_str(), f)
}
}