bitflag_attr/parser.rs
1//! Parsing flags from text.
2//!
3//! # Grammar
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//! Flags values can be formatted as _Flags_ by iterating over them, formatting each yielded flags
13//! value as a _Flag_. Any yielded flags value that sets exactly the bits of a defined flag with a
14//! name should be formatted as a _Name_. Otherwise it must be formatted as a _Hex Number_.
15//!
16//! Text that is empty or whitespace is an empty flags value.
17//!
18//! ## Modes
19//! Formatting and parsing supports three modes:
20//!
21//! - **Retain**: Formatting and parsing roundtrips exactly the bits of the source flags value.
22//! This is the default behavior.
23//! - **Truncate**: Flags values are truncated before formatting, and truncated after parsing.
24//! - **Strict**: A _Flag_ may only be formatted and parsed as a _Name_. _Hex numbers_ are not
25//! allowed. A consequence of this is that unknown bits and any bits that aren't in a contained
26//! named flag will be ignored. This is recommended for flags values serialized across API
27//! boundaries, like web services.
28//!
29//! Given the following flags type:
30//!
31//! ```rust
32//! # use bitflag_attr::bitflag;
33//! #[bitflag(u8)]
34//! #[derive(Clone, Copy)]
35//! enum Flags {
36//! A = 0b0000_0001,
37//! B = 0b0000_0010,
38//! AB = 0b0000_0011,
39//! C = 0b0000_1100,
40//! }
41//! ```
42//!
43//! The following are examples of how flags values can be formatted using any mode:
44//!
45//! ```rust,ignore
46//! 0b0000_0000 = ""
47//! 0b0000_0001 = "A"
48//! 0b0000_0010 = "B"
49//! 0b0000_0011 = "A | B"
50//! 0b0000_0011 = "AB"
51//! 0b0000_1111 = "A | B | C"
52//! ```
53//!
54//! Truncate mode will unset any unknown bits:
55//!
56//! ```rust,ignore
57//! 0b1000_0000 = ""
58//! 0b1111_1111 = "A | B | C"
59//! 0b0000_1000 = "0x8"
60//! ```
61//!
62//! Retain mode will include any unknown bits as a final _Flag_:
63//!
64//! ```rust,ignore
65//! 0b1000_0000 = "0x80"
66//! 0b1111_1111 = "A | B | C | 0xf0"
67//! 0b0000_1000 = "0x8"
68//! ```
69//!
70//! Strict mode will unset any unknown bits, as well as bits not contained in any defined named flags:
71//!
72//! ```rust,ignore
73//! 0b1000_0000 = ""
74//! 0b1111_1111 = "A | B | C"
75//! 0b0000_1000 = ""
76//! ```
77//!
78//! # Example
79//!
80//! As an example, this is how `Flags::A | Flags::B | 0x0c` can be represented as text:
81//!
82//! ```text
83//! A | B | 0x0c
84//! ```
85//!
86//! Alternatively, it could be represented without whitespace:
87//!
88//! ```text
89//! A|B|0x0C
90//! ```
91//!
92//! Note that identifiers are *case-sensitive*, so the following is **not equivalent**:
93//!
94//! ```text
95//! a|b|0x0C
96//! ```
97#![allow(clippy::let_unit_value)]
98
99use core::fmt::{self, Write};
100
101use crate::{BitsPrimitive, Flags};
102
103#[cfg(feature = "alloc")]
104use alloc::string::{String, ToString};
105
106#[cfg(not(feature = "alloc"))]
107use fmt::Display as ToString;
108
109/// Write a flags value as text.
110///
111/// Any bits that aren't part of a contained flag will be formatted as a hex number.
112pub fn to_writer<B: Flags>(flags: &B, mut writer: impl Write) -> Result<(), fmt::Error> {
113 // A formatter for bitflags that produces text output like:
114 //
115 // A | B | 0xf6
116 //
117 // The names of set flags are written in a bar-separated-format,
118 // followed by a hex number of any remaining bits that are set
119 // but don't correspond to any flags.
120
121 // Iterate over known flag values
122 let mut first = true;
123 let mut iter = flags.iter_names();
124 for (name, _) in &mut iter {
125 if !first {
126 writer.write_str(" | ")?;
127 }
128
129 first = false;
130 writer.write_str(name)?;
131 }
132
133 // Append any extra bits that correspond to flags to the end of the format
134 let remaining = iter.remaining().bits();
135 if remaining != B::Bits::EMPTY {
136 if !first {
137 writer.write_str(" | ")?;
138 }
139
140 write!(writer, "{remaining:#X}")?;
141 }
142
143 fmt::Result::Ok(())
144}
145
146/// Parse a flags value from text.
147///
148/// This function will fail on any names that don't correspond to defined flags.
149/// Unknown bits will be retained.
150pub fn from_text<B: Flags>(input: &str) -> Result<B, ParseError>
151where
152 B::Bits: ParseHex,
153{
154 let mut parsed_flags = B::empty();
155
156 // If the input is empty then return an empty set of flags
157 if input.trim().is_empty() {
158 return Ok(parsed_flags);
159 }
160
161 for flag in input.split('|') {
162 let flag = flag.trim();
163
164 // If the flag is empty then we've got missing input
165 if flag.is_empty() {
166 return Err(ParseError::empty_flag());
167 }
168
169 // If the flag starts with `0x` then it's a hex number
170 // Parse it directly to the underlying bits type
171 let parsed_flag = if let Some(flag) = flag.strip_prefix("0x") {
172 let bits =
173 <B::Bits>::parse_hex(flag).map_err(|_| ParseError::invalid_hex_flag(flag))?;
174
175 B::from_bits_retain(bits)
176 }
177 // Otherwise the flag is a name
178 // The generated flags type will determine whether
179 // or not it's a valid identifier
180 else {
181 B::from_name(flag).ok_or_else(|| ParseError::invalid_named_flag(flag))?
182 };
183
184 parsed_flags.set(parsed_flag);
185 }
186
187 Ok(parsed_flags)
188}
189
190/// Write a flags value as text, ignoring any unknown bits.
191pub fn to_writer_truncate<B: Flags>(flags: &B, writer: impl Write) -> Result<(), fmt::Error> {
192 to_writer(&B::from_bits_truncate(flags.bits()), writer)
193}
194
195/// Parse a flags value from text.
196///
197/// This function will fail on any names that don't correspond to defined flags.
198/// Unknown bits will be ignored.
199pub fn from_text_truncate<B: Flags>(input: &str) -> Result<B, ParseError>
200where
201 B::Bits: ParseHex,
202{
203 Ok(B::from_bits_truncate(from_text::<B>(input)?.bits()))
204}
205
206/// Write only the contained, defined, named flags in a flags value as text.
207pub fn to_writer_strict<B: Flags>(flags: &B, mut writer: impl Write) -> Result<(), fmt::Error> {
208 // This is a simplified version of `to_writer` that ignores
209 // any bits not corresponding to a named flag
210
211 let mut first = true;
212 let mut iter = flags.iter_names();
213 for (name, _) in &mut iter {
214 if !first {
215 writer.write_str(" | ")?;
216 }
217
218 first = false;
219 writer.write_str(name)?;
220 }
221
222 fmt::Result::Ok(())
223}
224
225/// Parse a flags value from text.
226///
227/// This function will fail on any names that don't correspond to defined flags.
228/// This function will fail to parse hex values.
229pub fn from_text_strict<B: Flags>(input: &str) -> Result<B, ParseError> {
230 // This is a simplified version of `from_str` that ignores
231 // any bits not corresponding to a named flag
232
233 let mut parsed_flags = B::empty();
234
235 // If the input is empty then return an empty set of flags
236 if input.trim().is_empty() {
237 return Ok(parsed_flags);
238 }
239
240 for flag in input.split('|') {
241 let flag = flag.trim();
242
243 // If the flag is empty then we've got missing input
244 if flag.is_empty() {
245 return Err(ParseError::empty_flag());
246 }
247
248 // If the flag starts with `0x` then it's a hex number
249 // These aren't supported in the strict parser
250 if flag.starts_with("0x") {
251 return Err(ParseError::invalid_hex_flag("unsupported hex flag value"));
252 }
253
254 let parsed_flag = B::from_name(flag).ok_or_else(|| ParseError::invalid_named_flag(flag))?;
255
256 parsed_flags.set(parsed_flag);
257 }
258
259 Ok(parsed_flags)
260}
261
262/// Parse a value from a hex string.
263pub trait ParseHex {
264 /// Parse the value from hex.
265 fn parse_hex(input: &str) -> Result<Self, ParseError>
266 where
267 Self: Sized;
268}
269
270/// An error encountered while parsing flags from text.
271#[derive(Debug)]
272pub struct ParseError(ParseErrorKind);
273
274#[derive(Debug)]
275#[allow(clippy::enum_variant_names)]
276enum ParseErrorKind {
277 EmptyFlag,
278 InvalidNamedFlag {
279 #[cfg(not(feature = "alloc"))]
280 got: (),
281 #[cfg(feature = "alloc")]
282 got: String,
283 },
284 InvalidHexFlag {
285 #[cfg(not(feature = "alloc"))]
286 got: (),
287 #[cfg(feature = "alloc")]
288 got: String,
289 },
290}
291
292impl ParseError {
293 /// An invalid hex flag was encountered.
294 pub fn invalid_hex_flag<T>(flag: T) -> Self
295 where
296 T: fmt::Display + ToString,
297 {
298 let _flag = flag;
299
300 let got = {
301 #[cfg(feature = "alloc")]
302 {
303 _flag.to_string()
304 }
305 };
306
307 ParseError(ParseErrorKind::InvalidHexFlag { got })
308 }
309
310 /// A named flag that doesn't correspond to any on the flags type was encountered.
311 pub fn invalid_named_flag<T>(flag: T) -> Self
312 where
313 T: fmt::Display + ToString,
314 {
315 let _flag = flag;
316
317 let got = {
318 #[cfg(feature = "alloc")]
319 {
320 _flag.to_string()
321 }
322 };
323
324 ParseError(ParseErrorKind::InvalidNamedFlag { got })
325 }
326
327 /// A hex or named flag wasn't found between separators.
328 pub const fn empty_flag() -> Self {
329 ParseError(ParseErrorKind::EmptyFlag)
330 }
331}
332
333impl fmt::Display for ParseError {
334 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
335 match &self.0 {
336 ParseErrorKind::InvalidNamedFlag { got } => {
337 let _got = got;
338
339 write!(f, "unrecognized named flag")?;
340
341 #[cfg(feature = "alloc")]
342 {
343 write!(f, " `{}`", _got)?;
344 }
345 }
346 ParseErrorKind::InvalidHexFlag { got } => {
347 let _got = got;
348
349 write!(f, "invalid hex flag")?;
350
351 #[cfg(feature = "alloc")]
352 {
353 write!(f, " `{}`", _got)?;
354 }
355 }
356 ParseErrorKind::EmptyFlag => {
357 write!(f, "encountered empty flag")?;
358 }
359 }
360
361 Ok(())
362 }
363}
364
365impl core::error::Error for ParseError {}