Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specifying a function for #[serde(expecting)] #2796

Open
Anders429 opened this issue Aug 11, 2024 · 0 comments
Open

Specifying a function for #[serde(expecting)] #2796

Anders429 opened this issue Aug 11, 2024 · 0 comments

Comments

@Anders429
Copy link

#1916 added support for #[serde(expecting = "...")] for providing a custom message to the generated Visitor::expecting() method. This works great for specifying a custom message when deriving Deserialize.

I am wondering if it would be possible to expand this behavior to specify a custom function, rather than just a string. Something like:

fn custom_expecting(formatter: &mut fmt::Formatter) -> fmt::Result {
    // Custom description of expected types.
}

#[derive(Deserialize)]
#[serde(expecting_with = "custom_expecting")]
struct Foo {
    // ...
}

The motivation for this is to allow slightly more involved expecting logic, supporting custom messages based on the flags provided by fmt::Formatter. While this can be accomplished currently with custom Deserialize implementations, I would like to use the derived Deserialize impl with only the expecting() method changed.

Is this something that could be supported in serde?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant