aboutsummaryrefslogtreecommitdiffstats
path: root/mass/mass.go
blob: 46ce33e2199dbae9ea2fdfcb133730f183e0dc69 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package mass

import (
	"errors"
	"fmt"
)

type Mass float32

const (
	Gram     Mass = 1
	Kilogram Mass = 1_000
	Pound    Mass = 453.5924
)

type FlowRate float32

const (
	KilogramsPerSecond FlowRate = 1
	PoundsPerMinute    FlowRate = 0.007_559_872_833
)

// FlowRateUnitStrings returns a slice of strings, each representing a
// flowRate unit.
// This is necessary because giu.Combo only works with strings.
func FlowRateUnitStrings() []string {
	return []string{"kg/s", "lb/min"}
}

const (
	DefaultFlowRateUnit FlowRate = KilogramsPerSecond
	// DefaultFlowRateUnitIndex is used to index FlowRateUnitStrings()
	DefaultFlowRateUnitIndex int32 = 0 // kg/s
)

func FlowRateUnitFromString(s string) (FlowRate, error) {
	// Each case corresponds to a value in FlowRateUnitStrings().
	switch s {
	case "kg/s":
		return KilogramsPerSecond, nil
	case "lb/min":
		return PoundsPerMinute, nil
	default:
		return *new(FlowRate), errors.New(
			fmt.Sprintf("invalid mass flow rate unit: '%s'", s))
	}
}