Skip to content

Provide an easy way to customize only the error message #1883

@yaymukund

Description

@yaymukund

EDIT After thinking about it some more, I feel it would be better to improve this error message rather than add API surface area. #1544 gets close, but I believe those errors are still too opaque for Cargo's purposes, and it's hidden behind a flag. Anyway, my original post:


I have an untagged union:

#[derive(Serialize, Deserialize)]
#[serde(untagged)]
enum MyEnum {
  Foo(Vec<String>),
  Bar(usize)
}

If all variants fail, you get this error message:

"data did not match any variant of untagged enum {}",

Is there a good way to customize the error message but keep the default #[derive(Deserialize)] implementation? Perhaps a new attribute?:

#[serde(untagged, expecting = "a number or a list of strings")]
enum MyEnum {
  ...
}

Advantages:

  • You don't have to reimplement Deserialize
  • Concise

Disadvantages

  • How common is this use-case? We have to do this a couple of times within Cargo.
  • Is there a better solution I am overlooking?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions