-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_length.go
43 lines (36 loc) · 958 Bytes
/
check_length.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
40
41
42
43
package bynom
import "context"
// RequireLen runs all parsers noms and if the finished without errors tests
// if the amount of bytes they "ate" equals n.
// If the amount of bytes does not equal n the function will return ErrRequirementNotMet.
func RequireLen(n int, noms ...Nom) Nom {
const funcName = "RequireLen"
return func(ctx context.Context, p Plate) (err error) {
var startPos int
if startPos, err = p.TellPosition(ctx); err != nil {
return WrapBreadcrumb(err, funcName, -1)
}
for _, nom := range noms {
if err = nom(ctx, p); err != nil {
return WrapBreadcrumb(err, funcName, -1)
}
}
var endPos int
if endPos, err = p.TellPosition(ctx); err != nil {
return WrapBreadcrumb(err, funcName, -1)
}
var l = endPos - startPos
if l != n {
return WrapBreadcrumb(
ErrRequirementNotMet{
Expected: n,
Have: l,
Msg: "invalid length",
},
funcName,
-1,
)
}
return
}
}