-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
36 lines (31 loc) · 941 Bytes
/
utils.go
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
package manifold
import (
"cmp"
"fmt"
)
// mustOk returns a function that enforces a successful operation (`ok` is true).
// If `ok` is false, it panics with the given `okContext`.
func mustOk[T any](okContext string) func(a T, ok bool) T {
return func(a T, ok bool) T {
if !ok {
panic(fmt.Errorf("mustOk: %s", okContext))
}
return a
}
}
// checkOneOf checks if the value is in the allowed set of values.
func checkOneOf[T comparable](value T, allowed ...T) error {
for _, a := range allowed {
if value == a {
return nil
}
}
return fmt.Errorf("invalid value: %v, allowed values are: %v", value, allowed)
}
// checkInRange checks if a value is within the allowed range [min, max] and returns a custom error if not.
func checkInRange[T cmp.Ordered](value, min, max T) error {
if value < min || value > max {
return fmt.Errorf("invalid value: %v, must be within range [%v, %v]", value, min, max)
}
return nil
}