diesel_trgm/
lib.rs

1#[macro_use]
2extern crate diesel;
3
4mod functions {
5    use diesel::sql_types::*;
6
7    sql_function! {
8        fn similarity<T: IntoNullable>(x: T, y: T) -> Float;
9    }
10
11    sql_function! {
12        fn word_similarity<T: IntoNullable>(x: T, y: T) -> Float;
13    }
14}
15
16mod dsl {
17    use diesel::expression::{AsExpression, Expression};
18
19    mod predicates {
20        use diesel::pg::Pg;
21        use diesel::sql_types::*;
22
23        diesel_infix_operator!(Similar, " % ", Bool, backend: Pg);
24        diesel_infix_operator!(WordSimilar, " <% ", Bool, backend: Pg);
25        diesel_infix_operator!(WordSimilarComm, " %> ", Bool, backend: Pg);
26        diesel_infix_operator!(Distance, " <-> ", Float, backend: Pg);
27        diesel_infix_operator!(WordDistance, " <<-> ", Float, backend: Pg);
28        diesel_infix_operator!(WordDistanceComm, " <->> ", Float, backend: Pg);
29    }
30
31    use self::predicates::*;
32
33    pub trait TrgmExtensions: Expression + Sized {
34        fn similar<T: AsExpression<Self::SqlType>>(self, other: T) -> Similar<Self, T::Expression> {
35            Similar::new(self, other.as_expression())
36        }
37    }
38
39    impl<T: Expression> TrgmExtensions for T {}
40}
41
42pub use self::dsl::*;
43pub use self::functions::*;