refactor: rule package

This commit is contained in:
krau
2025-08-23 16:14:12 +08:00
parent 94f796d0e8
commit 7300e54c40
8 changed files with 14 additions and 27 deletions

View File

@@ -11,7 +11,7 @@ import (
"github.com/duke-git/lancet/v2/slice"
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/msgelem"
"github.com/krau/SaveAny-Bot/database"
"github.com/krau/SaveAny-Bot/pkg/enums/rule"
"github.com/krau/SaveAny-Bot/pkg/rule"
)
func handleRuleCmd(ctx *ext.Context, update *ext.Update) error {

View File

@@ -7,8 +7,6 @@ import (
"github.com/charmbracelet/log"
"github.com/krau/SaveAny-Bot/database"
"github.com/krau/SaveAny-Bot/pkg/consts"
ruleenum "github.com/krau/SaveAny-Bot/pkg/enums/rule"
"github.com/krau/SaveAny-Bot/pkg/rule"
"github.com/krau/SaveAny-Bot/pkg/tfile"
)
@@ -37,7 +35,7 @@ func (m matchedStorName) String() string {
// can we use this storage name directly?
func (m matchedStorName) IsUsable() bool {
return m != "" && m != consts.RuleStorNameChosen
return m != "" && m != rule.RuleStorNameChosen
}
type MatchedDirPath string
@@ -47,7 +45,7 @@ func (m MatchedDirPath) String() string {
}
func (m MatchedDirPath) NeedNewForAlbum() bool {
return m != "" && m == consts.RuleDirPathNewForAlbum
return m != "" && m == rule.RuleDirPathNewForAlbum
}
func ApplyRule(ctx context.Context, rules []database.Rule, inputs *ruleInput) (matchedStorageName matchedStorName, dirPath MatchedDirPath) {
@@ -57,7 +55,7 @@ func ApplyRule(ctx context.Context, rules []database.Rule, inputs *ruleInput) (m
logger := log.FromContext(ctx)
for _, ur := range rules {
switch ur.Type {
case ruleenum.FileNameRegex.String():
case rule.FileNameRegex.String():
ru, err := rule.NewRuleFileNameRegex(ur.StorageName, ur.DirPath, ur.Data)
if err != nil {
logger.Errorf("Failed to create rule: %s", err)
@@ -72,7 +70,7 @@ func ApplyRule(ctx context.Context, rules []database.Rule, inputs *ruleInput) (m
dirPath = MatchedDirPath(ru.StoragePath())
matchedStorageName = matchedStorName(ru.StorageName())
}
case ruleenum.MessageRegex.String():
case rule.MessageRegex.String():
ru, err := rule.NewRuleMessageRegex(ur.StorageName, ur.DirPath, ur.Data)
if err != nil {
logger.Errorf("Failed to create rule: %s", err)
@@ -87,7 +85,7 @@ func ApplyRule(ctx context.Context, rules []database.Rule, inputs *ruleInput) (m
dirPath = MatchedDirPath(ru.StoragePath())
matchedStorageName = matchedStorName(ru.StorageName())
}
case ruleenum.IsAlbum.String():
case rule.IsAlbum.String():
matchAlbum, err := convertor.ToBool(ur.Data)
if err != nil {
matchAlbum = false

View File

@@ -1,4 +1,4 @@
package consts
package rule
const (
RuleStorNameChosen = "CHOSEN"

View File

@@ -3,7 +3,6 @@ package rule
import (
"regexp"
ruleenum "github.com/krau/SaveAny-Bot/pkg/enums/rule"
"github.com/krau/SaveAny-Bot/pkg/tfile"
)
@@ -14,8 +13,8 @@ type RuleFileNameRegex struct {
var _ RuleClass[tfile.TGFile] = (*RuleFileNameRegex)(nil)
func (r RuleFileNameRegex) Type() ruleenum.RuleType {
return ruleenum.FileNameRegex
func (r RuleFileNameRegex) Type() RuleType {
return FileNameRegex
}
func (r RuleFileNameRegex) Match(input tfile.TGFile) (bool, error) {

View File

@@ -1,9 +1,5 @@
package rule
import (
ruleenum "github.com/krau/SaveAny-Bot/pkg/enums/rule"
)
var _ RuleClass[bool] = (*RuleMediaType)(nil)
type RuleMediaType struct {
@@ -11,8 +7,8 @@ type RuleMediaType struct {
matchAlbum bool
}
func (r RuleMediaType) Type() ruleenum.RuleType {
return ruleenum.IsAlbum
func (r RuleMediaType) Type() RuleType {
return IsAlbum
}
func (r RuleMediaType) Match(input bool) (bool, error) {

View File

@@ -2,8 +2,6 @@ package rule
import (
"regexp"
ruleenum "github.com/krau/SaveAny-Bot/pkg/enums/rule"
)
var _ RuleClass[string] = (*RuleMessageRegex)(nil)
@@ -13,8 +11,8 @@ type RuleMessageRegex struct {
regex *regexp.Regexp
}
func (r RuleMessageRegex) Type() ruleenum.RuleType {
return ruleenum.MessageRegex
func (r RuleMessageRegex) Type() RuleType {
return MessageRegex
}
func (r RuleMessageRegex) Match(input string) (bool, error) {

View File

@@ -1,11 +1,7 @@
package rule
import (
ruleenum "github.com/krau/SaveAny-Bot/pkg/enums/rule"
)
type RuleClass[InputType any] interface {
Type() ruleenum.RuleType
Type() RuleType
Match(input InputType) (bool, error)
StorageName() string
StoragePath() string