MCPcopy Create free account
hub / github.com/It4innovations/hyperqueue / parse_resource_amount

Function parse_resource_amount

crates/hyperqueue/src/common/parser2.rs:241–259  ·  view source on GitHub ↗

Parse int32 or float with FRACTIONS_MAX_DIGITS precision (e.g. "2", "3.3", "0.0001")

()

Source from the content-addressed store, hash-verified

239
240/// Parse int32 or float with FRACTIONS_MAX_DIGITS precision (e.g. "2", "3.3", "0.0001")
241pub 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

Callers 3

p_allocation_requestFunction · 0.85
parse_resource_sumFunction · 0.85

Calls 3

parse_u32Function · 0.85
parse_integer_stringFunction · 0.85
lenMethod · 0.45

Tested by 1