serde
serde copied to clipboard
How to hide a field
Is there a way to hide a field akin to what we can do in clap.
Here is my structure;
#[derive(Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct Search {
query: Option<String>,
#[serde(default = "false")]
limit: usize,
#[serde(default = "false")]
experimental: bool,
}
And when I receive an unknown field, the following error message is thrown, which is great;
Query deserialize error: unknown field `plouf`, expected one of `query`, `limit`, `experimental`.
But I would like to hide the experimental
field from the error message.
I was looking for something like that:
pub struct Search {
...
#[serde(default = "false", hide)]
experimental: bool,
}
But it doesn’t exist. Did I miss something?
There is not such attribute. I think it would be useless mostly. You can try to utilize an expecting
attribute to override default serde error message:
#[derive(Deserialize, Debug)]
#[serde(deny_unknown_fields)]
#[serde(expecting = "expected one of `query`, `limit`")]
pub struct Search {
query: Option<String>,
#[serde(default = "false")]
limit: usize,
#[serde(default = "false")]
experimental: bool,
}
But I'm not sure that it would work in your case, because it is only one possible error message
Yeah there isn't an attribute for this.
I would recommend handwriting the Deserialize impl for Search
to have full control over the error reporting.