Skip to content

Commit 82f7712

Browse files
committed
functora-tagged serde optional feature
1 parent dca649b commit 82f7712

File tree

3 files changed

+25
-28
lines changed

3 files changed

+25
-28
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"rust-analyzer.cargo.features": ["diesel"]
2+
"rust-analyzer.cargo.features": ["serde", "diesel"]
33
}

rust/functora-tagged/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ description = "Lightweight, macro-free newtypes with refinement and derived trai
1111

1212
[dependencies]
1313
diesel = { version = "2.3.3", optional = true }
14-
serde = { version = "1.0.228", features = ["derive"] }
14+
serde = { version = "1.0.228", features = ["derive"], optional = true }
1515
thiserror = "2.0.17"
1616

1717
[features]
1818
default = []
1919
diesel = ["dep:diesel"]
20+
serde = ["dep:serde"]

rust/functora-tagged/src/lib.rs

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,19 @@
1-
use serde::{Deserialize, Serialize};
21
use std::fmt::{Debug, Display};
32
use std::marker::PhantomData;
43
use std::str::FromStr;
54
use thiserror::Error;
65

7-
#[derive(
8-
Eq, PartialEq, Ord, PartialOrd, Clone, Debug, Serialize,
9-
)]
6+
#[cfg(feature = "serde")]
7+
use serde::{Deserialize, Serialize};
8+
9+
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Debug)]
10+
#[cfg_attr(feature = "serde", derive(Serialize))]
1011
pub struct Tagged<Rep, Tag>(Rep, PhantomData<Tag>)
1112
where
1213
Rep: Refine<Tag>;
1314

1415
pub trait Refine<Tag>:
15-
Eq
16-
+ PartialEq
17-
+ Ord
18-
+ PartialOrd
19-
+ Clone
20-
+ Debug
21-
+ Serialize
22-
+ FromStr
16+
Eq + PartialEq + Ord + PartialOrd + Clone + Debug + FromStr
2317
{
2418
type DecodeErr: Debug
2519
+ Display
@@ -43,6 +37,7 @@ where
4337
{
4438
#[error("Tagged Decode error: {0}")]
4539
Decode(Rep::DecodeErr),
40+
4641
#[error("Tagged Refine error: {0}")]
4742
Refine(Rep::RefineErr),
4843
}
@@ -75,6 +70,21 @@ where
7570
}
7671
}
7772

73+
#[macro_export]
74+
macro_rules! lit {
75+
($arg:expr) => {{
76+
$crate::tagged::Tagged::from_str(stringify!($arg))
77+
.unwrap_or_else(|e| {
78+
panic!(
79+
"lit!({}) failed: {}",
80+
stringify!($arg),
81+
e
82+
)
83+
})
84+
}};
85+
}
86+
87+
#[cfg(feature = "serde")]
7888
impl<'a, Rep, Tag> Deserialize<'a> for Tagged<Rep, Tag>
7989
where
8090
Rep: Refine<Tag>,
@@ -92,20 +102,6 @@ where
92102
}
93103
}
94104

95-
#[macro_export]
96-
macro_rules! lit {
97-
($arg:expr) => {{
98-
$crate::tagged::Tagged::from_str(stringify!($arg))
99-
.unwrap_or_else(|e| {
100-
panic!(
101-
"lit!({}) failed: {}",
102-
stringify!($arg),
103-
e
104-
)
105-
})
106-
}};
107-
}
108-
109105
#[cfg(feature = "diesel")]
110106
mod diesel_impl {
111107
use crate::*;

0 commit comments

Comments
 (0)