93 lines
1.9 KiB
Go
93 lines
1.9 KiB
Go
package dimension
|
|
|
|
import (
|
|
"git.loyso.art/frx/eway/internal/config"
|
|
"git.loyso.art/frx/eway/internal/matcher"
|
|
)
|
|
|
|
type MatchResult uint8
|
|
|
|
const (
|
|
MatchResultMiss MatchResult = iota
|
|
MatchResultLength
|
|
MatchResultHeight
|
|
MatchResultWidth
|
|
MatchResultDepth
|
|
)
|
|
|
|
type Matcher struct {
|
|
length matcher.Unit
|
|
width matcher.Unit
|
|
height matcher.Unit
|
|
}
|
|
|
|
func New(cfg config.DimensionMatcher) *Matcher {
|
|
return &Matcher{
|
|
length: makeMatcherByConig(cfg.CaseInsensitive, cfg.Length...),
|
|
width: makeMatcherByConig(cfg.CaseInsensitive, cfg.Width...),
|
|
height: makeMatcherByConig(cfg.CaseInsensitive, cfg.Height...),
|
|
}
|
|
}
|
|
|
|
func makeMatcherByConig(insensitive bool, cfgs ...config.MatcherPredicate) matcher.Unit {
|
|
opts := make([]matcher.RadixOpt, 0, 1)
|
|
if insensitive {
|
|
opts = append(opts, matcher.RadixCaseInsensitive())
|
|
}
|
|
m := matcher.NewRadix(opts...)
|
|
|
|
for _, cfg := range cfgs {
|
|
switch cfg.Type {
|
|
case config.MatcherPredicateTypePattern:
|
|
m.Register(cfg.Value)
|
|
case config.MatcherPredicateTypeRegexp:
|
|
m.RegisterRegexp(cfg.Value)
|
|
default:
|
|
panic("unsupported matcher type")
|
|
}
|
|
}
|
|
|
|
return m
|
|
}
|
|
|
|
func (m *Matcher) Match(value string) MatchResult {
|
|
switch {
|
|
case value == "Высота":
|
|
fallthrough
|
|
case m.height.Match(value):
|
|
return MatchResultHeight
|
|
case value == "Глубина":
|
|
return MatchResultDepth
|
|
case value == "Длина":
|
|
fallthrough
|
|
case m.length.Match(value):
|
|
return MatchResultLength
|
|
case value == "Ширина":
|
|
fallthrough
|
|
case m.width.Match(value):
|
|
return MatchResultWidth
|
|
}
|
|
|
|
return MatchResultMiss
|
|
}
|
|
|
|
func (m *Matcher) GetRegisteredPatterns() map[string][]string {
|
|
out := map[string][]string{
|
|
"length": nil,
|
|
"width": nil,
|
|
"height": nil,
|
|
}
|
|
|
|
if m.height != nil {
|
|
out["height"] = m.height.Patterns()
|
|
}
|
|
if m.width != nil {
|
|
out["width"] = m.width.Patterns()
|
|
}
|
|
if m.length != nil {
|
|
out["length"] = m.length.Patterns()
|
|
}
|
|
|
|
return out
|
|
}
|