-
Notifications
You must be signed in to change notification settings - Fork 0
/
lenmismatch.go
39 lines (33 loc) · 944 Bytes
/
lenmismatch.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
37
38
39
package lerr
import (
"fmt"
)
// ErrLenMismatch represents a mis-matched length.
type ErrLenMismatch struct {
Expected, Actual int
}
// LenMismatch returns an ErrLenMismatch.
func LenMismatch(expected, actual int) ErrLenMismatch {
return ErrLenMismatch{
Expected: expected,
Actual: actual,
}
}
// Error fulfills the error interface.
func (e ErrLenMismatch) Error() string {
return fmt.Sprintf("Lengths do not match: Expected %v got %v", e.Expected, e.Actual)
}
// NewLenMismatch will return a nil error if expected and actual are equal and
// an instance of LenMismatch if they are different. The min and max will be set
// to the smaller and larger of the values passed in respectivly.
func NewLenMismatch(expected, actual int) (min, max int, err error) {
min, max = expected, actual
if expected == actual {
return
}
if actual < expected {
min, max = actual, expected
}
err = LenMismatch(expected, actual)
return
}