Skip to content

Commit 51fd55a

Browse files
feat: add Either type
1 parent f265c8a commit 51fd55a

File tree

3 files changed

+67
-3
lines changed

3 files changed

+67
-3
lines changed

benzina/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ features = [
2424
rustdoc-args = ["--cfg", "docsrs"]
2525

2626
[dependencies]
27-
diesel = { version = "2.3", default-features = false, optional = true }
27+
diesel = { version = "2.3", default-features = false }
2828
serde_core = { version = "1.0.221", optional = true }
2929
serde_json = { version = "1.0.144", optional = true }
3030
schemars = { version = "1", default-features = false, optional = true }
@@ -41,10 +41,10 @@ uuid = { version = ">=0.7.0, <2.0.0", default-features = false, features = ["v4"
4141

4242
[features]
4343
default = ["derive"]
44-
derive = ["dep:benzina-derive", "dep:diesel", "dep:indexmap"]
44+
derive = ["dep:benzina-derive", "dep:indexmap"]
4545
rustc-hash = ["dep:rustc-hash"]
4646

47-
postgres = ["benzina-derive?/postgres", "dep:diesel", "diesel/postgres_backend"]
47+
postgres = ["benzina-derive?/postgres", "diesel/postgres_backend"]
4848
typed-uuid = ["postgres", "diesel/uuid", "dep:uuid"]
4949
mysql = ["benzina-derive?/mysql"]
5050

benzina/src/either.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use diesel::backend::Backend;
2+
use diesel::expression::ValidGrouping;
3+
use diesel::query_builder::{AstPass, QueryFragment, QueryId};
4+
use diesel::{AppearsOnTable, Expression, QueryResult, SelectableExpression};
5+
6+
/// Either type for Diesel expressions - allows different expression types in match arms.
7+
#[derive(Debug, Clone, Copy)]
8+
pub enum Either<L, R> {
9+
Left(L),
10+
Right(R),
11+
}
12+
13+
impl<L, R> Expression for Either<L, R>
14+
where
15+
L: Expression,
16+
R: Expression<SqlType = L::SqlType>,
17+
{
18+
type SqlType = L::SqlType;
19+
}
20+
21+
impl<L, R, QS> AppearsOnTable<QS> for Either<L, R>
22+
where
23+
Self: Expression,
24+
L: AppearsOnTable<QS>,
25+
R: AppearsOnTable<QS>,
26+
{
27+
}
28+
29+
impl<L, R, GB> ValidGrouping<GB> for Either<L, R>
30+
where
31+
L: ValidGrouping<GB>,
32+
R: ValidGrouping<GB, IsAggregate = L::IsAggregate>,
33+
{
34+
type IsAggregate = L::IsAggregate;
35+
}
36+
37+
impl<L, R, QS> SelectableExpression<QS> for Either<L, R>
38+
where
39+
Self: AppearsOnTable<QS>,
40+
L: SelectableExpression<QS>,
41+
R: SelectableExpression<QS>,
42+
{
43+
}
44+
45+
impl<L, R, DB> QueryFragment<DB> for Either<L, R>
46+
where
47+
DB: Backend,
48+
L: QueryFragment<DB>,
49+
R: QueryFragment<DB>,
50+
{
51+
fn walk_ast<'b>(&'b self, pass: AstPass<'_, 'b, DB>) -> QueryResult<()> {
52+
match self {
53+
Either::Left(l) => l.walk_ast(pass),
54+
Either::Right(r) => r.walk_ast(pass),
55+
}
56+
}
57+
}
58+
59+
impl<L, R> QueryId for Either<L, R> {
60+
type QueryId = ();
61+
const HAS_STATIC_QUERY_ID: bool = false;
62+
}

benzina/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub use benzina_derive::{Enum, join};
77
pub use self::array::{Array, ArrayWithNullableItems};
88
#[cfg(feature = "ctid")]
99
pub use self::ctid::{Ctid, ctid};
10+
pub use self::either::Either;
1011
#[cfg(feature = "postgres")]
1112
pub use self::int::{U15, U31, U63};
1213
#[cfg(feature = "json")]
@@ -22,6 +23,7 @@ pub mod __private;
2223
mod array;
2324
#[cfg(feature = "ctid")]
2425
mod ctid;
26+
mod either;
2527
#[cfg(feature = "postgres")]
2628
pub mod error;
2729
#[cfg(feature = "example-generated")]

0 commit comments

Comments
 (0)