-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Regression in quality of TOML type mismatch error messages #3790
Comments
Thanks for the report! @dtolnay do you know if there's an easy-ish way to configure that error message locally? |
Yes the untagged enum error messages need some work. We are tracking this in serde-rs/serde#773. For now you can provide a custom message by writing the Deserialize impl yourself.
use serde::de::{self, Deserialize, Deserializer, Visitor};
impl Deserialize for TomlDependency {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer
{
struct TomlDependencyVisitor;
impl Visitor for TomlDependencyVisitor {
type Value = TomlDependency;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a version string like \"0.9.8\" or a \
detailed dependency like {version = \"0.9.8\"}")
}
fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
where E: de::Error
{
Ok(TomlDependency::Simple(s.to_owned()))
}
fn visit_map<V>(self, map: V) -> Result<Self::Value, V::Error>
where V: de::MapVisitor
{
let mvd = de::value::MapVisitorDeserializer::new(map);
DetailedTomlDependency::deserialize(mvd).map(TomlDependency::Detailed)
}
}
deserializer.deserialize(TomlDependencyVisitor)
}
} |
Might be worth the extra effort to implement the above, since that error message is even better than the original. |
@dtolnay awesome, thanks! @crumblingstatue yeah I'll send a PR. |
Unfortunately while `#[serde(untagged)]` is precisely what we want in terms of semantics it leaves a little to be desired in terms of error messages. This commit updates to remove the usage of that attribute in favor of implementing `Deserialize` directly, which is quite simple in these few cases. Closes rust-lang#3790
Improve TOML decoding error messages Unfortunately while `#[serde(untagged)]` is precisely what we want in terms of semantics it leaves a little to be desired in terms of error messages. This commit updates to remove the usage of that attribute in favor of implementing `Deserialize` directly, which is quite simple in these few cases. Closes #3790
Fixing this might require changes in
toml
, or maybe evenserde
, but I don't think we should sacrifice user experience for the developer productivity that serde provides.The text was updated successfully, but these errors were encountered: