parse the string into a vector of interval components i.e. (amount, unit) tuples
(
value: &str,
config: &IntervalParseConfig,
)
| 1455 | |
| 1456 | /// parse the string into a vector of interval components i.e. (amount, unit) tuples |
| 1457 | fn parse_interval_components( |
| 1458 | value: &str, |
| 1459 | config: &IntervalParseConfig, |
| 1460 | ) -> Result<Vec<(IntervalAmount, IntervalUnit)>, ArrowError> { |
| 1461 | let raw_pairs = split_interval_components(value); |
| 1462 | |
| 1463 | // parse amounts and units |
| 1464 | let Ok(pairs): Result<Vec<(IntervalAmount, IntervalUnit)>, ArrowError> = raw_pairs |
| 1465 | .iter() |
| 1466 | .map(|(a, u)| Ok((a.parse()?, IntervalUnit::from_str_or_config(*u, config)?))) |
| 1467 | .collect() |
| 1468 | else { |
| 1469 | return Err(ArrowError::ParseError(format!( |
| 1470 | "Invalid input syntax for type interval: {value:?}" |
| 1471 | ))); |
| 1472 | }; |
| 1473 | |
| 1474 | // collect parsed results |
| 1475 | let (amounts, units): (Vec<_>, Vec<_>) = pairs.into_iter().unzip(); |
| 1476 | |
| 1477 | // duplicate units? |
| 1478 | let mut observed_interval_types = 0; |
| 1479 | for (unit, (_, raw_unit)) in units.iter().zip(raw_pairs) { |
| 1480 | if observed_interval_types & (*unit as u16) != 0 { |
| 1481 | return Err(ArrowError::ParseError(format!( |
| 1482 | "Invalid input syntax for type interval: {:?}. Repeated type '{}'", |
| 1483 | value, |
| 1484 | raw_unit.unwrap_or_default(), |
| 1485 | ))); |
| 1486 | } |
| 1487 | |
| 1488 | observed_interval_types |= *unit as u16; |
| 1489 | } |
| 1490 | |
| 1491 | let result = amounts.iter().copied().zip(units.iter().copied()); |
| 1492 | |
| 1493 | Ok(result.collect::<Vec<_>>()) |
| 1494 | } |
| 1495 | |
| 1496 | /// Split an interval into a vec of amounts and units. |
| 1497 | /// |