aboutsummaryrefslogtreecommitdiff
path: root/internal/feed/filter/filter.go
blob: 8ff8a977a74d8479e7f55f29953e66ebd4922905 (plain)
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
package filter

import (
	"github.com/antonmedv/expr"
	"github.com/antonmedv/expr/vm"
	"github.com/mmcdole/gofeed"
)

type Filter struct {
	prog *vm.Program
}

func (f *Filter) Run(item *gofeed.Item) (bool, error) {
	if res, err := expr.Run(f.prog, item); err != nil {
		return false, err
	} else {
		return res.(bool), nil
	}
}

func New(s string) (*Filter, error) {
	prog, err := expr.Compile(s, expr.AsBool(), expr.Env(gofeed.Item{}))
	if err != nil {
		return nil, err
	}
	return &Filter{prog}, nil
}