Parse a flags value from text. This function will fail on any names that don't correspond to defined flags. This function will fail to parse hex values. */
(input: &str)
| 187 | This function will fail to parse hex values. |
| 188 | */ |
| 189 | pub fn from_str_strict<B: Flags>(input: &str) -> Result<B, ParseError> { |
| 190 | // This is a simplified version of `from_str` that ignores |
| 191 | // any bits not corresponding to a named flag |
| 192 | |
| 193 | let mut parsed_flags = B::empty(); |
| 194 | |
| 195 | // If the input is empty then return an empty set of flags |
| 196 | if input.trim().is_empty() { |
| 197 | return Ok(parsed_flags); |
| 198 | } |
| 199 | |
| 200 | for flag in input.split('|') { |
| 201 | let flag = flag.trim(); |
| 202 | |
| 203 | // If the flag is empty then we've got missing input |
| 204 | if flag.is_empty() { |
| 205 | return Err(ParseError::empty_flag()); |
| 206 | } |
| 207 | |
| 208 | // If the flag starts with `0x` then it's a hex number |
| 209 | // These aren't supported in the strict parser |
| 210 | if flag.starts_with("0x") { |
| 211 | return Err(ParseError::invalid_hex_flag("unsupported hex flag value")); |
| 212 | } |
| 213 | |
| 214 | let parsed_flag = B::from_name(flag).ok_or_else(|| ParseError::invalid_named_flag(flag))?; |
| 215 | |
| 216 | parsed_flags.insert(parsed_flag); |
| 217 | } |
| 218 | |
| 219 | Ok(parsed_flags) |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | Encode a value as a hex string. |