Skip to content

Commit

Permalink
Merge #238
Browse files Browse the repository at this point in the history
238: Improve error messages when serde_as is used wrongly or misleadingly r=jonasbb a=jonasbb



Co-authored-by: Jonas Bushart <jonas@bushart.org>
  • Loading branch information
bors[bot] and jonasbb authored Jan 5, 2021
2 parents b7a09f5 + c911f26 commit 977bdd4
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion serde_with_macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ proc-macro = true
maintenance = {status = "actively-developed"}

[dependencies]
darling = "0.11.0"
darling = "0.12.0"
proc-macro2 = "1.0.1"
quote = "1.0.0"

Expand Down
5 changes: 5 additions & 0 deletions serde_with_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ fn serde_as_add_attr_to_field(
let serde_as_options = SerdeAsOptions::from_field(field)?;
let serde_with_options = SerdeWithOptions::from_field(field)?;

// TODO allow multiple attributes
// Find index of serde_as attribute
let serde_as_idx = field.attrs.iter().enumerate().find_map(|(idx, attr)| {
if attr.path.is_ident("serde_as") {
Expand All @@ -515,6 +516,10 @@ fn serde_as_add_attr_to_field(
// serde_as Attribute
field.attrs.remove(serde_as_idx);

if !serde_as_options.has_any_set() {
return Err(DarlingError::custom("An empty `serde_as` attribute on a field has no effect. You are missing an `as`, `serialize_as`, or `deserialize_as` parameter."));
}

// Check if there are any conflicting attributes
if serde_as_options.has_any_set() && serde_with_options.has_any_set() {
return Err(DarlingError::custom("Cannot combine `serde_as` with serde's `with`, `deserialize_with`, or `serialize_with`."));
Expand Down
12 changes: 12 additions & 0 deletions serde_with_macros/tests/compile-fail/serde_as.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,16 @@ struct ConflictingAsAnnotations {
two_unkowns: u32,
}

/// Test error message for malformed attributes
#[serde_as]
#[derive(Serialize)]
struct MalformedAttributes {
#[serde_as(serialize_as = "123", deserialize_as = DisplayFromStr)]
missing_quotes: u32,
#[serde_as]
no_entries: u32,
#[serde_as()]
no_entries_brackets: u32,
}

fn main() {}
18 changes: 18 additions & 0 deletions serde_with_macros/tests/compile-fail/serde_as.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,21 @@ error: Unknown field: `unknw2`
|
59 | #[serde_as(unknwn1 = "Hello", unknw2 = "World")]
| ^^^^^^

error: Unable to parse attribute: expected literal
--> $DIR/serde_as.rs:67:55
|
67 | #[serde_as(serialize_as = "123", deserialize_as = DisplayFromStr)]
| ^^^^^^^^^^^^^^

error: An empty `serde_as` attribute on a field has no effect. You are missing an `as`, `serialize_as`, or `deserialize_as` parameter.
--> $DIR/serde_as.rs:70:5
|
70 | no_entries: u32,
| ^^^^^^^^^^

error: An empty `serde_as` attribute on a field has no effect. You are missing an `as`, `serialize_as`, or `deserialize_as` parameter.
--> $DIR/serde_as.rs:72:5
|
72 | no_entries_brackets: u32,
| ^^^^^^^^^^^^^^^^^^^

0 comments on commit 977bdd4

Please sign in to comment.