parse_duration

Function parse_duration 

Source
pub fn parse_duration(duration_str: &str) -> Result<Duration>
Expand description

Parse a Go-style duration string into a Rust Duration.

Supported units:

  • h (hours): “720h” = 30 days
  • d (days): “30d” = 30 days
  • w (weeks): “4w” = 28 days

§Constraints

  • Minimum: 1 hour (MIN_ROTATION_INTERVAL_HOURS)
  • Maximum: 8760 hours / 365 days (MAX_ROTATION_INTERVAL_HOURS)

These bounds ensure keys are rotated frequently enough for security compliance but not so frequently as to cause operational issues.

§Examples

use bindy::bind9::duration::parse_duration;
use std::time::Duration;

// Parse hours
assert_eq!(parse_duration("24h").unwrap(), Duration::from_secs(86400));

// Parse days
assert_eq!(parse_duration("30d").unwrap(), Duration::from_secs(2_592_000));

// Parse weeks
assert_eq!(parse_duration("4w").unwrap(), Duration::from_secs(2_419_200));

// Invalid formats return errors
assert!(parse_duration("").is_err());
assert!(parse_duration("10").is_err());  // Missing unit
assert!(parse_duration("10x").is_err()); // Invalid unit

§Errors

Returns an error if:

  • The format is invalid (missing unit, non-numeric value)
  • The duration is below the minimum (1h)
  • The duration is above the maximum (8760h / 365d / 52w)

§Panics

May panic if the MIN_ROTATION_INTERVAL_HOURS or MAX_ROTATION_INTERVAL_HOURS constants overflow when multiplied by SECONDS_PER_HOUR. This should never happen with the current constant values.