/rust/registry/src/index.crates.io-6f17d22bba15001f/bitflags-2.4.0/src/parser.rs
Line | Count | Source (jump to first uncovered line) |
1 | | /*! |
2 | | Parsing flags from text. |
3 | | |
4 | | Format and parse a flags value as text using the following grammar: |
5 | | |
6 | | - _Flags:_ (_Whitespace_ _Flag_ _Whitespace_)`|`* |
7 | | - _Flag:_ _Name_ | _Hex Number_ |
8 | | - _Name:_ The name of any defined flag |
9 | | - _Hex Number_: `0x`([0-9a-fA-F])* |
10 | | - _Whitespace_: (\s)* |
11 | | |
12 | | As an example, this is how `Flags::A | Flags::B | 0x0c` can be represented as text: |
13 | | |
14 | | ```text |
15 | | A | B | 0x0c |
16 | | ``` |
17 | | |
18 | | Alternatively, it could be represented without whitespace: |
19 | | |
20 | | ```text |
21 | | A|B|0x0C |
22 | | ``` |
23 | | |
24 | | Note that identifiers are *case-sensitive*, so the following is *not equivalent*: |
25 | | |
26 | | ```text |
27 | | a|b|0x0C |
28 | | ``` |
29 | | */ |
30 | | |
31 | | #![allow(clippy::let_unit_value)] |
32 | | |
33 | | use core::fmt::{self, Write}; |
34 | | |
35 | | use crate::{Bits, Flags}; |
36 | | |
37 | | /** |
38 | | Write a flags value as text. |
39 | | |
40 | | Any bits that aren't part of a contained flag will be formatted as a hex number. |
41 | | */ |
42 | 0 | pub fn to_writer<B: Flags>(flags: &B, mut writer: impl Write) -> Result<(), fmt::Error> |
43 | 0 | where |
44 | 0 | B::Bits: WriteHex, |
45 | 0 | { |
46 | 0 | // A formatter for bitflags that produces text output like: |
47 | 0 | // |
48 | 0 | // A | B | 0xf6 |
49 | 0 | // |
50 | 0 | // The names of set flags are written in a bar-separated-format, |
51 | 0 | // followed by a hex number of any remaining bits that are set |
52 | 0 | // but don't correspond to any flags. |
53 | 0 |
|
54 | 0 | // Iterate over known flag values |
55 | 0 | let mut first = true; |
56 | 0 | let mut iter = flags.iter_names(); |
57 | 0 | for (name, _) in &mut iter { |
58 | 0 | if !first { |
59 | 0 | writer.write_str(" | ")?; |
60 | 0 | } |
61 | | |
62 | 0 | first = false; |
63 | 0 | writer.write_str(name)?; |
64 | | } |
65 | | |
66 | | // Append any extra bits that correspond to flags to the end of the format |
67 | 0 | let remaining = iter.remaining().bits(); |
68 | 0 | if remaining != B::Bits::EMPTY { |
69 | 0 | if !first { |
70 | 0 | writer.write_str(" | ")?; |
71 | 0 | } |
72 | | |
73 | 0 | writer.write_str("0x")?; |
74 | 0 | remaining.write_hex(writer)?; |
75 | 0 | } |
76 | | |
77 | 0 | fmt::Result::Ok(()) |
78 | 0 | } Unexecuted instantiation: bitflags::parser::to_writer::<_, _> Unexecuted instantiation: bitflags::parser::to_writer::<cras_common::types_internal::EFFECT_TYPE, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<cras_common::types_internal::CRAS_NC_PROVIDER, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<cras_common::types_internal::CRAS_STREAM_ACTIVE_AP_EFFECT, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<cras_common::types_internal::EFFECT_TYPE, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<cras_common::types_internal::CRAS_NC_PROVIDER, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<nix::fcntl::RenameFlags, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<nix::fcntl::FallocateFlags, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<nix::fcntl::OFlag, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<nix::fcntl::FdFlag, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<nix::fcntl::AtFlags, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<nix::fcntl::SealFlag, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<nix::unistd::AccessFlags, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<nix::sys::stat::Mode, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<nix::sys::stat::SFlag, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<nix::sys::wait::WaitPidFlag, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<nix::sys::memfd::MemFdCreateFlag, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<nix::sys::signal::SaFlags, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<nix::sys::socket::TimestampingFlag, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<nix::sys::socket::MsgFlags, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<nix::sys::socket::SockFlag, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<nix::sys::statvfs::FsFlags, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<nix::sys::signalfd::SfdFlags, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<nix::fcntl::RenameFlags, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<nix::fcntl::FallocateFlags, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<nix::fcntl::OFlag, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<nix::fcntl::FdFlag, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<nix::fcntl::AtFlags, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<nix::fcntl::SealFlag, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<nix::unistd::AccessFlags, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<nix::sys::stat::Mode, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<nix::sys::stat::SFlag, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<nix::sys::memfd::MemFdCreateFlag, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<nix::sys::statvfs::FsFlags, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<openssl::cipher_ctx::CipherCtxFlags, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<openssl::cms::CMSOptions, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<openssl::ssl::SslOptions, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<openssl::ssl::ShutdownState, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<openssl::ssl::SslVerifyMode, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<openssl::ssl::ExtensionContext, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<openssl::ssl::SslSessionCacheMode, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<openssl::ssl::SslMode, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<openssl::ocsp::OcspFlag, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<openssl::pkcs7::Pkcs7Flags, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<openssl::x509::verify::X509CheckFlags, &mut core::fmt::Formatter> Unexecuted instantiation: bitflags::parser::to_writer::<openssl::x509::verify::X509VerifyFlags, &mut core::fmt::Formatter> |
79 | | |
80 | | pub(crate) struct AsDisplay<'a, B>(pub(crate) &'a B); |
81 | | |
82 | | impl<'a, B: Flags> fmt::Display for AsDisplay<'a, B> |
83 | | where |
84 | | B::Bits: WriteHex, |
85 | | { |
86 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
87 | 0 | to_writer(self.0, f) |
88 | 0 | } Unexecuted instantiation: <bitflags::parser::AsDisplay<_> as core::fmt::Display>::fmt Unexecuted instantiation: <bitflags::parser::AsDisplay<cras_common::types_internal::EFFECT_TYPE> as core::fmt::Display>::fmt Unexecuted instantiation: <bitflags::parser::AsDisplay<cras_common::types_internal::CRAS_NC_PROVIDER> as core::fmt::Display>::fmt |
89 | | } |
90 | | |
91 | | /** |
92 | | Parse a flags value from text. |
93 | | |
94 | | This function will fail on any names that don't correspond to defined flags. |
95 | | Unknown bits will be retained. |
96 | | */ |
97 | 0 | pub fn from_str<B: Flags>(input: &str) -> Result<B, ParseError> |
98 | 0 | where |
99 | 0 | B::Bits: ParseHex, |
100 | 0 | { |
101 | 0 | let mut parsed_flags = B::empty(); |
102 | 0 |
|
103 | 0 | // If the input is empty then return an empty set of flags |
104 | 0 | if input.trim().is_empty() { |
105 | 0 | return Ok(parsed_flags); |
106 | 0 | } |
107 | | |
108 | 0 | for flag in input.split('|') { |
109 | 0 | let flag = flag.trim(); |
110 | 0 |
|
111 | 0 | // If the flag is empty then we've got missing input |
112 | 0 | if flag.is_empty() { |
113 | 0 | return Err(ParseError::empty_flag()); |
114 | 0 | } |
115 | | |
116 | | // If the flag starts with `0x` then it's a hex number |
117 | | // Parse it directly to the underlying bits type |
118 | 0 | let parsed_flag = if let Some(flag) = flag.strip_prefix("0x") { |
119 | 0 | let bits = |
120 | 0 | <B::Bits>::parse_hex(flag).map_err(|_| ParseError::invalid_hex_flag(flag))?; Unexecuted instantiation: bitflags::parser::from_str::<_>::{closure#0} Unexecuted instantiation: bitflags::parser::from_str::<cras_common::types_internal::EFFECT_TYPE>::{closure#0} Unexecuted instantiation: bitflags::parser::from_str::<cras_common::types_internal::CRAS_NC_PROVIDER>::{closure#0} Unexecuted instantiation: bitflags::parser::from_str::<cras_common::types_internal::CRAS_STREAM_ACTIVE_AP_EFFECT>::{closure#0} Unexecuted instantiation: bitflags::parser::from_str::<nix::fcntl::RenameFlags>::{closure#0} Unexecuted instantiation: bitflags::parser::from_str::<nix::fcntl::FallocateFlags>::{closure#0} Unexecuted instantiation: bitflags::parser::from_str::<nix::fcntl::OFlag>::{closure#0} Unexecuted instantiation: bitflags::parser::from_str::<nix::fcntl::FdFlag>::{closure#0} Unexecuted instantiation: bitflags::parser::from_str::<nix::fcntl::AtFlags>::{closure#0} Unexecuted instantiation: bitflags::parser::from_str::<nix::fcntl::SealFlag>::{closure#0} Unexecuted instantiation: bitflags::parser::from_str::<nix::unistd::AccessFlags>::{closure#0} Unexecuted instantiation: bitflags::parser::from_str::<nix::sys::stat::Mode>::{closure#0} Unexecuted instantiation: bitflags::parser::from_str::<nix::sys::stat::SFlag>::{closure#0} Unexecuted instantiation: bitflags::parser::from_str::<nix::sys::wait::WaitPidFlag>::{closure#0} Unexecuted instantiation: bitflags::parser::from_str::<nix::sys::memfd::MemFdCreateFlag>::{closure#0} Unexecuted instantiation: bitflags::parser::from_str::<nix::sys::signal::SaFlags>::{closure#0} Unexecuted instantiation: bitflags::parser::from_str::<nix::sys::socket::TimestampingFlag>::{closure#0} Unexecuted instantiation: bitflags::parser::from_str::<nix::sys::socket::MsgFlags>::{closure#0} Unexecuted instantiation: bitflags::parser::from_str::<nix::sys::socket::SockFlag>::{closure#0} Unexecuted instantiation: bitflags::parser::from_str::<nix::sys::statvfs::FsFlags>::{closure#0} Unexecuted instantiation: bitflags::parser::from_str::<nix::sys::signalfd::SfdFlags>::{closure#0} Unexecuted instantiation: bitflags::parser::from_str::<nix::fcntl::RenameFlags>::{closure#0} Unexecuted instantiation: bitflags::parser::from_str::<nix::fcntl::FallocateFlags>::{closure#0} Unexecuted instantiation: bitflags::parser::from_str::<nix::fcntl::OFlag>::{closure#0} Unexecuted instantiation: bitflags::parser::from_str::<nix::fcntl::FdFlag>::{closure#0} Unexecuted instantiation: bitflags::parser::from_str::<nix::fcntl::AtFlags>::{closure#0} Unexecuted instantiation: bitflags::parser::from_str::<nix::fcntl::SealFlag>::{closure#0} Unexecuted instantiation: bitflags::parser::from_str::<nix::unistd::AccessFlags>::{closure#0} Unexecuted instantiation: bitflags::parser::from_str::<nix::sys::stat::Mode>::{closure#0} Unexecuted instantiation: bitflags::parser::from_str::<nix::sys::stat::SFlag>::{closure#0} Unexecuted instantiation: bitflags::parser::from_str::<nix::sys::memfd::MemFdCreateFlag>::{closure#0} Unexecuted instantiation: bitflags::parser::from_str::<nix::sys::statvfs::FsFlags>::{closure#0} Unexecuted instantiation: bitflags::parser::from_str::<openssl::cipher_ctx::CipherCtxFlags>::{closure#0} Unexecuted instantiation: bitflags::parser::from_str::<openssl::cms::CMSOptions>::{closure#0} Unexecuted instantiation: bitflags::parser::from_str::<openssl::ssl::SslOptions>::{closure#0} Unexecuted instantiation: bitflags::parser::from_str::<openssl::ssl::ShutdownState>::{closure#0} Unexecuted instantiation: bitflags::parser::from_str::<openssl::ssl::SslVerifyMode>::{closure#0} Unexecuted instantiation: bitflags::parser::from_str::<openssl::ssl::ExtensionContext>::{closure#0} Unexecuted instantiation: bitflags::parser::from_str::<openssl::ssl::SslSessionCacheMode>::{closure#0} Unexecuted instantiation: bitflags::parser::from_str::<openssl::ssl::SslMode>::{closure#0} Unexecuted instantiation: bitflags::parser::from_str::<openssl::ocsp::OcspFlag>::{closure#0} Unexecuted instantiation: bitflags::parser::from_str::<openssl::pkcs7::Pkcs7Flags>::{closure#0} Unexecuted instantiation: bitflags::parser::from_str::<openssl::x509::verify::X509CheckFlags>::{closure#0} Unexecuted instantiation: bitflags::parser::from_str::<openssl::x509::verify::X509VerifyFlags>::{closure#0} |
121 | | |
122 | 0 | B::from_bits_retain(bits) |
123 | | } |
124 | | // Otherwise the flag is a name |
125 | | // The generated flags type will determine whether |
126 | | // or not it's a valid identifier |
127 | | else { |
128 | 0 | B::from_name(flag).ok_or_else(|| ParseError::invalid_named_flag(flag))? Unexecuted instantiation: bitflags::parser::from_str::<_>::{closure#1} Unexecuted instantiation: bitflags::parser::from_str::<cras_common::types_internal::EFFECT_TYPE>::{closure#1} Unexecuted instantiation: bitflags::parser::from_str::<cras_common::types_internal::CRAS_NC_PROVIDER>::{closure#1} Unexecuted instantiation: bitflags::parser::from_str::<cras_common::types_internal::CRAS_STREAM_ACTIVE_AP_EFFECT>::{closure#1} Unexecuted instantiation: bitflags::parser::from_str::<nix::fcntl::RenameFlags>::{closure#1} Unexecuted instantiation: bitflags::parser::from_str::<nix::fcntl::FallocateFlags>::{closure#1} Unexecuted instantiation: bitflags::parser::from_str::<nix::fcntl::OFlag>::{closure#1} Unexecuted instantiation: bitflags::parser::from_str::<nix::fcntl::FdFlag>::{closure#1} Unexecuted instantiation: bitflags::parser::from_str::<nix::fcntl::AtFlags>::{closure#1} Unexecuted instantiation: bitflags::parser::from_str::<nix::fcntl::SealFlag>::{closure#1} Unexecuted instantiation: bitflags::parser::from_str::<nix::unistd::AccessFlags>::{closure#1} Unexecuted instantiation: bitflags::parser::from_str::<nix::sys::stat::Mode>::{closure#1} Unexecuted instantiation: bitflags::parser::from_str::<nix::sys::stat::SFlag>::{closure#1} Unexecuted instantiation: bitflags::parser::from_str::<nix::sys::wait::WaitPidFlag>::{closure#1} Unexecuted instantiation: bitflags::parser::from_str::<nix::sys::memfd::MemFdCreateFlag>::{closure#1} Unexecuted instantiation: bitflags::parser::from_str::<nix::sys::signal::SaFlags>::{closure#1} Unexecuted instantiation: bitflags::parser::from_str::<nix::sys::socket::TimestampingFlag>::{closure#1} Unexecuted instantiation: bitflags::parser::from_str::<nix::sys::socket::MsgFlags>::{closure#1} Unexecuted instantiation: bitflags::parser::from_str::<nix::sys::socket::SockFlag>::{closure#1} Unexecuted instantiation: bitflags::parser::from_str::<nix::sys::statvfs::FsFlags>::{closure#1} Unexecuted instantiation: bitflags::parser::from_str::<nix::sys::signalfd::SfdFlags>::{closure#1} Unexecuted instantiation: bitflags::parser::from_str::<nix::fcntl::RenameFlags>::{closure#1} Unexecuted instantiation: bitflags::parser::from_str::<nix::fcntl::FallocateFlags>::{closure#1} Unexecuted instantiation: bitflags::parser::from_str::<nix::fcntl::OFlag>::{closure#1} Unexecuted instantiation: bitflags::parser::from_str::<nix::fcntl::FdFlag>::{closure#1} Unexecuted instantiation: bitflags::parser::from_str::<nix::fcntl::AtFlags>::{closure#1} Unexecuted instantiation: bitflags::parser::from_str::<nix::fcntl::SealFlag>::{closure#1} Unexecuted instantiation: bitflags::parser::from_str::<nix::unistd::AccessFlags>::{closure#1} Unexecuted instantiation: bitflags::parser::from_str::<nix::sys::stat::Mode>::{closure#1} Unexecuted instantiation: bitflags::parser::from_str::<nix::sys::stat::SFlag>::{closure#1} Unexecuted instantiation: bitflags::parser::from_str::<nix::sys::memfd::MemFdCreateFlag>::{closure#1} Unexecuted instantiation: bitflags::parser::from_str::<nix::sys::statvfs::FsFlags>::{closure#1} Unexecuted instantiation: bitflags::parser::from_str::<openssl::cipher_ctx::CipherCtxFlags>::{closure#1} Unexecuted instantiation: bitflags::parser::from_str::<openssl::cms::CMSOptions>::{closure#1} Unexecuted instantiation: bitflags::parser::from_str::<openssl::ssl::SslOptions>::{closure#1} Unexecuted instantiation: bitflags::parser::from_str::<openssl::ssl::ShutdownState>::{closure#1} Unexecuted instantiation: bitflags::parser::from_str::<openssl::ssl::SslVerifyMode>::{closure#1} Unexecuted instantiation: bitflags::parser::from_str::<openssl::ssl::ExtensionContext>::{closure#1} Unexecuted instantiation: bitflags::parser::from_str::<openssl::ssl::SslSessionCacheMode>::{closure#1} Unexecuted instantiation: bitflags::parser::from_str::<openssl::ssl::SslMode>::{closure#1} Unexecuted instantiation: bitflags::parser::from_str::<openssl::ocsp::OcspFlag>::{closure#1} Unexecuted instantiation: bitflags::parser::from_str::<openssl::pkcs7::Pkcs7Flags>::{closure#1} Unexecuted instantiation: bitflags::parser::from_str::<openssl::x509::verify::X509CheckFlags>::{closure#1} Unexecuted instantiation: bitflags::parser::from_str::<openssl::x509::verify::X509VerifyFlags>::{closure#1} |
129 | | }; |
130 | | |
131 | 0 | parsed_flags.insert(parsed_flag); |
132 | | } |
133 | | |
134 | 0 | Ok(parsed_flags) |
135 | 0 | } Unexecuted instantiation: bitflags::parser::from_str::<_> Unexecuted instantiation: bitflags::parser::from_str::<cras_common::types_internal::EFFECT_TYPE> Unexecuted instantiation: bitflags::parser::from_str::<cras_common::types_internal::CRAS_NC_PROVIDER> Unexecuted instantiation: bitflags::parser::from_str::<cras_common::types_internal::CRAS_STREAM_ACTIVE_AP_EFFECT> Unexecuted instantiation: bitflags::parser::from_str::<nix::fcntl::RenameFlags> Unexecuted instantiation: bitflags::parser::from_str::<nix::fcntl::FallocateFlags> Unexecuted instantiation: bitflags::parser::from_str::<nix::fcntl::OFlag> Unexecuted instantiation: bitflags::parser::from_str::<nix::fcntl::FdFlag> Unexecuted instantiation: bitflags::parser::from_str::<nix::fcntl::AtFlags> Unexecuted instantiation: bitflags::parser::from_str::<nix::fcntl::SealFlag> Unexecuted instantiation: bitflags::parser::from_str::<nix::unistd::AccessFlags> Unexecuted instantiation: bitflags::parser::from_str::<nix::sys::stat::Mode> Unexecuted instantiation: bitflags::parser::from_str::<nix::sys::stat::SFlag> Unexecuted instantiation: bitflags::parser::from_str::<nix::sys::wait::WaitPidFlag> Unexecuted instantiation: bitflags::parser::from_str::<nix::sys::memfd::MemFdCreateFlag> Unexecuted instantiation: bitflags::parser::from_str::<nix::sys::signal::SaFlags> Unexecuted instantiation: bitflags::parser::from_str::<nix::sys::socket::TimestampingFlag> Unexecuted instantiation: bitflags::parser::from_str::<nix::sys::socket::MsgFlags> Unexecuted instantiation: bitflags::parser::from_str::<nix::sys::socket::SockFlag> Unexecuted instantiation: bitflags::parser::from_str::<nix::sys::statvfs::FsFlags> Unexecuted instantiation: bitflags::parser::from_str::<nix::sys::signalfd::SfdFlags> Unexecuted instantiation: bitflags::parser::from_str::<nix::fcntl::RenameFlags> Unexecuted instantiation: bitflags::parser::from_str::<nix::fcntl::FallocateFlags> Unexecuted instantiation: bitflags::parser::from_str::<nix::fcntl::OFlag> Unexecuted instantiation: bitflags::parser::from_str::<nix::fcntl::FdFlag> Unexecuted instantiation: bitflags::parser::from_str::<nix::fcntl::AtFlags> Unexecuted instantiation: bitflags::parser::from_str::<nix::fcntl::SealFlag> Unexecuted instantiation: bitflags::parser::from_str::<nix::unistd::AccessFlags> Unexecuted instantiation: bitflags::parser::from_str::<nix::sys::stat::Mode> Unexecuted instantiation: bitflags::parser::from_str::<nix::sys::stat::SFlag> Unexecuted instantiation: bitflags::parser::from_str::<nix::sys::memfd::MemFdCreateFlag> Unexecuted instantiation: bitflags::parser::from_str::<nix::sys::statvfs::FsFlags> Unexecuted instantiation: bitflags::parser::from_str::<openssl::cipher_ctx::CipherCtxFlags> Unexecuted instantiation: bitflags::parser::from_str::<openssl::cms::CMSOptions> Unexecuted instantiation: bitflags::parser::from_str::<openssl::ssl::SslOptions> Unexecuted instantiation: bitflags::parser::from_str::<openssl::ssl::ShutdownState> Unexecuted instantiation: bitflags::parser::from_str::<openssl::ssl::SslVerifyMode> Unexecuted instantiation: bitflags::parser::from_str::<openssl::ssl::ExtensionContext> Unexecuted instantiation: bitflags::parser::from_str::<openssl::ssl::SslSessionCacheMode> Unexecuted instantiation: bitflags::parser::from_str::<openssl::ssl::SslMode> Unexecuted instantiation: bitflags::parser::from_str::<openssl::ocsp::OcspFlag> Unexecuted instantiation: bitflags::parser::from_str::<openssl::pkcs7::Pkcs7Flags> Unexecuted instantiation: bitflags::parser::from_str::<openssl::x509::verify::X509CheckFlags> Unexecuted instantiation: bitflags::parser::from_str::<openssl::x509::verify::X509VerifyFlags> |
136 | | |
137 | | /** |
138 | | Encode a value as a hex string. |
139 | | |
140 | | Implementors of this trait should not write the `0x` prefix. |
141 | | */ |
142 | | pub trait WriteHex { |
143 | | /// Write the value as hex. |
144 | | fn write_hex<W: fmt::Write>(&self, writer: W) -> fmt::Result; |
145 | | } |
146 | | |
147 | | /** |
148 | | Parse a value from a hex string. |
149 | | */ |
150 | | pub trait ParseHex { |
151 | | /// Parse the value from hex. |
152 | | fn parse_hex(input: &str) -> Result<Self, ParseError> |
153 | | where |
154 | | Self: Sized; |
155 | | } |
156 | | |
157 | | /// An error encountered while parsing flags from text. |
158 | | #[derive(Debug)] |
159 | | pub struct ParseError(ParseErrorKind); |
160 | | |
161 | | #[derive(Debug)] |
162 | | #[allow(clippy::enum_variant_names)] |
163 | | enum ParseErrorKind { |
164 | | EmptyFlag, |
165 | | InvalidNamedFlag { |
166 | | #[cfg(not(feature = "std"))] |
167 | | got: (), |
168 | | #[cfg(feature = "std")] |
169 | | got: String, |
170 | | }, |
171 | | InvalidHexFlag { |
172 | | #[cfg(not(feature = "std"))] |
173 | | got: (), |
174 | | #[cfg(feature = "std")] |
175 | | got: String, |
176 | | }, |
177 | | } |
178 | | |
179 | | impl ParseError { |
180 | | /// An invalid hex flag was encountered. |
181 | 0 | pub fn invalid_hex_flag(flag: impl fmt::Display) -> Self { |
182 | 0 | let _flag = flag; |
183 | 0 |
|
184 | 0 | let got = { |
185 | 0 | #[cfg(feature = "std")] |
186 | 0 | { |
187 | 0 | _flag.to_string() |
188 | 0 | } |
189 | 0 | }; |
190 | 0 |
|
191 | 0 | ParseError(ParseErrorKind::InvalidHexFlag { got }) |
192 | 0 | } Unexecuted instantiation: <bitflags::parser::ParseError>::invalid_hex_flag::<&str> Unexecuted instantiation: <bitflags::parser::ParseError>::invalid_hex_flag::<&str> Unexecuted instantiation: <bitflags::parser::ParseError>::invalid_hex_flag::<&str> Unexecuted instantiation: <bitflags::parser::ParseError>::invalid_hex_flag::<&str> Unexecuted instantiation: <bitflags::parser::ParseError>::invalid_hex_flag::<&str> |
193 | | |
194 | | /// A named flag that doesn't correspond to any on the flags type was encountered. |
195 | 0 | pub fn invalid_named_flag(flag: impl fmt::Display) -> Self { |
196 | 0 | let _flag = flag; |
197 | 0 |
|
198 | 0 | let got = { |
199 | 0 | #[cfg(feature = "std")] |
200 | 0 | { |
201 | 0 | _flag.to_string() |
202 | 0 | } |
203 | 0 | }; |
204 | 0 |
|
205 | 0 | ParseError(ParseErrorKind::InvalidNamedFlag { got }) |
206 | 0 | } Unexecuted instantiation: <bitflags::parser::ParseError>::invalid_named_flag::<_> Unexecuted instantiation: <bitflags::parser::ParseError>::invalid_named_flag::<&str> Unexecuted instantiation: <bitflags::parser::ParseError>::invalid_named_flag::<&str> Unexecuted instantiation: <bitflags::parser::ParseError>::invalid_named_flag::<&str> Unexecuted instantiation: <bitflags::parser::ParseError>::invalid_named_flag::<&str> |
207 | | |
208 | | /// A hex or named flag wasn't found between separators. |
209 | 0 | pub const fn empty_flag() -> Self { |
210 | 0 | ParseError(ParseErrorKind::EmptyFlag) |
211 | 0 | } |
212 | | } |
213 | | |
214 | | impl fmt::Display for ParseError { |
215 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
216 | 0 | match &self.0 { |
217 | 0 | ParseErrorKind::InvalidNamedFlag { got } => { |
218 | 0 | let _got = got; |
219 | 0 |
|
220 | 0 | write!(f, "unrecognized named flag")?; |
221 | | |
222 | | #[cfg(feature = "std")] |
223 | | { |
224 | | write!(f, " `{}`", _got)?; |
225 | | } |
226 | | } |
227 | 0 | ParseErrorKind::InvalidHexFlag { got } => { |
228 | 0 | let _got = got; |
229 | 0 |
|
230 | 0 | write!(f, "invalid hex flag")?; |
231 | | |
232 | | #[cfg(feature = "std")] |
233 | | { |
234 | | write!(f, " `{}`", _got)?; |
235 | | } |
236 | | } |
237 | | ParseErrorKind::EmptyFlag => { |
238 | 0 | write!(f, "encountered empty flag")?; |
239 | | } |
240 | | } |
241 | | |
242 | 0 | Ok(()) |
243 | 0 | } |
244 | | } |
245 | | |
246 | | #[cfg(feature = "std")] |
247 | | impl std::error::Error for ParseError {} |