wip: ship calculator
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
package validator
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"fyne.io/fyne/v2"
|
||||
)
|
||||
|
||||
func NewStackValidator(first fyne.StringValidator, rest ...fyne.StringValidator) fyne.StringValidator {
|
||||
if first == nil {
|
||||
panic("first validator cannot be nil")
|
||||
}
|
||||
return func(s string) error {
|
||||
if err := first(s); err != nil {
|
||||
return err
|
||||
}
|
||||
for i := range rest {
|
||||
if err := rest[i](s); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func NewMutualValidator(other func() float64, valid func(float64) bool) fyne.StringValidator {
|
||||
if other == nil {
|
||||
panic("other value getter cannot be nil")
|
||||
}
|
||||
return func(s string) error {
|
||||
myValue, err := ParseFloat(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !valid(myValue) {
|
||||
return errors.New("invalid value")
|
||||
}
|
||||
if !valid(other()) {
|
||||
return errors.New("invalid other value")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func FloatValueValidator(s string) error {
|
||||
if _, err := ParseFloat(s); err != nil {
|
||||
return errors.New("not a float value")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ParseFloat(s string) (float64, error) {
|
||||
return strconv.ParseFloat(s, 64)
|
||||
}
|
||||
Reference in New Issue
Block a user