db-collation 0.2.0

Offline, exact comparison of strings using an explicitly identified database collation and version.
Documentation
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
// Copyright 2026 db-collation contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! The closed set of collations this crate reproduces exactly.
//!
//! A [`Collation`] is a *resolved description*: the caller reads the source
//! database's catalog/metadata (or knows it statically) and passes the result
//! in. This crate never performs that discovery itself.

use core::cmp::Ordering;
use core::fmt;

use crate::error::{Error, Result};

/// A provider-specific collation data version token.
///
/// For `PostgreSQL` ICU this is the `collversion` reported by
/// `pg_collation_actual_version(oid)`, e.g. `153.128`. For `MySQL` it is the
/// UCA version implied by the collation name, e.g. `9.0.0`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct VersionId(String);

impl VersionId {
    /// Construct a version token from anything string-like.
    #[must_use]
    pub fn new(v: impl Into<String>) -> Self {
        Self(v.into())
    }

    /// The token as a string slice.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl From<String> for VersionId {
    fn from(v: String) -> Self {
        Self(v)
    }
}

impl From<&str> for VersionId {
    fn from(v: &str) -> Self {
        Self(v.to_owned())
    }
}

impl fmt::Display for VersionId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

/// `PostgreSQL` collation provider.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum PostgresProvider {
    /// ICU provider (`collprovider = 'i'`).
    Icu,
    /// libc provider (`collprovider = 'c'`). Only `C`/`POSIX` are supported and
    /// they are bytewise.
    Libc,
    /// The `C`/`POSIX` locale under any provider: bytewise ordering.
    C,
}

/// A resolved `PostgreSQL` collation description.
///
/// Fields are private; use the constructors and [`PostgresCollation::with_rules`]
/// / [`PostgresCollation::with_encoding`] to build one. `rules` and `encoding`
/// are part of the identity: a custom-rule collation or a non-UTF-8 source is
/// not reproducible by this crate and is refused at construction.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub struct PostgresCollation {
    provider: PostgresProvider,
    locale: String,
    deterministic: bool,
    version: VersionId,
    rules: Option<String>,
    encoding: String,
}

impl PostgresCollation {
    /// Bytewise `C`/`POSIX` collation.
    ///
    /// The comparison is bytewise, but the library only supports UTF-8 sources,
    /// so a non-UTF-8 source is still refused at construction (the `encoding`
    /// field defaults to `UTF8`; use [`PostgresCollation::with_encoding`] to
    /// describe another).
    #[must_use]
    pub fn c() -> Self {
        Self {
            provider: PostgresProvider::C,
            locale: String::new(),
            deterministic: true,
            version: VersionId::new("c"),
            rules: None,
            encoding: "UTF8".to_owned(),
        }
    }

    /// A `PostgreSQL` ICU collation description with no custom rules, assuming a
    /// UTF-8 source. Use [`PostgresCollation::with_rules`] /
    /// [`PostgresCollation::with_encoding`] to describe other sources.
    #[must_use]
    pub fn icu(locale: impl Into<String>, deterministic: bool, version: VersionId) -> Self {
        Self {
            provider: PostgresProvider::Icu,
            locale: locale.into(),
            deterministic,
            version,
            rules: None,
            encoding: "UTF8".to_owned(),
        }
    }

    /// A `PostgreSQL` libc collation description. Only `C`/`POSIX` are
    /// reproducible (bytewise); any other libc locale is refused at
    /// construction. Use [`PostgresCollation::c`] for the `C` locale directly.
    #[must_use]
    pub fn libc(locale: impl Into<String>) -> Self {
        Self {
            provider: PostgresProvider::Libc,
            locale: locale.into(),
            deterministic: true,
            version: VersionId::new("libc"),
            rules: None,
            encoding: "UTF8".to_owned(),
        }
    }

    /// Attach custom ICU collation rules (`collicurules`).
    ///
    /// The library refuses **any** value here at construction — including the
    /// empty string — with `Error::Unsupported`. Custom ICU rules are not
    /// reproducible exactly; do not compare such a collation locally.
    #[must_use]
    pub fn with_rules(mut self, rules: impl Into<String>) -> Self {
        self.rules = Some(rules.into());
        self
    }

    /// Set the source encoding (`pg_collation`/database encoding, e.g. `UTF8`,
    /// `LATIN1`).
    #[must_use]
    pub fn with_encoding(mut self, encoding: impl Into<String>) -> Self {
        self.encoding = encoding.into();
        self
    }

    /// The provider.
    #[must_use]
    pub const fn provider(&self) -> PostgresProvider {
        self.provider
    }

    /// ICU locale id (empty for `C`).
    #[must_use]
    pub fn locale(&self) -> &str {
        &self.locale
    }

    /// Whether the source collation is deterministic.
    #[must_use]
    pub const fn deterministic(&self) -> bool {
        self.deterministic
    }

    /// The source collation data version (`collversion`).
    #[must_use]
    pub const fn version(&self) -> &VersionId {
        &self.version
    }

    /// Custom ICU collation rules, if any.
    #[must_use]
    pub fn rules(&self) -> Option<&str> {
        self.rules.as_deref()
    }

    /// The source encoding.
    #[must_use]
    pub fn encoding(&self) -> &str {
        &self.encoding
    }
}

/// `MySQL` UCA weight-table version.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum MysqlUcaVersion {
    /// UCA 4.0.0, used by `utf8mb4_unicode_ci` and friends.
    Uca400,
    /// UCA 9.0.0, used by `utf8mb4_0900_ai_ci` and friends.
    Uca900,
}

impl MysqlUcaVersion {
    /// The canonical version token.
    #[must_use]
    pub fn version_id(self) -> VersionId {
        match self {
            Self::Uca400 => VersionId::new("4.0.0"),
            Self::Uca900 => VersionId::new("9.0.0"),
        }
    }
}

/// Trailing-space comparison semantics.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Pad {
    /// Trailing spaces are significant (NO PAD; the `utf8mb4_0900_*` family).
    NoPad,
    /// Trailing spaces are ignored (PAD SPACE; UCA 4.0.0 collations).
    Space,
}

/// A resolved `MySQL` collation description.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub struct MysqlCollation {
    /// The collation name, e.g. `utf8mb4_0900_ai_ci`.
    pub name: String,
    /// The UCA version of its weight table.
    pub uca_version: MysqlUcaVersion,
    /// Padding semantics.
    pub pad: Pad,
}

impl MysqlCollation {
    /// Build a `MySQL` UCA collation description from its resolved parts.
    #[must_use]
    pub fn new(name: impl Into<String>, uca_version: MysqlUcaVersion, pad: Pad) -> Self {
        Self {
            name: name.into(),
            uca_version,
            pad,
        }
    }

    /// `utf8mb4_0900_ai_ci`: UCA 9.0.0, accent- and case-insensitive, NO PAD.
    #[must_use]
    pub fn utf8mb4_0900_ai_ci() -> Self {
        Self {
            name: "utf8mb4_0900_ai_ci".to_owned(),
            uca_version: MysqlUcaVersion::Uca900,
            pad: Pad::NoPad,
        }
    }

    /// `utf8mb4_unicode_ci`: UCA 4.0.0, accent- and case-insensitive, PAD SPACE.
    #[must_use]
    pub fn utf8mb4_unicode_ci() -> Self {
        Self {
            name: "utf8mb4_unicode_ci".to_owned(),
            uca_version: MysqlUcaVersion::Uca400,
            pad: Pad::Space,
        }
    }
}

/// `Oracle` collation provider.
///
/// Oracle compares character data under a *named collation* selected by
/// `NLS_SORT`. Only two families are reproducible by this crate: the bytewise
/// `BINARY` collation and the `UCA*_DUCET` Unicode Collation Algorithm
/// collations built from the open DUCET tables.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum OracleProvider {
    /// Bytewise comparison (`NLS_SORT=BINARY`) — the `Binary` backend.
    Binary,
    /// A `UCA*_DUCET` collation backed by an open DUCET weight table.
    Uca,
}

/// `Oracle` UCA version for a `UCA*_DUCET` collation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum OracleUcaVersion {
    /// `UCA0700_DUCET` — Unicode 7.0.0 DUCET.
    Uca700,
    /// `UCA1210_DUCET` — Unicode 12.1.0 DUCET.
    Uca1210,
}

impl OracleUcaVersion {
    /// The canonical version token.
    #[must_use]
    pub fn version_id(self) -> VersionId {
        match self {
            Self::Uca700 => VersionId::new("7.0.0"),
            Self::Uca1210 => VersionId::new("12.1.0"),
        }
    }

    /// The `Oracle` collation name this version corresponds to.
    #[must_use]
    pub const fn collation_name(self) -> &'static str {
        match self {
            Self::Uca700 => "UCA0700_DUCET",
            Self::Uca1210 => "UCA1210_DUCET",
        }
    }
}

/// A resolved `Oracle` collation description.
///
/// Fields are private; use the constructors. Only `NLS_SORT=BINARY` on an
/// `AL32UTF8` database with `VARCHAR2` semantics, and the `UCA0700_DUCET` /
/// `UCA1210_DUCET` collations, are reproducible. Every other Oracle collation
/// (monolingual, `_M`, `BINARY_CI`/`BINARY_AI`, `*_ROOT`, `*_ORADUCET`, the
/// tailored UCA collations) is refused at construction.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub struct OracleCollation {
    provider: OracleProvider,
    /// The resolved `NLS_SORT` name (normalized), e.g. `BINARY`, `UCA1210_DUCET`.
    nls_sort: String,
    uca_version: Option<OracleUcaVersion>,
    // Only AL32UTF8 is modelled. Kept explicit so a wrong source is refused
    // rather than silently compared with the wrong bytes.
    charset: String,
    // `CHAR` is blank-padded; `VARCHAR2` is not. Only the latter is modelled.
    blank_padded: bool,
}

impl OracleCollation {
    /// Bytewise `NLS_SORT=BINARY` on an `AL32UTF8` `VARCHAR2`.
    #[must_use]
    pub fn binary() -> Self {
        Self {
            provider: OracleProvider::Binary,
            nls_sort: "BINARY".to_owned(),
            uca_version: None,
            charset: "AL32UTF8".to_owned(),
            blank_padded: false,
        }
    }

    /// A `UCA*_DUCET` collation on an `AL32UTF8` `VARCHAR2`.
    #[must_use]
    pub fn uca_ducet(version: OracleUcaVersion) -> Self {
        Self {
            provider: OracleProvider::Uca,
            nls_sort: version.collation_name().to_owned(),
            uca_version: Some(version),
            charset: "AL32UTF8".to_owned(),
            blank_padded: false,
        }
    }

    /// Build a description from a raw resolved `NLS_SORT` name and source
    /// charset. The name is normalized (trimmed, upper-cased). Unknown or
    /// unsupported names are refused at [`Collation::oracle`].
    #[must_use]
    pub fn from_nls_sort(nls_sort: impl Into<String>, charset: impl Into<String>) -> Self {
        let name = nls_sort.into().trim().to_ascii_uppercase();
        let (provider, uca_version) = match name.as_str() {
            "UCA0700_DUCET" => (OracleProvider::Uca, Some(OracleUcaVersion::Uca700)),
            "UCA1210_DUCET" => (OracleProvider::Uca, Some(OracleUcaVersion::Uca1210)),
            // `BINARY` and anything else: unsupported names are refused at
            // construction; only `BINARY` is a valid binary-provider name.
            _ => (OracleProvider::Binary, None),
        };
        Self {
            provider,
            nls_sort: name,
            uca_version,
            charset: charset.into(),
            blank_padded: false,
        }
    }

    /// Mark the source as a blank-padded `CHAR`/`NCHAR` column.
    ///
    /// Blank-padded comparison differs from `VARCHAR2` even under the same
    /// collation, so any blank-padded source is refused at construction.
    #[must_use]
    pub fn with_blank_padded(mut self, blank_padded: bool) -> Self {
        self.blank_padded = blank_padded;
        self
    }

    /// The provider.
    #[must_use]
    pub const fn provider(&self) -> OracleProvider {
        self.provider
    }

    /// The resolved `NLS_SORT` name.
    #[must_use]
    pub fn nls_sort(&self) -> &str {
        &self.nls_sort
    }

    /// The UCA version, for `UCA*_DUCET` collations.
    #[must_use]
    pub const fn uca_version(&self) -> Option<OracleUcaVersion> {
        self.uca_version
    }

    /// The source character set (only `AL32UTF8` is modelled).
    #[must_use]
    pub fn charset(&self) -> &str {
        &self.charset
    }

    /// Whether the source is blank-padded (`CHAR`/`NCHAR`).
    #[must_use]
    pub const fn blank_padded(&self) -> bool {
        self.blank_padded
    }
}

/// Which backend a [`Collation`] uses.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum Backend {
    /// Bytewise comparison (`PostgreSQL` `C`/`POSIX`, `MySQL` `*_bin`).
    Binary,
    /// `PostgreSQL` ICU.
    PostgresIcu(PostgresCollation),
    /// `MySQL` UCA weight tables.
    MysqlUca(MysqlCollation),
    /// `Oracle` (bytewise `BINARY` or `UCA*_DUCET`).
    Oracle(OracleCollation),
}

/// A collation that this crate can compare with, or an explicit refusal.
///
/// Construct one of these, then call [`Collation::compare`]. Construction fails
/// closed: anything not modelled exactly yields [`Error`].
///
/// Serialization round-trips the *description* ([`Backend`]); deserialization
/// re-runs the same validated construction, so a tampered or stale payload
/// cannot produce an unchecked `Collation`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(try_from = "Backend", into = "Backend"))]
pub struct Collation {
    backend: Backend,
}

#[cfg(feature = "serde")]
impl TryFrom<Backend> for Collation {
    type Error = Error;

    fn try_from(backend: Backend) -> Result<Self> {
        Self::from_backend(backend)
    }
}

#[cfg(feature = "serde")]
impl From<Collation> for Backend {
    fn from(collation: Collation) -> Self {
        collation.backend
    }
}

impl Collation {
    /// Bytewise comparison. Always available.
    #[must_use]
    pub const fn binary() -> Self {
        Self {
            backend: Backend::Binary,
        }
    }

    /// `PostgreSQL` `C`/`POSIX`, bytewise.
    #[must_use]
    pub const fn postgres_c() -> Self {
        Self::binary()
    }

    /// Construct a `PostgreSQL` ICU collation, validating the version at runtime.
    ///
    /// Returns [`Error::VersionMismatch`] if `source_version` does not match the
    /// ICU data the linked backend would use, and [`Error::Unsupported`] for
    /// custom rules or non-ICU providers.
    pub fn postgres_icu(
        locale: impl Into<String>,
        deterministic: bool,
        source_version: VersionId,
    ) -> Result<Self> {
        let spec = PostgresCollation::icu(locale, deterministic, source_version);
        Self::postgres(spec)
    }

    /// Construct a collation from a fully resolved `PostgreSQL` description.
    ///
    /// Refuses configurations this crate cannot reproduce exactly: libc
    /// providers other than `C`/`POSIX`, custom ICU rules, and non-UTF-8
    /// sources.
    #[allow(clippy::needless_pass_by_value)]
    pub fn postgres(spec: PostgresCollation) -> Result<Self> {
        if !spec.encoding().eq_ignore_ascii_case("UTF8") {
            return Err(Error::unsupported(
                "postgres",
                spec.locale(),
                "only UTF-8 sources are reproducible; other encodings are refused",
            ));
        }
        if spec.rules().is_some() {
            return Err(Error::unsupported(
                "postgres",
                spec.locale(),
                "custom ICU collation rules (collicurules) are not reproducible",
            ));
        }
        match spec.provider() {
            PostgresProvider::C => Ok(Self::binary()),
            PostgresProvider::Libc => {
                // `C`/`POSIX` under libc are bytewise and reproducible; other
                // libc locales depend on the host OS locale database and are not.
                let locale = spec.locale();
                if locale.eq_ignore_ascii_case("C") || locale.eq_ignore_ascii_case("POSIX") {
                    Ok(Self::binary())
                } else {
                    Err(Error::unsupported(
                        "postgres",
                        locale,
                        "libc collations other than C/POSIX are not reproducible portably",
                    ))
                }
            }
            PostgresProvider::Icu => {
                #[cfg(feature = "postgres-icu")]
                {
                    crate::postgres::validate(&spec)?;
                    Ok(Self {
                        backend: Backend::PostgresIcu(spec),
                    })
                }
                #[cfg(not(feature = "postgres-icu"))]
                {
                    let _ = spec;
                    Err(Error::FeatureDisabled {
                        feature: "postgres-icu",
                    })
                }
            }
        }
    }

    /// Construct the modelled `MySQL` `utf8mb4_0900_ai_ci` collation.
    ///
    /// Only available with the `mysql-uca` feature (enabled by default). Use
    /// [`Collation::mysql`] when the feature set is not known at compile time;
    /// it returns [`Error::FeatureDisabled`] instead.
    #[cfg(feature = "mysql-uca")]
    #[must_use]
    pub fn mysql_0900_ai_ci() -> Self {
        Self::mysql(MysqlCollation::utf8mb4_0900_ai_ci()).expect("utf8mb4_0900_ai_ci is supported")
    }

    /// Construct the modelled `MySQL` `utf8mb4_unicode_ci` collation.
    ///
    /// Only available with the `mysql-uca` feature (enabled by default). Use
    /// [`Collation::mysql`] when the feature set is not known at compile time;
    /// it returns [`Error::FeatureDisabled`] instead.
    #[cfg(feature = "mysql-uca")]
    #[must_use]
    pub fn mysql_unicode_ci() -> Self {
        Self::mysql(MysqlCollation::utf8mb4_unicode_ci()).expect("utf8mb4_unicode_ci is supported")
    }

    /// Construct a collation from a fully resolved `MySQL` description.
    #[allow(clippy::needless_pass_by_value)]
    pub fn mysql(spec: MysqlCollation) -> Result<Self> {
        #[cfg(feature = "mysql-uca")]
        {
            crate::mysql::validate(&spec)?;
            Ok(Self {
                backend: Backend::MysqlUca(spec),
            })
        }
        #[cfg(not(feature = "mysql-uca"))]
        {
            let _ = spec;
            Err(Error::FeatureDisabled {
                feature: "mysql-uca",
            })
        }
    }

    /// Construct a collation from a fully resolved `Oracle` description.
    ///
    /// Only `NLS_SORT=BINARY` on `AL32UTF8` `VARCHAR2` (bytewise) and the
    /// `UCA0700_DUCET` / `UCA1210_DUCET` collations are reproducible. Every
    /// other Oracle collation — monolingual, `_M`, `BINARY_CI`/`BINARY_AI`,
    /// `*_ROOT`, `*_ORADUCET`, tailored UCA, non-`AL32UTF8`, blank-padded — is
    /// refused with [`Error::Unsupported`].
    #[allow(clippy::needless_pass_by_value)]
    pub fn oracle(spec: OracleCollation) -> Result<Self> {
        #[cfg(feature = "oracle")]
        {
            Ok(Self {
                backend: crate::oracle::validate(&spec)?,
            })
        }
        #[cfg(not(feature = "oracle"))]
        {
            let _ = spec;
            Err(Error::FeatureDisabled { feature: "oracle" })
        }
    }

    /// Resolve a `MySQL` collation by name **without a version**, refusing any
    /// name whose semantics depend on the server version.
    ///
    /// This is intentionally conservative. Prefer [`Collation::mysql`] with an
    /// explicit [`MysqlCollation`] resolved by the caller.
    pub fn from_mysql_name_unspecified_version(name: &str) -> Result<Self> {
        match name {
            "utf8mb4_0900_ai_ci" => Self::mysql(MysqlCollation::utf8mb4_0900_ai_ci()),
            "utf8mb4_unicode_ci" => Self::mysql(MysqlCollation::utf8mb4_unicode_ci()),
            // `utf8mb4_0900_bin` is NO PAD and orders by code point; for valid
            // UTF-8 that is exactly the bytewise `binary` backend. (The legacy
            // `utf8mb4_bin` is PAD SPACE and is intentionally not mapped here.)
            "utf8mb4_0900_bin" => Ok(Self::binary()),
            _ => Err(Error::unsupported(
                "mysql",
                name,
                "unrecognised or version-dependent collation; resolve it explicitly",
            )),
        }
    }

    /// The backend this collation uses.
    #[must_use]
    pub const fn backend(&self) -> &Backend {
        &self.backend
    }

    /// Compare two UTF-8 strings exactly as the source database would.
    ///
    /// This is the primary operation. Sort keys are intentionally not part of
    /// the stable API: the contract is a three-way comparison, and a collation
    /// that also defines a bytewise tie-break (`PostgreSQL` deterministic
    /// collations) cannot be reproduced by a single ICU sort key, which encodes
    /// only the primary ICU comparison.
    ///
    /// Returns [`Error`] if the underlying backend fails at runtime. The binary
    /// backend never fails; the ICU backend can, and a failed comparison is
    /// never silently reported as equality.
    pub fn compare(&self, a: &str, b: &str) -> Result<Ordering> {
        match &self.backend {
            Backend::Binary => Ok(a.as_bytes().cmp(b.as_bytes())),
            #[cfg(feature = "postgres-icu")]
            Backend::PostgresIcu(spec) => crate::postgres::compare(spec, a, b),
            #[cfg(not(feature = "postgres-icu"))]
            Backend::PostgresIcu(_) => unreachable!("postgres backend cannot be constructed"),
            #[cfg(feature = "mysql-uca")]
            Backend::MysqlUca(spec) => Ok(crate::mysql::compare(spec, a, b)),
            #[cfg(not(feature = "mysql-uca"))]
            Backend::MysqlUca(_) => unreachable!("mysql backend cannot be constructed"),
            #[cfg(feature = "oracle-uca")]
            Backend::Oracle(spec) => crate::oracle::compare(spec, a, b),
            #[cfg(not(feature = "oracle-uca"))]
            Backend::Oracle(_) => unreachable!("oracle backend cannot be constructed"),
        }
    }

    /// Whether `a` and `b` are equal under this collation.
    ///
    /// Returns [`Error`] for the same reasons as [`Collation::compare`].
    pub fn equal(&self, a: &str, b: &str) -> Result<bool> {
        Ok(self.compare(a, b)? == Ordering::Equal)
    }

    /// Build a collation from a resolved [`Backend`] description, re-running the
    /// same validation as the public constructors.
    ///
    /// This is the single construction path used by deserialization, so an
    /// arbitrary or stale description cannot bypass the support boundary.
    pub fn from_backend(backend: Backend) -> Result<Self> {
        match backend {
            Backend::Binary => Ok(Self::binary()),
            Backend::PostgresIcu(spec) => Self::postgres(spec),
            Backend::MysqlUca(spec) => Self::mysql(spec),
            Backend::Oracle(spec) => Self::oracle(spec),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_binary_is_bytewise() {
        let c = Collation::binary();
        assert_eq!(c.compare("a", "b").unwrap(), Ordering::Less);
        assert_eq!(c.compare("A", "a").unwrap(), Ordering::Less);
        assert!(c.equal("a", "a").unwrap());
    }

    #[test]
    fn test_postgres_libc_is_refused() {
        let spec = PostgresCollation::libc("en_US.UTF-8");
        assert!(Collation::postgres(spec).is_err());
    }

    #[test]
    fn test_postgres_libc_c_is_binary() {
        for locale in ["C", "POSIX", "posix"] {
            let spec = PostgresCollation::libc(locale);
            let c = Collation::postgres(spec).unwrap();
            assert!(matches!(c.backend(), Backend::Binary), "{locale}");
        }
    }

    #[test]
    fn test_unspecified_mysql_name_unknown_is_refused() {
        assert!(Collation::from_mysql_name_unspecified_version("utf8mb4_general_ci").is_err());
        assert!(Collation::from_mysql_name_unspecified_version("utf8mb4_zh_0900_as_cs").is_err());
    }

    #[test]
    fn test_collation_is_send_sync() {
        static_assertions::assert_impl_all!(Collation: Send, Sync);
    }

    #[test]
    fn test_error_is_fallback() {
        let e = Error::unsupported("mysql", "x", "y");
        assert!(e.is_fallback_required());
    }
}