Skip to content

Commit dca649b

Browse files
committed
functora-tagged configurable diesel feature
1 parent 272deb3 commit dca649b

File tree

3 files changed

+87
-75
lines changed

3 files changed

+87
-75
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"rust-analyzer.cargo.features": ["diesel"]
3+
}

rust/functora-tagged/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ documentation = "https://docs.rs/functora-tagged"
1010
description = "Lightweight, macro-free newtypes with refinement and derived traits."
1111

1212
[dependencies]
13-
diesel = "2.3.3"
13+
diesel = { version = "2.3.3", optional = true }
1414
serde = { version = "1.0.228", features = ["derive"] }
1515
thiserror = "2.0.17"
16+
17+
[features]
18+
default = []
19+
diesel = ["dep:diesel"]

rust/functora-tagged/src/lib.rs

Lines changed: 79 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -106,89 +106,94 @@ macro_rules! lit {
106106
}};
107107
}
108108

109-
//
110-
// Diesel
111-
//
112-
113-
use diesel::Queryable;
114-
use diesel::backend::Backend;
115-
use diesel::deserialize::FromSql;
116-
use diesel::expression::AsExpression;
117-
use diesel::serialize::{Output, ToSql};
118-
use diesel::sql_types::{SingleValue, SqlType};
119-
120-
impl<Rep, Tag, ST> AsExpression<ST> for Tagged<Rep, Tag>
121-
where
122-
Rep: Refine<Tag> + AsExpression<ST>,
123-
ST: SqlType + SingleValue,
124-
{
125-
type Expression = <Rep as AsExpression<ST>>::Expression;
109+
#[cfg(feature = "diesel")]
110+
mod diesel_impl {
111+
use crate::*;
112+
use diesel::Queryable;
113+
use diesel::backend::Backend;
114+
use diesel::deserialize::FromSql;
115+
use diesel::expression::AsExpression;
116+
use diesel::serialize::{Output, ToSql};
117+
use diesel::sql_types::{SingleValue, SqlType};
118+
119+
impl<Rep, Tag, ST> AsExpression<ST> for Tagged<Rep, Tag>
120+
where
121+
Rep: Refine<Tag> + AsExpression<ST>,
122+
ST: SqlType + SingleValue,
123+
{
124+
type Expression =
125+
<Rep as AsExpression<ST>>::Expression;
126126

127-
fn as_expression(self) -> Self::Expression {
128-
self.0.clone().as_expression()
127+
fn as_expression(self) -> Self::Expression {
128+
self.0.clone().as_expression()
129+
}
129130
}
130-
}
131131

132-
impl<Rep, Tag, ST> AsExpression<ST> for &Tagged<Rep, Tag>
133-
where
134-
Rep: Refine<Tag> + AsExpression<ST>,
135-
ST: SqlType + SingleValue,
136-
{
137-
type Expression = <Rep as AsExpression<ST>>::Expression;
132+
impl<Rep, Tag, ST> AsExpression<ST> for &Tagged<Rep, Tag>
133+
where
134+
Rep: Refine<Tag> + AsExpression<ST>,
135+
ST: SqlType + SingleValue,
136+
{
137+
type Expression =
138+
<Rep as AsExpression<ST>>::Expression;
138139

139-
fn as_expression(self) -> Self::Expression {
140-
self.0.clone().as_expression()
140+
fn as_expression(self) -> Self::Expression {
141+
self.0.clone().as_expression()
142+
}
141143
}
142-
}
143144

144-
impl<DB, Rep, Tag, ST> ToSql<ST, DB> for Tagged<Rep, Tag>
145-
where
146-
Rep: Refine<Tag> + ToSql<ST, DB>,
147-
ST: SqlType + SingleValue,
148-
DB: Backend,
149-
Tag: Debug,
150-
{
151-
fn to_sql<'a>(
152-
&'a self,
153-
out: &mut Output<'a, '_, DB>,
154-
) -> diesel::serialize::Result {
155-
self.0.to_sql(out)
145+
impl<DB, Rep, Tag, ST> ToSql<ST, DB> for Tagged<Rep, Tag>
146+
where
147+
Rep: Refine<Tag> + ToSql<ST, DB>,
148+
ST: SqlType + SingleValue,
149+
DB: Backend,
150+
Tag: Debug,
151+
{
152+
fn to_sql<'a>(
153+
&'a self,
154+
out: &mut Output<'a, '_, DB>,
155+
) -> diesel::serialize::Result {
156+
self.0.to_sql(out)
157+
}
156158
}
157-
}
158159

159-
impl<DB, Rep, Tag, ST> FromSql<ST, DB> for Tagged<Rep, Tag>
160-
where
161-
Rep: Refine<Tag> + FromSql<ST, DB>,
162-
ST: SqlType + SingleValue,
163-
DB: Backend,
164-
{
165-
fn from_sql(
166-
bytes: DB::RawValue<'_>,
167-
) -> diesel::deserialize::Result<Self> {
168-
let rep = Rep::from_sql(bytes)?;
169-
Tagged::new(rep).map_err(|e| {
170-
format!("Tagged decode/refine failed: {e}")
171-
.into()
172-
})
160+
impl<DB, Rep, Tag, ST> FromSql<ST, DB> for Tagged<Rep, Tag>
161+
where
162+
Rep: Refine<Tag> + FromSql<ST, DB>,
163+
ST: SqlType + SingleValue,
164+
DB: Backend,
165+
{
166+
fn from_sql(
167+
bytes: DB::RawValue<'_>,
168+
) -> diesel::deserialize::Result<Self> {
169+
let rep = Rep::from_sql(bytes)?;
170+
Tagged::new(rep).map_err(|e| {
171+
format!("Tagged decode/refine failed: {e}")
172+
.into()
173+
})
174+
}
173175
}
174-
}
175176

176-
impl<Rep, Tag, ST, DB> Queryable<ST, DB>
177-
for Tagged<Rep, Tag>
178-
where
179-
Rep: Refine<Tag> + FromSql<ST, DB> + Queryable<ST, DB>,
180-
ST: SqlType + SingleValue,
181-
DB: Backend,
182-
{
183-
type Row = <Rep as Queryable<ST, DB>>::Row;
184-
185-
fn build(
186-
row: Self::Row,
187-
) -> diesel::deserialize::Result<Self> {
188-
let rep = <Rep as Queryable<ST, DB>>::build(row)?;
189-
Tagged::new(rep).map_err(|e| {
190-
format!("Tagged decode/refine failed: {e}")
191-
.into()
192-
})
177+
impl<Rep, Tag, ST, DB> Queryable<ST, DB>
178+
for Tagged<Rep, Tag>
179+
where
180+
Rep: Refine<Tag>
181+
+ FromSql<ST, DB>
182+
+ Queryable<ST, DB>,
183+
ST: SqlType + SingleValue,
184+
DB: Backend,
185+
{
186+
type Row = <Rep as Queryable<ST, DB>>::Row;
187+
188+
fn build(
189+
row: Self::Row,
190+
) -> diesel::deserialize::Result<Self> {
191+
let rep =
192+
<Rep as Queryable<ST, DB>>::build(row)?;
193+
Tagged::new(rep).map_err(|e| {
194+
format!("Tagged decode/refine failed: {e}")
195+
.into()
196+
})
197+
}
193198
}
194199
}

0 commit comments

Comments
 (0)