Deserialize a set of flags from a human-readable string or their underlying bits. Any unknown bits will be retained. */
(deserializer: D)
| 35 | Any unknown bits will be retained. |
| 36 | */ |
| 37 | pub fn deserialize<'de, B: Flags, D: Deserializer<'de>>(deserializer: D) -> Result<B, D::Error> |
| 38 | where |
| 39 | B::Bits: ParseHex + Deserialize<'de>, |
| 40 | { |
| 41 | if deserializer.is_human_readable() { |
| 42 | // Deserialize human-readable flags by parsing them from strings like `"A | B"` |
| 43 | struct FlagsVisitor<B>(core::marker::PhantomData<B>); |
| 44 | |
| 45 | impl<'de, B: Flags> Visitor<'de> for FlagsVisitor<B> |
| 46 | where |
| 47 | B::Bits: ParseHex, |
| 48 | { |
| 49 | type Value = B; |
| 50 | |
| 51 | fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 52 | formatter.write_str("a string value of `|` separated flags") |
| 53 | } |
| 54 | |
| 55 | fn visit_str<E: Error>(self, flags: &str) -> Result<Self::Value, E> { |
| 56 | parser::from_str(flags).map_err(|e| E::custom(e)) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | deserializer.deserialize_str(FlagsVisitor(Default::default())) |
| 61 | } else { |
| 62 | // Deserialize non-human-readable flags directly from the underlying bits |
| 63 | let bits = B::Bits::deserialize(deserializer)?; |
| 64 | |
| 65 | Ok(B::from_bits_retain(bits)) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | #[cfg(test)] |
| 70 | mod tests { |
nothing calls this directly
no test coverage detected