Skip to main content

db_collation/
lib.rs

1// Copyright 2026 db-collation contributors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! # db-collation
16//!
17//! Offline, exact string comparison under an **explicitly identified database
18//! collation and version**.
19//!
20//! This crate exists for one purpose: reproduce the *exact* ordering semantics
21//! of a source database's collation locally, without talking to that database.
22//! It is a pure function of a [`Collation`] description and the input bytes. It
23//! opens no sockets, holds no drivers, and never issues a query.
24//!
25//! ## Guarantees and non-guarantees
26//!
27//! - **Exact or refused.** For a supported configuration, [`Collation::compare`]
28//!   matches the source database's comparison operator exactly (validated by the
29//!   differential harness in `harness/`). For anything not explicitly modelled,
30//!   construction returns [`Error::Unsupported`] and the caller should fall back
31//!   to comparing on the source database. A *supported* configuration can still
32//!   reject an individual input that exceeds a documented source limit (e.g.
33//!   Oracle's 2000-byte UCA sort key); that is [`Error::InputLimit`], and the
34//!   caller falls back for that input only.
35//! - **No approximation.** Selecting the same locale name is *not* sufficient:
36//!   ICU data versions change ordering (e.g. `あ` vs `U+11DB0` reversed between
37//!   ICU 76 and 78), and `MySQL` collations differ by UCA version, tailoring and
38//!   padding. This crate therefore gates on version, not on names alone.
39//! - **Deterministic tie-break.** `PostgreSQL` deterministic collations break ICU
40//!   equality ties with a bytewise comparison (shorter string first). That is
41//!   reproduced here; nondeterministic collations intentionally do not.
42//!
43//! ## Supported configurations
44//!
45//! See [`Collation`] and the crate `README` for the current matrix.
46//!
47//! # Examples
48//!
49//! ```
50//! use db_collation::{Collation, Result};
51//! use std::cmp::Ordering;
52//!
53//! fn main() -> Result<()> {
54//!     // Bytewise comparison is always available.
55//!     assert_eq!(Collation::binary().compare("a", "b")?, Ordering::Less);
56//!
57//!     // Unsupported configurations fail closed, so the caller can fall back.
58//!     assert!(Collation::from_mysql_name_unspecified_version("utf8mb4_general_ci").is_err());
59//!     Ok(())
60//! }
61//! ```
62//!
63//! With the `mysql-uca` feature (enabled by default):
64//!
65//! ```
66//! # #[cfg(feature = "mysql-uca")] {
67//! use db_collation::{Collation, Result};
68//! use std::cmp::Ordering;
69//!
70//! fn main() -> Result<()> {
71//!     let c = Collation::mysql_0900_ai_ci();
72//!     assert_eq!(c.compare("a", "A")?, Ordering::Equal);
73//!     Ok(())
74//! }
75//! # }
76//! ```
77#![cfg_attr(docsrs, feature(doc_cfg))]
78#![forbid(unsafe_op_in_unsafe_fn)]
79
80mod error;
81#[cfg(feature = "mysql-uca")]
82#[allow(unreachable_pub)]
83mod mysql;
84#[cfg(feature = "oracle")]
85#[allow(unreachable_pub)]
86mod oracle;
87#[cfg(feature = "postgres-icu")]
88#[allow(unreachable_pub)]
89mod postgres;
90mod spec;
91#[cfg(feature = "postgres-icu")]
92#[allow(unreachable_pub)]
93mod thread_local;
94
95pub use error::{Error, Result};
96#[cfg(feature = "postgres-icu")]
97pub use postgres::{collation_version, icu_library_version};
98pub use spec::{
99    Backend, Collation, MysqlCollation, MysqlUcaVersion, OracleCollation, OracleProvider,
100    OracleUcaVersion, Pad, PostgresCollation, PostgresProvider, VersionId,
101};