parseDurationSpec function

Duration parseDurationSpec(
  1. String raw
)

Parses a single-unit duration string — '3600s', '5m', '24h' — into a Duration. Compounds ('1h30m') and unknown units are rejected with FormatException.

Implementation

Duration parseDurationSpec(String raw) {
  final match = _durationPattern.firstMatch(raw.trim());
  if (match == null) {
    throw FormatException(
      'invalid duration "$raw" — expected a single-unit spec like 3600s, '
      '5m or 24h',
    );
  }
  final amount = int.parse(match.group(1)!);
  return switch (match.group(2)!) {
    's' => Duration(seconds: amount),
    'm' => Duration(minutes: amount),
    _ => Duration(hours: amount),
  };
}