Parse int32 or float with FRACTIONS_MAX_DIGITS precision (e.g. "2", "3.3", "0.0001")
()
| 239 | |
| 240 | /// Parse int32 or float with FRACTIONS_MAX_DIGITS precision (e.g. "2", "3.3", "0.0001") |
| 241 | pub fn parse_resource_amount() -> impl CharParser<ResourceAmount> { |
| 242 | parse_u32() |
| 243 | .then(just('.').ignore_then(parse_integer_string()).or_not()) |
| 244 | .try_map(|(units, frac), span| { |
| 245 | let f = if let Some(s) = frac { |
| 246 | if s.len() > FRACTIONS_MAX_DIGITS { |
| 247 | return Err(ParseError::custom(span, "Resource precision exceeded")); |
| 248 | } |
| 249 | let mut n = s.parse::<u32>().unwrap(); |
| 250 | for _ in s.len()..FRACTIONS_MAX_DIGITS { |
| 251 | n *= 10; |
| 252 | } |
| 253 | n |
| 254 | } else { |
| 255 | 0 |
| 256 | }; |
| 257 | Ok(ResourceAmount::new(units, f)) |
| 258 | }) |
| 259 | } |
| 260 | |
| 261 | /// Parses an exact string. |
| 262 | /// Use this instead of `chomsky::text::keyword` or `chomsky::primitive::just` for better error |