aboutsummaryrefslogtreecommitdiffstats
path: root/volume.go
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2024-01-20 17:56:39 -0500
committerSam Anthony <sam@samanthony.xyz>2024-01-20 17:56:39 -0500
commit3aaae870ba76d8f0907b10a61d829ad353936306 (patch)
tree8be8cb950d0aa8039cd977c72e2f9438cae4a1e8 /volume.go
parentdb183cf7570e0f4e448ab5ced0ae41969261a815 (diff)
downloadvolute-3aaae870ba76d8f0907b10a61d829ad353936306.zip
flatten source directory structure by removing modules
Diffstat (limited to 'volume.go')
-rw-r--r--volume.go33
1 files changed, 33 insertions, 0 deletions
diff --git a/volume.go b/volume.go
new file mode 100644
index 0000000..6f5fb31
--- /dev/null
+++ b/volume.go
@@ -0,0 +1,33 @@
+package main
+
+import (
+ "errors"
+ "fmt"
+)
+
+type Volume float32
+
+const (
+ CubicCentimetre Volume = 1
+ Litre Volume = 1_000
+ CubicMetre Volume = 1_000_000
+ CubicInch Volume = 16.38706
+)
+
+var VolumeUnits = []string{"cc", "L", "m³", "in³"}
+
+func ParseVolumeUnit(s string) (Volume, error) {
+ // Each case corresponds to a value in VolumeUnits.
+ switch s {
+ case "cc":
+ return CubicCentimetre, nil
+ case "L":
+ return Litre, nil
+ case "m³":
+ return CubicMetre, nil
+ case "in³":
+ return CubicInch, nil
+ default:
+ return *new(Volume), errors.New(fmt.Sprintf("invalid volume unit: '%s'", s))
+ }
+}