package convids import ( "errors" "fmt" "iter" "regexp" "slices" "strings" ) var ErrNoName = errors.New("missing name") var ErrNilPointer = errors.New("nil pointer") var animePattern = `^\[.+\] ` var animeRegexp = regexp.MustCompile(animePattern) func (s *Show) Match(path string) (bool, error) { if s == nil { return false, ErrNilPointer } if s.Pattern != "" { return s.matchRegexp(path) } if s.Name == "" { return false, ErrNoName } if s.Anime { if !animeRegexp.MatchString(path) { return false, nil } path = animeRegexp.ReplaceAllString(path, "") } return strings.HasPrefix(path, s.Name), nil } func (s *Show) matchRegexp(path string) (f bool, err error) { if s.re == nil { p := s.Pattern if s.Anime { p = animePattern + strings.TrimPrefix(p, "^") } else if !strings.HasPrefix(p, "^") { p = "^" + p } s.re, err = regexp.Compile(p) if err != nil { return } } return s.re.MatchString(path), nil } func (ss *Shows) All() iter.Seq[*Show] { if ss == nil { return func(func(*Show) bool) {} } return slices.Values(*ss) } func (d *Data) AllShows(silent bool) iter.Seq[*Show] { return func(yield func(*Show) bool) { if d == nil || d.Config == nil { return } for _, show := range d.Config.Shows { sh := (*d.Shows)[show] if sh == nil || len(*sh) < 1 { continue } if !silent { fmt.Printf("Checking %s shows\n\n", show) } for s := range sh.All() { if s == nil { continue } if !yield(s) { return } } } } }