Skip to content

Commit 4346492

Browse files
committed
Display blanked implementation, thiserror -> derive_more
1 parent aabc80e commit 4346492

File tree

4 files changed

+68
-24
lines changed

4 files changed

+68
-24
lines changed

rust/functora-tagged/Cargo.lock

Lines changed: 28 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/functora-tagged/Cargo.toml

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

1212
[dependencies]
13+
derive_more = { version = "2.0.1", features = ["display"] }
1314
diesel = { version = "2.3.3", optional = true }
1415
serde = { version = "1.0.228", optional = true }
15-
thiserror = "2.0.17"
1616

1717
[features]
1818
default = []

rust/functora-tagged/src/lib.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#![doc = include_str!("../README.md")]
2-
use std::fmt::Debug;
2+
use derive_more::Display;
3+
use std::error::Error;
4+
use std::fmt::{Debug, Display};
35
use std::marker::PhantomData;
46
use std::str::FromStr;
5-
use thiserror::Error;
67

78
#[derive(Debug)]
89
pub struct Tagged<Rep, Tag>(Rep, PhantomData<Tag>);
@@ -56,18 +57,34 @@ impl<Rep: Clone, Tag> Clone for Tagged<Rep, Tag> {
5657
}
5758
}
5859

59-
#[derive(Debug, Error)]
60+
impl<Rep: Display, Tag> Display for Tagged<Rep, Tag> {
61+
fn fmt(
62+
&self,
63+
f: &mut std::fmt::Formatter<'_>,
64+
) -> std::fmt::Result {
65+
self.rep().fmt(f)
66+
}
67+
}
68+
69+
#[derive(Debug, Display)]
6070
pub enum ParseError<Rep, Tag>
6171
where
6272
Rep: FromStr,
6373
Tag: Refine<Rep>,
6474
{
65-
#[error("Decode failed: {0}")]
6675
Decode(Rep::Err),
67-
#[error("Refine failed: {0}")]
6876
Refine(Tag::RefineError),
6977
}
7078

79+
impl<Rep, Tag> Error for ParseError<Rep, Tag>
80+
where
81+
Rep: Debug + FromStr,
82+
Tag: Debug + Refine<Rep>,
83+
Rep::Err: Debug + Display,
84+
Tag::RefineError: Debug + Display,
85+
{
86+
}
87+
7188
impl<Rep, Tag> Eq for ParseError<Rep, Tag>
7289
where
7390
Rep: FromStr,
@@ -134,8 +151,6 @@ where
134151

135152
#[cfg(feature = "serde")]
136153
use serde::{Deserialize, Serialize};
137-
#[cfg(feature = "serde")]
138-
use std::fmt::Display;
139154

140155
#[cfg(feature = "serde")]
141156
impl<Rep: Serialize, Tag> Serialize for Tagged<Rep, Tag> {

rust/functora-tagged/tests/integration.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
use derive_more::Display;
12
use functora_tagged::{ParseError, Refine, Tagged};
23
#[cfg(feature = "serde")]
34
use serde::{Deserialize, Serialize};
5+
use std::error::Error;
46
use std::fmt::Debug;
5-
use thiserror::Error;
67

78
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Debug)]
89
pub enum NonEmptyTag {}
@@ -17,27 +18,29 @@ pub enum EmailTag {}
1718
pub type Email = Tagged<NonEmpty<String>, EmailTag>;
1819

1920
#[derive(
20-
Eq, PartialEq, Ord, PartialOrd, Clone, Debug, Error,
21+
Eq, PartialEq, Ord, PartialOrd, Clone, Debug, Display,
2122
)]
22-
#[error("Empty value is not allowed")]
23-
pub struct EmptyError;
23+
pub struct NonEmptyError;
24+
impl Error for NonEmptyError {}
2425

2526
impl Refine<String> for NonEmptyTag {
26-
type RefineError = EmptyError;
27+
type RefineError = NonEmptyError;
2728
fn refine(
2829
rep: String,
2930
) -> Result<String, Self::RefineError> {
3031
if rep.is_empty() {
31-
Err(EmptyError)
32+
Err(NonEmptyError)
3233
} else {
3334
Ok(rep)
3435
}
3536
}
3637
}
3738

38-
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Error)]
39-
#[error("Invalid UserId format")]
39+
#[derive(
40+
Eq, PartialEq, Ord, PartialOrd, Debug, Display,
41+
)]
4042
pub struct UserIdError;
43+
impl Error for UserIdError {}
4144

4245
impl Refine<NonEmpty<String>> for UserIdTag {
4346
type RefineError = UserIdError;
@@ -53,9 +56,11 @@ impl Refine<NonEmpty<String>> for UserIdTag {
5356
}
5457
}
5558

56-
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Error)]
57-
#[error("String is too short: {0}, minimum length: 3")]
59+
#[derive(
60+
Eq, PartialEq, Ord, PartialOrd, Debug, Display,
61+
)]
5862
pub struct EmailError(usize);
63+
impl Error for EmailError {}
5964

6065
impl Refine<NonEmpty<String>> for EmailTag {
6166
type RefineError = EmailError;
@@ -179,7 +184,7 @@ fn test_serde_user_id_invalid_refine() {
179184
let toml = r#"user_id = "bad""#;
180185
let err = toml::from_str::<Wrapper>(toml).unwrap_err();
181186
assert!(
182-
err.to_string().contains("Invalid UserId format"),
187+
err.to_string().contains("UserIdError"),
183188
"Unexpected failure: {err}"
184189
);
185190
let toml = r#"user_id = "user_123""#;
@@ -283,10 +288,7 @@ mod diesel_tests {
283288
let err = sql_query("SELECT id, email FROM users")
284289
.load::<UserRow>(&mut conn)
285290
.unwrap_err();
286-
assert!(
287-
err.to_string()
288-
.contains("Invalid UserId format")
289-
);
291+
assert!(err.to_string().contains("UserIdError"));
290292
}
291293

292294
#[test]

0 commit comments

Comments
 (0)