package main

import (
	"fmt"
	"regexp"
	"strconv"
	"time"
)

var durationRe = regexp.MustCompile(`^(\d+(?:\.\d+)?)(ms|s|m)$`)

func parseDuration(s string) (time.Duration, error) {
	m := durationRe.FindStringSubmatch(s)
	if m == nil {
		return 0, fmt.Errorf("invalid duration: %s", s)
	}

	value, _ := strconv.ParseFloat(m[1], 64)
	switch m[2] {
	case "ms":
		return time.Duration(value * float64(time.Millisecond)), nil
	case "s":
		return time.Duration(value * float64(time.Second)), nil
	case "m":
		return time.Duration(value * float64(time.Minute)), nil
	default:
		return 0, fmt.Errorf("unknown unit: %s", m[2])
	}
}
