serde icon indicating copy to clipboard operation
serde copied to clipboard

How to hide a field

Open irevoire opened this issue 2 years ago • 1 comments

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?

irevoire avatar May 18 '22 08:05 irevoire

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

Mingun avatar May 18 '22 09:05 Mingun

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.

dtolnay avatar Jul 09 '23 07:07 dtolnay