Skip to main content

tiberius/tds/
collation.rs

1//! legacy implementation of collations (or codepages rather) for dealing with varchar's with legacy databases
2//! references [1] which has some mappings from the katmai (SQL Server 2008) source code and is a TDS driver
3//! directly from microsoft
4//! [2] is helpful to map CP1234 to the appropriate encoding
5//!
6//! [1] <https://github.com/Microsoft/mssql-jdbc/blob/eb14f63077c47ef1fc1c690deb8cfab602baeb85/src/main/java/com/microsoft/sqlserver/jdbc/SQLCollation.java>
7//! [2] <https://github.com/lifthrasiir/rust-encoding/blob/496823171f15d9b9446b2ec3fb7765f22346256b/src/label.rs#L282>
8
9use encoding_rs::Encoding;
10use std::fmt;
11
12use crate::error::Error;
13
14/// The collation of a character column, describing its locale (LCID), sort
15/// order and code page.
16#[derive(Debug, Clone, Copy, PartialEq, Eq)]
17pub struct Collation {
18    /// LCID ColFlags Version
19    info: u32,
20    /// Sortid
21    sort_id: u8,
22}
23
24impl Collation {
25    /// Create a new collation from a raw LCID/flags/version word and sort id.
26    pub fn new(info: u32, sort_id: u8) -> Self {
27        Self { info, sort_id }
28    }
29
30    /// return the locale id part of the LCID (the specification here uses ambiguous terms)
31    pub fn lcid(&self) -> u16 {
32        (self.info & 0xffff) as u16
33    }
34
35    /// The sort id of the collation.
36    pub fn sort_id(&self) -> u8 {
37        self.sort_id
38    }
39
40    /// The raw LCID/flags/version word of the collation.
41    pub fn info(&self) -> u32 {
42        self.info
43    }
44
45    /// return an encoding for a given collation
46    pub fn encoding(&self) -> crate::Result<&'static Encoding> {
47        let res = if self.sort_id == 0 {
48            lcid_to_encoding(self.lcid())
49        } else {
50            sortid_to_encoding(self.sort_id)
51        };
52
53        res.ok_or_else(|| {
54            Error::Encoding(
55                format!(
56                    "encoding: unspported encoding (LCID: {:#04x}, sort ID: {})",
57                    self.lcid(),
58                    self.sort_id(),
59                )
60                .into(),
61            )
62        })
63    }
64}
65
66impl fmt::Display for Collation {
67    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
68        match self.encoding() {
69            Ok(encoding) => write!(f, "{}", encoding.name()),
70            _ => write!(f, "None"),
71        }
72    }
73}
74
75/// https://github.com/Microsoft/mssql-jdbc/blob/eb14f63077c47ef1fc1c690deb8cfab602baeb85/src/main/java/com/microsoft/sqlserver/jdbc/SQLCollation.java#L102-L310
76/// maps an LCID (it's locale part which is only 2 bytes) to a codepage
77///
78/// generate the code below from source code:
79/// 1. (regex)replace: (.*?)\((.*?),(.*?)\) with $2 => $3
80/// 2. replace: Encoding.CP(.*?) with encoding::all::WINDOWS_$1
81/// 3. replace: Encoding.UNICODE with encoding::all::UTF16_LE
82///
83/// the unimplemented!() one's are not supported by rust-encoding
84pub fn lcid_to_encoding(locale: u16) -> Option<&'static Encoding> {
85    match locale {
86        0x0401 => Some(encoding_rs::WINDOWS_1256),
87        0x0402 => Some(encoding_rs::WINDOWS_1251),
88        0x0403 => Some(encoding_rs::WINDOWS_1252),
89        // CP950
90        0x0404 | 0x0c04 | 0x1404 => Some(encoding_rs::BIG5),
91        0x0405 => Some(encoding_rs::WINDOWS_1250),
92        0x0406 => Some(encoding_rs::WINDOWS_1252),
93        0x0407 => Some(encoding_rs::WINDOWS_1252),
94        0x0408 => Some(encoding_rs::WINDOWS_1253),
95        0x0409 => Some(encoding_rs::WINDOWS_1252),
96        0x040a => Some(encoding_rs::WINDOWS_1252),
97        0x040b => Some(encoding_rs::WINDOWS_1252),
98        0x040c => Some(encoding_rs::WINDOWS_1252),
99        0x040d => Some(encoding_rs::WINDOWS_1255),
100        0x040e => Some(encoding_rs::WINDOWS_1250),
101        0x040f => Some(encoding_rs::WINDOWS_1252),
102        0x0410 => Some(encoding_rs::WINDOWS_1252),
103        // CP932
104        0x0411 => Some(encoding_rs::SHIFT_JIS),
105        0x0412 => Some(encoding_rs::EUC_KR),
106        0x0413 => Some(encoding_rs::WINDOWS_1252),
107        0x0414 => Some(encoding_rs::WINDOWS_1252),
108        0x0415 => Some(encoding_rs::WINDOWS_1250),
109        0x0416 => Some(encoding_rs::WINDOWS_1252),
110        0x0417 => Some(encoding_rs::WINDOWS_1252),
111        0x0418 => Some(encoding_rs::WINDOWS_1250),
112        0x0419 => Some(encoding_rs::WINDOWS_1251),
113        0x041a => Some(encoding_rs::WINDOWS_1250),
114        0x041b => Some(encoding_rs::WINDOWS_1250),
115        0x041c => Some(encoding_rs::WINDOWS_1250),
116        0x041d => Some(encoding_rs::WINDOWS_1252),
117        0x041e => Some(encoding_rs::WINDOWS_874),
118        0x041f => Some(encoding_rs::WINDOWS_1254),
119        0x0420 => Some(encoding_rs::WINDOWS_1256),
120        0x0421 => Some(encoding_rs::WINDOWS_1252),
121        0x0422 => Some(encoding_rs::WINDOWS_1251),
122        0x0423 => Some(encoding_rs::WINDOWS_1251),
123        0x0424 => Some(encoding_rs::WINDOWS_1250),
124        0x0425 => Some(encoding_rs::WINDOWS_1257),
125        0x0426 => Some(encoding_rs::WINDOWS_1257),
126        0x0427 => Some(encoding_rs::WINDOWS_1257),
127        0x0428 => Some(encoding_rs::WINDOWS_1251),
128        0x0429 => Some(encoding_rs::WINDOWS_1256),
129        0x042a => Some(encoding_rs::WINDOWS_1258),
130        0x042b => Some(encoding_rs::WINDOWS_1252),
131        0x042c => Some(encoding_rs::WINDOWS_1254),
132        0x042d => Some(encoding_rs::WINDOWS_1252),
133        0x042e => Some(encoding_rs::WINDOWS_1252),
134        0x042f => Some(encoding_rs::WINDOWS_1251),
135        0x0432 => Some(encoding_rs::WINDOWS_1252),
136        0x0434 => Some(encoding_rs::WINDOWS_1252),
137        0x0435 => Some(encoding_rs::WINDOWS_1252),
138        0x0436 => Some(encoding_rs::WINDOWS_1252),
139        0x0437 => Some(encoding_rs::WINDOWS_1252),
140        0x0438 => Some(encoding_rs::WINDOWS_1252),
141        0x0439 => Some(encoding_rs::UTF_16LE),
142        0x043a => Some(encoding_rs::UTF_16LE),
143        0x043b => Some(encoding_rs::WINDOWS_1252),
144        0x043e => Some(encoding_rs::WINDOWS_1252),
145        0x043f => Some(encoding_rs::WINDOWS_1251),
146        0x0440 => Some(encoding_rs::WINDOWS_1251),
147        0x0441 => Some(encoding_rs::WINDOWS_1252),
148        0x0442 => Some(encoding_rs::WINDOWS_1250),
149        0x0443 => Some(encoding_rs::WINDOWS_1254),
150        0x0444 => Some(encoding_rs::WINDOWS_1251),
151        0x0445 => Some(encoding_rs::UTF_16LE),
152        0x0446 => Some(encoding_rs::UTF_16LE),
153        0x0447 => Some(encoding_rs::UTF_16LE),
154        0x0448 => Some(encoding_rs::UTF_16LE),
155        0x0449 => Some(encoding_rs::UTF_16LE),
156        0x044a => Some(encoding_rs::UTF_16LE),
157        0x044b => Some(encoding_rs::UTF_16LE),
158        0x044c => Some(encoding_rs::UTF_16LE),
159        0x044d => Some(encoding_rs::UTF_16LE),
160        0x044e => Some(encoding_rs::UTF_16LE),
161        0x044f => Some(encoding_rs::UTF_16LE),
162        0x0450 => Some(encoding_rs::WINDOWS_1251),
163        0x0451 => Some(encoding_rs::UTF_16LE),
164        0x0452 => Some(encoding_rs::WINDOWS_1252),
165        0x0453 => Some(encoding_rs::UTF_16LE),
166        0x0454 => Some(encoding_rs::UTF_16LE),
167        0x0456 => Some(encoding_rs::WINDOWS_1252),
168        0x0457 => Some(encoding_rs::UTF_16LE),
169        0x045a => Some(encoding_rs::UTF_16LE),
170        0x045b => Some(encoding_rs::UTF_16LE),
171        0x045d => Some(encoding_rs::WINDOWS_1252),
172        0x045e => Some(encoding_rs::WINDOWS_1252),
173        0x0461 => Some(encoding_rs::UTF_16LE),
174        0x0462 => Some(encoding_rs::WINDOWS_1252),
175        0x0463 => Some(encoding_rs::UTF_16LE),
176        0x0464 => Some(encoding_rs::WINDOWS_1252),
177        0x0465 => Some(encoding_rs::UTF_16LE),
178        0x0468 => Some(encoding_rs::WINDOWS_1252),
179        0x046a => Some(encoding_rs::WINDOWS_1252),
180        0x046b => Some(encoding_rs::WINDOWS_1252),
181        0x046c => Some(encoding_rs::WINDOWS_1252),
182        0x046d => Some(encoding_rs::WINDOWS_1251),
183        0x046e => Some(encoding_rs::WINDOWS_1252),
184        0x046f => Some(encoding_rs::WINDOWS_1252),
185        0x0470 => Some(encoding_rs::WINDOWS_1252),
186        0x0478 => Some(encoding_rs::WINDOWS_1252),
187        0x047a => Some(encoding_rs::WINDOWS_1252),
188        0x047c => Some(encoding_rs::WINDOWS_1252),
189        0x047e => Some(encoding_rs::WINDOWS_1252),
190        0x0480 => Some(encoding_rs::WINDOWS_1256),
191        0x0481 => Some(encoding_rs::UTF_16LE),
192        0x0482 => Some(encoding_rs::WINDOWS_1252),
193        0x0483 => Some(encoding_rs::WINDOWS_1252),
194        0x0484 => Some(encoding_rs::WINDOWS_1252),
195        0x0485 => Some(encoding_rs::WINDOWS_1251),
196        0x0486 => Some(encoding_rs::WINDOWS_1252),
197        0x0487 => Some(encoding_rs::WINDOWS_1252),
198        0x0488 => Some(encoding_rs::WINDOWS_1252),
199        0x048c => Some(encoding_rs::WINDOWS_1256),
200        0x0801 => Some(encoding_rs::WINDOWS_1256),
201        // CP936
202        0x0804 | 0x1004 => Some(encoding_rs::GB18030),
203        0x0807 => Some(encoding_rs::WINDOWS_1252),
204        0x0809 => Some(encoding_rs::WINDOWS_1252),
205        0x080a => Some(encoding_rs::WINDOWS_1252),
206        0x080c => Some(encoding_rs::WINDOWS_1252),
207        0x0810 => Some(encoding_rs::WINDOWS_1252),
208        0x0813 => Some(encoding_rs::WINDOWS_1252),
209        0x0814 => Some(encoding_rs::WINDOWS_1252),
210        0x0816 => Some(encoding_rs::WINDOWS_1252),
211        0x081a => Some(encoding_rs::WINDOWS_1250),
212        0x081d => Some(encoding_rs::WINDOWS_1252),
213        0x0827 => Some(encoding_rs::WINDOWS_1257),
214        0x082c => Some(encoding_rs::WINDOWS_1251),
215        0x082e => Some(encoding_rs::WINDOWS_1252),
216        0x083b => Some(encoding_rs::WINDOWS_1252),
217        0x083c => Some(encoding_rs::WINDOWS_1252),
218        0x083e => Some(encoding_rs::WINDOWS_1252),
219        0x0843 => Some(encoding_rs::WINDOWS_1251),
220        0x0845 => Some(encoding_rs::UTF_16LE),
221        0x0850 => Some(encoding_rs::WINDOWS_1251),
222        0x085d => Some(encoding_rs::WINDOWS_1252),
223        0x085f => Some(encoding_rs::WINDOWS_1252),
224        0x086b => Some(encoding_rs::WINDOWS_1252),
225        0x0c01 => Some(encoding_rs::WINDOWS_1256),
226        0x0c07 => Some(encoding_rs::WINDOWS_1252),
227        0x0c09 => Some(encoding_rs::WINDOWS_1252),
228        0x0c0a => Some(encoding_rs::WINDOWS_1252),
229        0x0c0c => Some(encoding_rs::WINDOWS_1252),
230        0x0c1a => Some(encoding_rs::WINDOWS_1251),
231        0x0c3b => Some(encoding_rs::WINDOWS_1252),
232        0x0c6b => Some(encoding_rs::WINDOWS_1252),
233        0x1001 => Some(encoding_rs::WINDOWS_1256),
234        0x1007 => Some(encoding_rs::WINDOWS_1252),
235        0x1009 => Some(encoding_rs::WINDOWS_1252),
236        0x100a => Some(encoding_rs::WINDOWS_1252),
237        0x100c => Some(encoding_rs::WINDOWS_1252),
238        0x101a => Some(encoding_rs::WINDOWS_1250),
239        0x103b => Some(encoding_rs::WINDOWS_1252),
240        0x1401 => Some(encoding_rs::WINDOWS_1256),
241        0x1407 => Some(encoding_rs::WINDOWS_1252),
242        0x1409 => Some(encoding_rs::WINDOWS_1252),
243        0x140a => Some(encoding_rs::WINDOWS_1252),
244        0x140c => Some(encoding_rs::WINDOWS_1252),
245        0x141a => Some(encoding_rs::WINDOWS_1250),
246        0x143b => Some(encoding_rs::WINDOWS_1252),
247        0x1801 => Some(encoding_rs::WINDOWS_1256),
248        0x1809 => Some(encoding_rs::WINDOWS_1252),
249        0x180a => Some(encoding_rs::WINDOWS_1252),
250        0x180c => Some(encoding_rs::WINDOWS_1252),
251        0x181a => Some(encoding_rs::WINDOWS_1250),
252        0x183b => Some(encoding_rs::WINDOWS_1252),
253        0x1c01 => Some(encoding_rs::WINDOWS_1256),
254        0x1c09 => Some(encoding_rs::WINDOWS_1252),
255        0x1c0a => Some(encoding_rs::WINDOWS_1252),
256        0x1c1a => Some(encoding_rs::WINDOWS_1251),
257        0x1c3b => Some(encoding_rs::WINDOWS_1252),
258        0x2001 => Some(encoding_rs::WINDOWS_1256),
259        0x2009 => Some(encoding_rs::WINDOWS_1252),
260        0x200a => Some(encoding_rs::WINDOWS_1252),
261        0x201a => Some(encoding_rs::WINDOWS_1251),
262        0x203b => Some(encoding_rs::WINDOWS_1252),
263        0x2401 => Some(encoding_rs::WINDOWS_1256),
264        0x2409 => Some(encoding_rs::WINDOWS_1252),
265        0x240a => Some(encoding_rs::WINDOWS_1252),
266        0x243b => Some(encoding_rs::WINDOWS_1252),
267        0x2801 => Some(encoding_rs::WINDOWS_1256),
268        0x2809 => Some(encoding_rs::WINDOWS_1252),
269        0x280a => Some(encoding_rs::WINDOWS_1252),
270        0x2c01 => Some(encoding_rs::WINDOWS_1256),
271        0x2c09 => Some(encoding_rs::WINDOWS_1252),
272        0x2c0a => Some(encoding_rs::WINDOWS_1252),
273        0x3001 => Some(encoding_rs::WINDOWS_1256),
274        0x3009 => Some(encoding_rs::WINDOWS_1252),
275        0x300a => Some(encoding_rs::WINDOWS_1252),
276        0x3401 => Some(encoding_rs::WINDOWS_1256),
277        0x3409 => Some(encoding_rs::WINDOWS_1252),
278        0x340a => Some(encoding_rs::WINDOWS_1252),
279        0x3801 => Some(encoding_rs::WINDOWS_1256),
280        0x380a => Some(encoding_rs::WINDOWS_1252),
281        0x3c01 => Some(encoding_rs::WINDOWS_1256),
282        0x3c0a => Some(encoding_rs::WINDOWS_1252),
283        0x4001 => Some(encoding_rs::WINDOWS_1256),
284        0x4009 => Some(encoding_rs::WINDOWS_1252),
285        0x400a => Some(encoding_rs::WINDOWS_1252),
286        0x4409 => Some(encoding_rs::WINDOWS_1252),
287        0x440a => Some(encoding_rs::WINDOWS_1252),
288        0x4809 => Some(encoding_rs::WINDOWS_1252),
289        0x480a => Some(encoding_rs::WINDOWS_1252),
290        0x4c0a => Some(encoding_rs::WINDOWS_1252),
291        0x500a => Some(encoding_rs::WINDOWS_1252),
292        0x540a => Some(encoding_rs::WINDOWS_1252),
293        _ => None,
294    }
295}
296
297/// [1] https://github.com/Microsoft/mssql-jdbc/blob/eb14f63077c47ef1fc1c690deb8cfab602baeb85/src/main/java/com/microsoft/sqlserver/jdbc/SQLCollation.java#L362-L482
298/// [2] https://msdn.microsoft.com/de-de/library/ms144250(v=sql.105).aspx
299///
300/// [2] does only contain 3/4 of the content [1] contains, so the source code is again the better source of information
301///
302/// generate the code below from source code:
303/// 1. (regex)replace .*\((.*?),.*?,(.*?)\) with $1 => $2
304/// 2. see above/as above
305pub fn sortid_to_encoding(sort_id: u8) -> Option<&'static Encoding> {
306    match sort_id {
307        // 30 | 31 | 32 | 33 | 34 | 35 => Some(encoding_rs::WINDOWS_437),
308        // 40 | 41 | 42 | 43 | 44 | 45 | 49 => Some(encoding_rs::WINDOWS_850),
309        50 => Some(encoding_rs::WINDOWS_1252),
310        51 => Some(encoding_rs::WINDOWS_1252),
311        52 => Some(encoding_rs::WINDOWS_1252),
312        53 => Some(encoding_rs::WINDOWS_1252),
313        54 => Some(encoding_rs::WINDOWS_1252),
314        // 55 | 56 | 57 | 58 | 59 | 60 | 61 => Some(encoding_rs::WINDOWS_850),
315        71 => Some(encoding_rs::WINDOWS_1252),
316        72 => Some(encoding_rs::WINDOWS_1252),
317        73 => Some(encoding_rs::WINDOWS_1252),
318        74 => Some(encoding_rs::WINDOWS_1252),
319        75 => Some(encoding_rs::WINDOWS_1252),
320        80 => Some(encoding_rs::WINDOWS_1250),
321        81 => Some(encoding_rs::WINDOWS_1250),
322        82 => Some(encoding_rs::WINDOWS_1250),
323        83 => Some(encoding_rs::WINDOWS_1250),
324        84 => Some(encoding_rs::WINDOWS_1250),
325        85 => Some(encoding_rs::WINDOWS_1250),
326        86 => Some(encoding_rs::WINDOWS_1250),
327        87 => Some(encoding_rs::WINDOWS_1250),
328        88 => Some(encoding_rs::WINDOWS_1250),
329        89 => Some(encoding_rs::WINDOWS_1250),
330        90 => Some(encoding_rs::WINDOWS_1250),
331        91 => Some(encoding_rs::WINDOWS_1250),
332        92 => Some(encoding_rs::WINDOWS_1250),
333        93 => Some(encoding_rs::WINDOWS_1250),
334        94 => Some(encoding_rs::WINDOWS_1250),
335        95 => Some(encoding_rs::WINDOWS_1250),
336        96 => Some(encoding_rs::WINDOWS_1250),
337        97 => Some(encoding_rs::WINDOWS_1250),
338        98 => Some(encoding_rs::WINDOWS_1250),
339        104 => Some(encoding_rs::WINDOWS_1251),
340        105 => Some(encoding_rs::WINDOWS_1251),
341        106 => Some(encoding_rs::WINDOWS_1251),
342        107 => Some(encoding_rs::WINDOWS_1251),
343        108 => Some(encoding_rs::WINDOWS_1251),
344        112 => Some(encoding_rs::WINDOWS_1253),
345        113 => Some(encoding_rs::WINDOWS_1253),
346        114 => Some(encoding_rs::WINDOWS_1253),
347        120 => Some(encoding_rs::WINDOWS_1253),
348        121 => Some(encoding_rs::WINDOWS_1253),
349        122 => Some(encoding_rs::WINDOWS_1253),
350        124 => Some(encoding_rs::WINDOWS_1253),
351        128 => Some(encoding_rs::WINDOWS_1254),
352        129 => Some(encoding_rs::WINDOWS_1254),
353        130 => Some(encoding_rs::WINDOWS_1254),
354        136 => Some(encoding_rs::WINDOWS_1255),
355        137 => Some(encoding_rs::WINDOWS_1255),
356        138 => Some(encoding_rs::WINDOWS_1255),
357        144 => Some(encoding_rs::WINDOWS_1256),
358        145 => Some(encoding_rs::WINDOWS_1256),
359        146 => Some(encoding_rs::WINDOWS_1256),
360        152 => Some(encoding_rs::WINDOWS_1257),
361        153 => Some(encoding_rs::WINDOWS_1257),
362        154 => Some(encoding_rs::WINDOWS_1257),
363        155 => Some(encoding_rs::WINDOWS_1257),
364        156 => Some(encoding_rs::WINDOWS_1257),
365        157 => Some(encoding_rs::WINDOWS_1257),
366        158 => Some(encoding_rs::WINDOWS_1257),
367        159 => Some(encoding_rs::WINDOWS_1257),
368        160 => Some(encoding_rs::WINDOWS_1257),
369        183 => Some(encoding_rs::WINDOWS_1252),
370        184 => Some(encoding_rs::WINDOWS_1252),
371        185 => Some(encoding_rs::WINDOWS_1252),
372        186 => Some(encoding_rs::WINDOWS_1252),
373        // CP 932
374        192 | 193 | 200 => Some(encoding_rs::SHIFT_JIS),
375        194 => Some(encoding_rs::EUC_KR),
376        195 => Some(encoding_rs::EUC_KR),
377        // CP950
378        196 | 197 | 202 => Some(encoding_rs::BIG5),
379        // CP936 (GB18030 is an extension of it with more chars), should be backwards-compatible)
380        198 | 199 | 203 => Some(encoding_rs::GB18030),
381        201 => Some(encoding_rs::BIG5),
382        204 => Some(encoding_rs::WINDOWS_874),
383        205 => Some(encoding_rs::WINDOWS_874),
384        206 => Some(encoding_rs::WINDOWS_874),
385        210 => Some(encoding_rs::WINDOWS_1252),
386        211 => Some(encoding_rs::WINDOWS_1252),
387        212 => Some(encoding_rs::WINDOWS_1252),
388        213 => Some(encoding_rs::WINDOWS_1252),
389        214 => Some(encoding_rs::WINDOWS_1252),
390        215 => Some(encoding_rs::WINDOWS_1252),
391        216 => Some(encoding_rs::WINDOWS_1252),
392        217 => Some(encoding_rs::WINDOWS_1252),
393        _ => None,
394    }
395}
396
397#[cfg(test)]
398mod tests {
399    use super::*;
400
401    #[test]
402    fn accessors_split_info_and_sort_id() {
403        // info holds LCID in the low 16 bits plus flags/version in the high bits.
404        let collation = Collation::new(0x0020_0409, 52);
405        assert_eq!(collation.info(), 0x0020_0409);
406        assert_eq!(collation.lcid(), 0x0409);
407        assert_eq!(collation.sort_id(), 52);
408    }
409
410    #[test]
411    fn encoding_from_lcid_when_sort_id_zero() {
412        // sort_id == 0 -> resolve via the LCID.
413        let collation = Collation::new(0x0409, 0);
414        let encoding = collation.encoding().expect("known LCID must resolve");
415        assert_eq!(encoding, encoding_rs::WINDOWS_1252);
416    }
417
418    #[test]
419    fn encoding_from_sort_id_when_present() {
420        // A non-zero sort_id takes precedence over the LCID.
421        let collation = Collation::new(0x0405, 80);
422        let encoding = collation.encoding().expect("known sort id must resolve");
423        assert_eq!(encoding, encoding_rs::WINDOWS_1250);
424    }
425
426    #[test]
427    fn encoding_unknown_lcid_errors() {
428        let collation = Collation::new(0xFFFF, 0);
429        let err = collation.encoding().unwrap_err();
430        assert!(matches!(err, Error::Encoding(_)));
431    }
432
433    #[test]
434    fn encoding_unknown_sort_id_errors() {
435        let collation = Collation::new(0x0409, 250);
436        assert!(collation.encoding().is_err());
437    }
438
439    #[test]
440    fn display_uses_encoding_name() {
441        let collation = Collation::new(0x0409, 0);
442        assert_eq!(format!("{}", collation), encoding_rs::WINDOWS_1252.name());
443    }
444
445    #[test]
446    fn display_falls_back_to_none_on_unknown() {
447        let collation = Collation::new(0xFFFF, 0);
448        assert_eq!(format!("{}", collation), "None");
449    }
450
451    #[test]
452    fn lcid_to_encoding_known_and_unknown() {
453        assert_eq!(lcid_to_encoding(0x0401), Some(encoding_rs::WINDOWS_1256));
454        assert_eq!(lcid_to_encoding(0x0404), Some(encoding_rs::BIG5));
455        assert_eq!(lcid_to_encoding(0x0411), Some(encoding_rs::SHIFT_JIS));
456        assert_eq!(lcid_to_encoding(0x0412), Some(encoding_rs::EUC_KR));
457        assert_eq!(lcid_to_encoding(0x0804), Some(encoding_rs::GB18030));
458        assert_eq!(lcid_to_encoding(0x0439), Some(encoding_rs::UTF_16LE));
459        assert_eq!(lcid_to_encoding(0x041e), Some(encoding_rs::WINDOWS_874));
460        assert_eq!(lcid_to_encoding(0x0000), None);
461    }
462
463    #[test]
464    fn sortid_to_encoding_known_and_unknown() {
465        assert_eq!(sortid_to_encoding(50), Some(encoding_rs::WINDOWS_1252));
466        assert_eq!(sortid_to_encoding(80), Some(encoding_rs::WINDOWS_1250));
467        assert_eq!(sortid_to_encoding(104), Some(encoding_rs::WINDOWS_1251));
468        assert_eq!(sortid_to_encoding(112), Some(encoding_rs::WINDOWS_1253));
469        assert_eq!(sortid_to_encoding(192), Some(encoding_rs::SHIFT_JIS));
470        assert_eq!(sortid_to_encoding(194), Some(encoding_rs::EUC_KR));
471        assert_eq!(sortid_to_encoding(196), Some(encoding_rs::BIG5));
472        assert_eq!(sortid_to_encoding(198), Some(encoding_rs::GB18030));
473        assert_eq!(sortid_to_encoding(204), Some(encoding_rs::WINDOWS_874));
474        assert_eq!(sortid_to_encoding(0), None);
475        assert_eq!(sortid_to_encoding(255), None);
476    }
477
478    #[test]
479    fn lcid_to_encoding_covers_all_documented_locales() {
480        let cases: &[(u16, &encoding_rs::Encoding)] = &[
481            (0x0401, encoding_rs::WINDOWS_1256),
482            (0x0402, encoding_rs::WINDOWS_1251),
483            (0x0403, encoding_rs::WINDOWS_1252),
484            (0x0404, encoding_rs::BIG5),
485            (0x0c04, encoding_rs::BIG5),
486            (0x1404, encoding_rs::BIG5),
487            (0x0405, encoding_rs::WINDOWS_1250),
488            (0x0406, encoding_rs::WINDOWS_1252),
489            (0x0407, encoding_rs::WINDOWS_1252),
490            (0x0408, encoding_rs::WINDOWS_1253),
491            (0x0409, encoding_rs::WINDOWS_1252),
492            (0x040a, encoding_rs::WINDOWS_1252),
493            (0x040b, encoding_rs::WINDOWS_1252),
494            (0x040c, encoding_rs::WINDOWS_1252),
495            (0x040d, encoding_rs::WINDOWS_1255),
496            (0x040e, encoding_rs::WINDOWS_1250),
497            (0x040f, encoding_rs::WINDOWS_1252),
498            (0x0410, encoding_rs::WINDOWS_1252),
499            (0x0411, encoding_rs::SHIFT_JIS),
500            (0x0412, encoding_rs::EUC_KR),
501            (0x0413, encoding_rs::WINDOWS_1252),
502            (0x0414, encoding_rs::WINDOWS_1252),
503            (0x0415, encoding_rs::WINDOWS_1250),
504            (0x0416, encoding_rs::WINDOWS_1252),
505            (0x0417, encoding_rs::WINDOWS_1252),
506            (0x0418, encoding_rs::WINDOWS_1250),
507            (0x0419, encoding_rs::WINDOWS_1251),
508            (0x041a, encoding_rs::WINDOWS_1250),
509            (0x041b, encoding_rs::WINDOWS_1250),
510            (0x041c, encoding_rs::WINDOWS_1250),
511            (0x041d, encoding_rs::WINDOWS_1252),
512            (0x041e, encoding_rs::WINDOWS_874),
513            (0x041f, encoding_rs::WINDOWS_1254),
514            (0x0420, encoding_rs::WINDOWS_1256),
515            (0x0421, encoding_rs::WINDOWS_1252),
516            (0x0422, encoding_rs::WINDOWS_1251),
517            (0x0423, encoding_rs::WINDOWS_1251),
518            (0x0424, encoding_rs::WINDOWS_1250),
519            (0x0425, encoding_rs::WINDOWS_1257),
520            (0x0426, encoding_rs::WINDOWS_1257),
521            (0x0427, encoding_rs::WINDOWS_1257),
522            (0x0428, encoding_rs::WINDOWS_1251),
523            (0x0429, encoding_rs::WINDOWS_1256),
524            (0x042a, encoding_rs::WINDOWS_1258),
525            (0x042b, encoding_rs::WINDOWS_1252),
526            (0x042c, encoding_rs::WINDOWS_1254),
527            (0x042d, encoding_rs::WINDOWS_1252),
528            (0x042e, encoding_rs::WINDOWS_1252),
529            (0x042f, encoding_rs::WINDOWS_1251),
530            (0x0432, encoding_rs::WINDOWS_1252),
531            (0x0434, encoding_rs::WINDOWS_1252),
532            (0x0435, encoding_rs::WINDOWS_1252),
533            (0x0436, encoding_rs::WINDOWS_1252),
534            (0x0437, encoding_rs::WINDOWS_1252),
535            (0x0438, encoding_rs::WINDOWS_1252),
536            (0x0439, encoding_rs::UTF_16LE),
537            (0x043a, encoding_rs::UTF_16LE),
538            (0x043b, encoding_rs::WINDOWS_1252),
539            (0x043e, encoding_rs::WINDOWS_1252),
540            (0x043f, encoding_rs::WINDOWS_1251),
541            (0x0440, encoding_rs::WINDOWS_1251),
542            (0x0441, encoding_rs::WINDOWS_1252),
543            (0x0442, encoding_rs::WINDOWS_1250),
544            (0x0443, encoding_rs::WINDOWS_1254),
545            (0x0444, encoding_rs::WINDOWS_1251),
546            (0x0445, encoding_rs::UTF_16LE),
547            (0x0446, encoding_rs::UTF_16LE),
548            (0x0447, encoding_rs::UTF_16LE),
549            (0x0448, encoding_rs::UTF_16LE),
550            (0x0449, encoding_rs::UTF_16LE),
551            (0x044a, encoding_rs::UTF_16LE),
552            (0x044b, encoding_rs::UTF_16LE),
553            (0x044c, encoding_rs::UTF_16LE),
554            (0x044d, encoding_rs::UTF_16LE),
555            (0x044e, encoding_rs::UTF_16LE),
556            (0x044f, encoding_rs::UTF_16LE),
557            (0x0450, encoding_rs::WINDOWS_1251),
558            (0x0451, encoding_rs::UTF_16LE),
559            (0x0452, encoding_rs::WINDOWS_1252),
560            (0x0453, encoding_rs::UTF_16LE),
561            (0x0454, encoding_rs::UTF_16LE),
562            (0x0456, encoding_rs::WINDOWS_1252),
563            (0x0457, encoding_rs::UTF_16LE),
564            (0x045a, encoding_rs::UTF_16LE),
565            (0x045b, encoding_rs::UTF_16LE),
566            (0x045d, encoding_rs::WINDOWS_1252),
567            (0x045e, encoding_rs::WINDOWS_1252),
568            (0x0461, encoding_rs::UTF_16LE),
569            (0x0462, encoding_rs::WINDOWS_1252),
570            (0x0463, encoding_rs::UTF_16LE),
571            (0x0464, encoding_rs::WINDOWS_1252),
572            (0x0465, encoding_rs::UTF_16LE),
573            (0x0468, encoding_rs::WINDOWS_1252),
574            (0x046a, encoding_rs::WINDOWS_1252),
575            (0x046b, encoding_rs::WINDOWS_1252),
576            (0x046c, encoding_rs::WINDOWS_1252),
577            (0x046d, encoding_rs::WINDOWS_1251),
578            (0x046e, encoding_rs::WINDOWS_1252),
579            (0x046f, encoding_rs::WINDOWS_1252),
580            (0x0470, encoding_rs::WINDOWS_1252),
581            (0x0478, encoding_rs::WINDOWS_1252),
582            (0x047a, encoding_rs::WINDOWS_1252),
583            (0x047c, encoding_rs::WINDOWS_1252),
584            (0x047e, encoding_rs::WINDOWS_1252),
585            (0x0480, encoding_rs::WINDOWS_1256),
586            (0x0481, encoding_rs::UTF_16LE),
587            (0x0482, encoding_rs::WINDOWS_1252),
588            (0x0483, encoding_rs::WINDOWS_1252),
589            (0x0484, encoding_rs::WINDOWS_1252),
590            (0x0485, encoding_rs::WINDOWS_1251),
591            (0x0486, encoding_rs::WINDOWS_1252),
592            (0x0487, encoding_rs::WINDOWS_1252),
593            (0x0488, encoding_rs::WINDOWS_1252),
594            (0x048c, encoding_rs::WINDOWS_1256),
595            (0x0801, encoding_rs::WINDOWS_1256),
596            (0x0804, encoding_rs::GB18030),
597            (0x1004, encoding_rs::GB18030),
598            (0x0807, encoding_rs::WINDOWS_1252),
599            (0x0809, encoding_rs::WINDOWS_1252),
600            (0x080a, encoding_rs::WINDOWS_1252),
601            (0x080c, encoding_rs::WINDOWS_1252),
602            (0x0810, encoding_rs::WINDOWS_1252),
603            (0x0813, encoding_rs::WINDOWS_1252),
604            (0x0814, encoding_rs::WINDOWS_1252),
605            (0x0816, encoding_rs::WINDOWS_1252),
606            (0x081a, encoding_rs::WINDOWS_1250),
607            (0x081d, encoding_rs::WINDOWS_1252),
608            (0x0827, encoding_rs::WINDOWS_1257),
609            (0x082c, encoding_rs::WINDOWS_1251),
610            (0x082e, encoding_rs::WINDOWS_1252),
611            (0x083b, encoding_rs::WINDOWS_1252),
612            (0x083c, encoding_rs::WINDOWS_1252),
613            (0x083e, encoding_rs::WINDOWS_1252),
614            (0x0843, encoding_rs::WINDOWS_1251),
615            (0x0845, encoding_rs::UTF_16LE),
616            (0x0850, encoding_rs::WINDOWS_1251),
617            (0x085d, encoding_rs::WINDOWS_1252),
618            (0x085f, encoding_rs::WINDOWS_1252),
619            (0x086b, encoding_rs::WINDOWS_1252),
620            (0x0c01, encoding_rs::WINDOWS_1256),
621            (0x0c07, encoding_rs::WINDOWS_1252),
622            (0x0c09, encoding_rs::WINDOWS_1252),
623            (0x0c0a, encoding_rs::WINDOWS_1252),
624            (0x0c0c, encoding_rs::WINDOWS_1252),
625            (0x0c1a, encoding_rs::WINDOWS_1251),
626            (0x0c3b, encoding_rs::WINDOWS_1252),
627            (0x0c6b, encoding_rs::WINDOWS_1252),
628            (0x1001, encoding_rs::WINDOWS_1256),
629            (0x1007, encoding_rs::WINDOWS_1252),
630            (0x1009, encoding_rs::WINDOWS_1252),
631            (0x100a, encoding_rs::WINDOWS_1252),
632            (0x100c, encoding_rs::WINDOWS_1252),
633            (0x101a, encoding_rs::WINDOWS_1250),
634            (0x103b, encoding_rs::WINDOWS_1252),
635            (0x1401, encoding_rs::WINDOWS_1256),
636            (0x1407, encoding_rs::WINDOWS_1252),
637            (0x1409, encoding_rs::WINDOWS_1252),
638            (0x140a, encoding_rs::WINDOWS_1252),
639            (0x140c, encoding_rs::WINDOWS_1252),
640            (0x141a, encoding_rs::WINDOWS_1250),
641            (0x143b, encoding_rs::WINDOWS_1252),
642            (0x1801, encoding_rs::WINDOWS_1256),
643            (0x1809, encoding_rs::WINDOWS_1252),
644            (0x180a, encoding_rs::WINDOWS_1252),
645            (0x180c, encoding_rs::WINDOWS_1252),
646            (0x181a, encoding_rs::WINDOWS_1250),
647            (0x183b, encoding_rs::WINDOWS_1252),
648            (0x1c01, encoding_rs::WINDOWS_1256),
649            (0x1c09, encoding_rs::WINDOWS_1252),
650            (0x1c0a, encoding_rs::WINDOWS_1252),
651            (0x1c1a, encoding_rs::WINDOWS_1251),
652            (0x1c3b, encoding_rs::WINDOWS_1252),
653            (0x2001, encoding_rs::WINDOWS_1256),
654            (0x2009, encoding_rs::WINDOWS_1252),
655            (0x200a, encoding_rs::WINDOWS_1252),
656            (0x201a, encoding_rs::WINDOWS_1251),
657            (0x203b, encoding_rs::WINDOWS_1252),
658            (0x2401, encoding_rs::WINDOWS_1256),
659            (0x2409, encoding_rs::WINDOWS_1252),
660            (0x240a, encoding_rs::WINDOWS_1252),
661            (0x243b, encoding_rs::WINDOWS_1252),
662            (0x2801, encoding_rs::WINDOWS_1256),
663            (0x2809, encoding_rs::WINDOWS_1252),
664            (0x280a, encoding_rs::WINDOWS_1252),
665            (0x2c01, encoding_rs::WINDOWS_1256),
666            (0x2c09, encoding_rs::WINDOWS_1252),
667            (0x2c0a, encoding_rs::WINDOWS_1252),
668            (0x3001, encoding_rs::WINDOWS_1256),
669            (0x3009, encoding_rs::WINDOWS_1252),
670            (0x300a, encoding_rs::WINDOWS_1252),
671            (0x3401, encoding_rs::WINDOWS_1256),
672            (0x3409, encoding_rs::WINDOWS_1252),
673            (0x340a, encoding_rs::WINDOWS_1252),
674            (0x3801, encoding_rs::WINDOWS_1256),
675            (0x380a, encoding_rs::WINDOWS_1252),
676            (0x3c01, encoding_rs::WINDOWS_1256),
677            (0x3c0a, encoding_rs::WINDOWS_1252),
678            (0x4001, encoding_rs::WINDOWS_1256),
679            (0x4009, encoding_rs::WINDOWS_1252),
680            (0x400a, encoding_rs::WINDOWS_1252),
681            (0x4409, encoding_rs::WINDOWS_1252),
682            (0x440a, encoding_rs::WINDOWS_1252),
683            (0x4809, encoding_rs::WINDOWS_1252),
684            (0x480a, encoding_rs::WINDOWS_1252),
685            (0x4c0a, encoding_rs::WINDOWS_1252),
686            (0x500a, encoding_rs::WINDOWS_1252),
687            (0x540a, encoding_rs::WINDOWS_1252),
688        ];
689
690        for (locale, expected) in cases {
691            assert_eq!(
692                lcid_to_encoding(*locale),
693                Some(*expected),
694                "locale {:#06x}",
695                locale
696            );
697        }
698    }
699
700    #[test]
701    fn sortid_to_encoding_covers_all_documented_sort_ids() {
702        let cases: &[(u8, &encoding_rs::Encoding)] = &[
703            (50, encoding_rs::WINDOWS_1252),
704            (51, encoding_rs::WINDOWS_1252),
705            (52, encoding_rs::WINDOWS_1252),
706            (53, encoding_rs::WINDOWS_1252),
707            (54, encoding_rs::WINDOWS_1252),
708            (71, encoding_rs::WINDOWS_1252),
709            (72, encoding_rs::WINDOWS_1252),
710            (73, encoding_rs::WINDOWS_1252),
711            (74, encoding_rs::WINDOWS_1252),
712            (75, encoding_rs::WINDOWS_1252),
713            (80, encoding_rs::WINDOWS_1250),
714            (81, encoding_rs::WINDOWS_1250),
715            (82, encoding_rs::WINDOWS_1250),
716            (83, encoding_rs::WINDOWS_1250),
717            (84, encoding_rs::WINDOWS_1250),
718            (85, encoding_rs::WINDOWS_1250),
719            (86, encoding_rs::WINDOWS_1250),
720            (87, encoding_rs::WINDOWS_1250),
721            (88, encoding_rs::WINDOWS_1250),
722            (89, encoding_rs::WINDOWS_1250),
723            (90, encoding_rs::WINDOWS_1250),
724            (91, encoding_rs::WINDOWS_1250),
725            (92, encoding_rs::WINDOWS_1250),
726            (93, encoding_rs::WINDOWS_1250),
727            (94, encoding_rs::WINDOWS_1250),
728            (95, encoding_rs::WINDOWS_1250),
729            (96, encoding_rs::WINDOWS_1250),
730            (97, encoding_rs::WINDOWS_1250),
731            (98, encoding_rs::WINDOWS_1250),
732            (104, encoding_rs::WINDOWS_1251),
733            (105, encoding_rs::WINDOWS_1251),
734            (106, encoding_rs::WINDOWS_1251),
735            (107, encoding_rs::WINDOWS_1251),
736            (108, encoding_rs::WINDOWS_1251),
737            (112, encoding_rs::WINDOWS_1253),
738            (113, encoding_rs::WINDOWS_1253),
739            (114, encoding_rs::WINDOWS_1253),
740            (120, encoding_rs::WINDOWS_1253),
741            (121, encoding_rs::WINDOWS_1253),
742            (122, encoding_rs::WINDOWS_1253),
743            (124, encoding_rs::WINDOWS_1253),
744            (128, encoding_rs::WINDOWS_1254),
745            (129, encoding_rs::WINDOWS_1254),
746            (130, encoding_rs::WINDOWS_1254),
747            (136, encoding_rs::WINDOWS_1255),
748            (137, encoding_rs::WINDOWS_1255),
749            (138, encoding_rs::WINDOWS_1255),
750            (144, encoding_rs::WINDOWS_1256),
751            (145, encoding_rs::WINDOWS_1256),
752            (146, encoding_rs::WINDOWS_1256),
753            (152, encoding_rs::WINDOWS_1257),
754            (153, encoding_rs::WINDOWS_1257),
755            (154, encoding_rs::WINDOWS_1257),
756            (155, encoding_rs::WINDOWS_1257),
757            (156, encoding_rs::WINDOWS_1257),
758            (157, encoding_rs::WINDOWS_1257),
759            (158, encoding_rs::WINDOWS_1257),
760            (159, encoding_rs::WINDOWS_1257),
761            (160, encoding_rs::WINDOWS_1257),
762            (183, encoding_rs::WINDOWS_1252),
763            (184, encoding_rs::WINDOWS_1252),
764            (185, encoding_rs::WINDOWS_1252),
765            (186, encoding_rs::WINDOWS_1252),
766            (192, encoding_rs::SHIFT_JIS),
767            (193, encoding_rs::SHIFT_JIS),
768            (200, encoding_rs::SHIFT_JIS),
769            (194, encoding_rs::EUC_KR),
770            (195, encoding_rs::EUC_KR),
771            (196, encoding_rs::BIG5),
772            (197, encoding_rs::BIG5),
773            (202, encoding_rs::BIG5),
774            (198, encoding_rs::GB18030),
775            (199, encoding_rs::GB18030),
776            (203, encoding_rs::GB18030),
777            (201, encoding_rs::BIG5),
778            (204, encoding_rs::WINDOWS_874),
779            (205, encoding_rs::WINDOWS_874),
780            (206, encoding_rs::WINDOWS_874),
781            (210, encoding_rs::WINDOWS_1252),
782            (211, encoding_rs::WINDOWS_1252),
783            (212, encoding_rs::WINDOWS_1252),
784            (213, encoding_rs::WINDOWS_1252),
785            (214, encoding_rs::WINDOWS_1252),
786            (215, encoding_rs::WINDOWS_1252),
787            (216, encoding_rs::WINDOWS_1252),
788            (217, encoding_rs::WINDOWS_1252),
789        ];
790
791        for (sort_id, expected) in cases {
792            assert_eq!(
793                sortid_to_encoding(*sort_id),
794                Some(*expected),
795                "sort_id {}",
796                sort_id
797            );
798        }
799    }
800
801    #[test]
802    fn derives_eq_and_copy() {
803        let a = Collation::new(0x0409, 0);
804        let b = a;
805        assert_eq!(a, b);
806        assert_ne!(a, Collation::new(0x0409, 1));
807    }
808}