Skip to main content

diesel/sqlite/connection/
collation_needed.rs

1//! Types used by [`SqliteConnection::on_collation_needed`](super::SqliteConnection::on_collation_needed).
2
3#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
4extern crate libsqlite3_sys as ffi;
5
6#[cfg(all(target_family = "wasm", target_os = "unknown"))]
7use sqlite_wasm_rs as ffi;
8
9/// Text encoding SQLite requested for a missing collation.
10///
11/// [`register_collation`](super::SqliteConnection::register_collation) always
12/// installs `SQLITE_UTF8`, so most callbacks can ignore this field.
13#[derive(#[automatically_derived]
impl ::core::fmt::Debug for SqliteTextRep {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            SqliteTextRep::Utf8 =>
                ::core::fmt::Formatter::write_str(f, "Utf8"),
            SqliteTextRep::Utf16Be =>
                ::core::fmt::Formatter::write_str(f, "Utf16Be"),
            SqliteTextRep::Utf16Le =>
                ::core::fmt::Formatter::write_str(f, "Utf16Le"),
            SqliteTextRep::Other(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Other",
                    &__self_0),
        }
    }
}Debug, #[automatically_derived]
impl ::core::clone::Clone for SqliteTextRep {
    #[inline]
    fn clone(&self) -> SqliteTextRep {
        let _: ::core::clone::AssertParamIsClone<i32>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for SqliteTextRep { }Copy, #[automatically_derived]
impl ::core::cmp::PartialEq for SqliteTextRep {
    #[inline]
    fn eq(&self, other: &SqliteTextRep) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr &&
            match (self, other) {
                (SqliteTextRep::Other(__self_0),
                    SqliteTextRep::Other(__arg1_0)) => __self_0 == __arg1_0,
                _ => true,
            }
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for SqliteTextRep {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<i32>;
    }
}Eq)]
14#[non_exhaustive]
15pub enum SqliteTextRep {
16    /// `SQLITE_UTF8`.
17    Utf8,
18    /// `SQLITE_UTF16BE`.
19    Utf16Be,
20    /// `SQLITE_UTF16LE`.
21    Utf16Le,
22    /// An encoding this release does not name. Preserves the raw
23    /// `eTextRep` for forward compatibility. Treat the inner integer as
24    /// opaque, and match a named variant if a future Diesel release adds one.
25    Other(i32),
26}
27
28impl SqliteTextRep {
29    pub(super) fn from_ffi(text_rep: i32) -> Self {
30        match text_rep {
31            ffi::SQLITE_UTF8 => SqliteTextRep::Utf8,
32            ffi::SQLITE_UTF16BE => SqliteTextRep::Utf16Be,
33            ffi::SQLITE_UTF16LE => SqliteTextRep::Utf16Le,
34            other => SqliteTextRep::Other(other),
35        }
36    }
37}
38
39/// Context passed to the collation-needed callback.
40///
41/// Added in SQLite 3.0.0.
42#[derive(#[automatically_derived]
impl<'a> ::core::fmt::Debug for CollationNeededContext<'a> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f,
            "CollationNeededContext", "name", &self.name, "text_rep",
            &&self.text_rep)
    }
}Debug, #[automatically_derived]
impl<'a> ::core::clone::Clone for CollationNeededContext<'a> {
    #[inline]
    fn clone(&self) -> CollationNeededContext<'a> {
        let _: ::core::clone::AssertParamIsClone<&'a str>;
        let _: ::core::clone::AssertParamIsClone<SqliteTextRep>;
        *self
    }
}Clone, #[automatically_derived]
impl<'a> ::core::marker::Copy for CollationNeededContext<'a> { }Copy)]
43#[non_exhaustive]
44pub struct CollationNeededContext<'a> {
45    /// The name of the missing collation, as UTF-8.
46    pub name: &'a str,
47    /// Preferred text encoding for the collation.
48    pub text_rep: SqliteTextRep,
49}
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54
55    #[diesel_test_helper::test]
56    fn from_ffi_maps_all_documented_variants() {
57        assert_eq!(
58            SqliteTextRep::from_ffi(ffi::SQLITE_UTF8),
59            SqliteTextRep::Utf8
60        );
61        assert_eq!(
62            SqliteTextRep::from_ffi(ffi::SQLITE_UTF16BE),
63            SqliteTextRep::Utf16Be,
64        );
65        assert_eq!(
66            SqliteTextRep::from_ffi(ffi::SQLITE_UTF16LE),
67            SqliteTextRep::Utf16Le,
68        );
69    }
70
71    #[diesel_test_helper::test]
72    fn from_ffi_preserves_unknown_encodings_in_other() {
73        // 999 is not any current SQLite eTextRep. If SQLite ever assigns it,
74        // this test starts failing and the enum learns a new named variant.
75        assert_eq!(SqliteTextRep::from_ffi(999), SqliteTextRep::Other(999));
76    }
77}