40 lines
585 B
Go
40 lines
585 B
Go
package x
|
|
|
|
import (
|
|
"errors"
|
|
)
|
|
|
|
func Assert(cond bool, msg string) {
|
|
if cond {
|
|
panic(errors.New(msg))
|
|
}
|
|
}
|
|
|
|
func Check(cond bool, err error) *invariants {
|
|
return new(invariants).Check(cond, err)
|
|
}
|
|
|
|
type invariants struct {
|
|
errs []error
|
|
}
|
|
|
|
func (i *invariants) Check(cond bool, err error) *invariants {
|
|
if !cond {
|
|
i.errs = append(i.errs, err)
|
|
}
|
|
return i
|
|
}
|
|
|
|
func (i *invariants) Join() error {
|
|
return errors.Join(i.errs...)
|
|
}
|
|
func (i *invariants) First() error {
|
|
if len(i.errs) > 0 {
|
|
return i.errs[0]
|
|
}
|
|
return nil
|
|
}
|
|
func (i *invariants) All() []error {
|
|
return i.errs
|
|
}
|