0

Is there any way to add a platform based condition on #[serde(with = "path_handling")]? So, basically I want to use this custome Serde method only in Inix and on Windows I want to use default way.

pub struct Res {
    pub last: bool,
    #[serde(with = "path_handling")] // ignore this line on windows as path_handling module contains unix specific logic
    pub path: PathBuf,
}
markalex
  • 8,623
  • 2
  • 7
  • 32
Rusting_It
  • 35
  • 5
  • Relevant: https://stackoverflow.com/questions/42551113/is-it-possible-to-conditionally-enable-an-attribute-like-derive https://stackoverflow.com/questions/41742046/is-there-a-list-of-all-cfg-features – E_net4 Sep 05 '22 at 10:19

1 Answers1

3

Using cfg_attr and target_family.

pub struct Res {
    pub last: bool,
    #[cfg_attr(target_family = "unix", serde(with = "path_handling"))]
    pub path: PathBuf,
}
Sprite
  • 3,222
  • 1
  • 12
  • 29