Skip to content

Commit 82a5354

Browse files
committed
Adjust error message
1 parent 7dfec1b commit 82a5354

File tree

6 files changed

+44
-3
lines changed

6 files changed

+44
-3
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ required-features = ["unwrap"]
253253
[[test]]
254254
name = "compile_fail"
255255
path = "tests/compile_fail/mod.rs"
256-
required-features = ["as_ref", "debug", "display", "from", "into", "is_variant", "try_from"]
256+
required-features = ["as_ref", "debug", "display", "from", "into", "is_variant", "try_from", "try_into"]
257257

258258
[[test]]
259259
name = "no_std"

impl/src/try_into.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::utils::{
44
};
55
use proc_macro2::{Span, TokenStream};
66
use quote::{quote, ToTokens};
7-
use syn::{Attribute, DeriveInput, Ident, Result};
7+
use syn::{Attribute, DeriveInput, Error, Ident, Result};
88

99
use crate::utils::{
1010
attr::{self, ParseMultiple as _},
@@ -52,7 +52,20 @@ pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStre
5252
struct_: vec!["ignore", "owned", "ref", "ref_mut"],
5353
field: vec!["ignore"],
5454
},
55-
)?;
55+
).map_err(|err| {
56+
let msg = &err.to_string();
57+
if msg == "Only a single attribute is allowed" {
58+
Error::new(
59+
err.span(),
60+
"Only a single attribute is allowed.\n\
61+
For the top-level attribute, an additional `#[try_into(error(...))]` may be provided.",
62+
)
63+
} else if msg.starts_with("Attribute parameter not supported. Supported attribute parameters are:") {
64+
Error::new(err.span(), format!("{msg}, error"))
65+
} else {
66+
err
67+
}
68+
})?;
5669
assert!(
5770
state.derive_type == DeriveType::Enum,
5871
"Only enums can derive TryInto"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#[derive(derive_more::TryInto)]
2+
#[try_into(foo)]
3+
enum MixedData {
4+
Int(u32),
5+
String(String),
6+
}
7+
8+
fn main() {}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error: Attribute parameter not supported. Supported attribute parameters are: ignore, owned, ref, ref_mut, error
2+
--> tests/compile_fail/try_into/invalid_attr.rs:2:12
3+
|
4+
2 | #[try_into(foo)]
5+
| ^^^
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#[derive(derive_more::TryInto)]
2+
#[try_into(owned)]
3+
#[try_into(foo)]
4+
enum MixedData {
5+
Int(u32),
6+
String(String),
7+
}
8+
9+
fn main() {}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
error: Only a single attribute is allowed.
2+
For the top-level attribute, an additional `#[try_into(error(...))]` may be provided.
3+
--> tests/compile_fail/try_into/surplus_attrs.rs:3:1
4+
|
5+
3 | #[try_into(foo)]
6+
| ^

0 commit comments

Comments
 (0)