MCPcopy Create free account
hub / github.com/bitflags/bitflags / deserialize

Function deserialize

src/external/serde.rs:37–67  ·  view source on GitHub ↗

Deserialize a set of flags from a human-readable string or their underlying bits. Any unknown bits will be retained. */

(deserializer: D)

Source from the content-addressed store, hash-verified

35Any unknown bits will be retained.
36*/
37pub fn deserialize<'de, B: Flags, D: Deserializer<'de>>(deserializer: D) -> Result<B, D::Error>
38where
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)]
70mod tests {

Callers

nothing calls this directly

Calls 2

FlagsVisitorClass · 0.85
OkClass · 0.85

Tested by

no test coverage detected