aboutsummaryrefslogtreecommitdiff
path: root/internal/feed/filter/filter.go
blob: bdf59930b30ee54e382e0135bbf0fe32d2aba006 (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/Necoro/gofeed"
	"github.com/expr-lang/expr"
	"github.com/expr-lang/expr/vm"
)

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
}