chore: code cleanup
Some checks are pending
Trigger CMFA Update / trigger-CMFA-update (push) Waiting to run

This commit is contained in:
wwqgtxx 2024-12-19 09:29:17 +08:00
parent bb803249fa
commit 5a9ad0ed3c
5 changed files with 18 additions and 21 deletions

View File

@ -3,6 +3,8 @@ package common
import ( import (
"errors" "errors"
C "github.com/metacubex/mihomo/constant"
"golang.org/x/exp/slices" "golang.org/x/exp/slices"
) )
@ -38,3 +40,5 @@ func ParseParams(params []string) (isSrc bool, noResolve bool) {
} }
return return
} }
type ParseRuleFunc func(tp, payload, target string, params []string, subRules map[string][]C.Rule) (C.Rule, error)

View File

@ -23,9 +23,7 @@ type Logic struct {
payloadOnce sync.Once payloadOnce sync.Once
} }
type ParseRuleFunc func(tp, payload, target string, params []string, subRules map[string][]C.Rule) (C.Rule, error) func NewSubRule(payload, adapter string, subRules map[string][]C.Rule, parseRule common.ParseRuleFunc) (*Logic, error) {
func NewSubRule(payload, adapter string, subRules map[string][]C.Rule, parseRule ParseRuleFunc) (*Logic, error) {
logic := &Logic{Base: &common.Base{}, payload: payload, adapter: adapter, ruleType: C.SubRules, subRules: subRules} logic := &Logic{Base: &common.Base{}, payload: payload, adapter: adapter, ruleType: C.SubRules, subRules: subRules}
err := logic.parsePayload(fmt.Sprintf("(%s)", payload), parseRule) err := logic.parsePayload(fmt.Sprintf("(%s)", payload), parseRule)
if err != nil { if err != nil {
@ -38,7 +36,7 @@ func NewSubRule(payload, adapter string, subRules map[string][]C.Rule, parseRule
return logic, nil return logic, nil
} }
func NewNOT(payload string, adapter string, parseRule ParseRuleFunc) (*Logic, error) { func NewNOT(payload string, adapter string, parseRule common.ParseRuleFunc) (*Logic, error) {
logic := &Logic{Base: &common.Base{}, payload: payload, adapter: adapter, ruleType: C.NOT} logic := &Logic{Base: &common.Base{}, payload: payload, adapter: adapter, ruleType: C.NOT}
err := logic.parsePayload(payload, parseRule) err := logic.parsePayload(payload, parseRule)
if err != nil { if err != nil {
@ -51,7 +49,7 @@ func NewNOT(payload string, adapter string, parseRule ParseRuleFunc) (*Logic, er
return logic, nil return logic, nil
} }
func NewOR(payload string, adapter string, parseRule ParseRuleFunc) (*Logic, error) { func NewOR(payload string, adapter string, parseRule common.ParseRuleFunc) (*Logic, error) {
logic := &Logic{Base: &common.Base{}, payload: payload, adapter: adapter, ruleType: C.OR} logic := &Logic{Base: &common.Base{}, payload: payload, adapter: adapter, ruleType: C.OR}
err := logic.parsePayload(payload, parseRule) err := logic.parsePayload(payload, parseRule)
if err != nil { if err != nil {
@ -60,7 +58,7 @@ func NewOR(payload string, adapter string, parseRule ParseRuleFunc) (*Logic, err
return logic, nil return logic, nil
} }
func NewAND(payload string, adapter string, parseRule ParseRuleFunc) (*Logic, error) { func NewAND(payload string, adapter string, parseRule common.ParseRuleFunc) (*Logic, error) {
logic := &Logic{Base: &common.Base{}, payload: payload, adapter: adapter, ruleType: C.AND} logic := &Logic{Base: &common.Base{}, payload: payload, adapter: adapter, ruleType: C.AND}
err := logic.parsePayload(payload, parseRule) err := logic.parsePayload(payload, parseRule)
if err != nil { if err != nil {
@ -79,7 +77,7 @@ func (r Range) containRange(preStart, preEnd int) bool {
return preStart < r.start && preEnd > r.end return preStart < r.start && preEnd > r.end
} }
func (logic *Logic) payloadToRule(subPayload string, parseRule ParseRuleFunc) (C.Rule, error) { func (logic *Logic) payloadToRule(subPayload string, parseRule common.ParseRuleFunc) (C.Rule, error) {
splitStr := strings.SplitN(subPayload, ",", 2) splitStr := strings.SplitN(subPayload, ",", 2)
if len(splitStr) < 2 { if len(splitStr) < 2 {
return nil, fmt.Errorf("[%s] format is error", subPayload) return nil, fmt.Errorf("[%s] format is error", subPayload)
@ -160,7 +158,7 @@ func (logic *Logic) findSubRuleRange(payload string, ruleRanges []Range) []Range
return subRuleRange return subRuleRange
} }
func (logic *Logic) parsePayload(payload string, parseRule ParseRuleFunc) error { func (logic *Logic) parsePayload(payload string, parseRule common.ParseRuleFunc) error {
regex, err := regexp.Compile("\\(.*\\)") regex, err := regexp.Compile("\\(.*\\)")
if err != nil { if err != nil {
return err return err

View File

@ -91,3 +91,5 @@ func ParseRule(tp, payload, target string, params []string, subRules map[string]
return return
} }
var _ RC.ParseRuleFunc = ParseRule

View File

@ -9,6 +9,7 @@ import (
"github.com/metacubex/mihomo/component/resource" "github.com/metacubex/mihomo/component/resource"
C "github.com/metacubex/mihomo/constant" C "github.com/metacubex/mihomo/constant"
P "github.com/metacubex/mihomo/constant/provider" P "github.com/metacubex/mihomo/constant/provider"
"github.com/metacubex/mihomo/rules/common"
) )
var ( var (
@ -27,9 +28,7 @@ type ruleProviderSchema struct {
Payload []string `provider:"payload,omitempty"` Payload []string `provider:"payload,omitempty"`
} }
type parseRuleFunc func(tp, payload, target string, params []string, subRules map[string][]C.Rule) (parsed C.Rule, parseErr error) func ParseRuleProvider(name string, mapping map[string]any, parse common.ParseRuleFunc) (P.RuleProvider, error) {
func ParseRuleProvider(name string, mapping map[string]any, parse parseRuleFunc) (P.RuleProvider, error) {
schema := &ruleProviderSchema{} schema := &ruleProviderSchema{}
decoder := structure.NewDecoder(structure.Option{TagName: "provider", WeaklyTypedInput: true}) decoder := structure.NewDecoder(structure.Option{TagName: "provider", WeaklyTypedInput: true})
if err := decoder.Decode(mapping, schema); err != nil { if err := decoder.Decode(mapping, schema); err != nil {

View File

@ -13,6 +13,7 @@ import (
"github.com/metacubex/mihomo/component/resource" "github.com/metacubex/mihomo/component/resource"
C "github.com/metacubex/mihomo/constant" C "github.com/metacubex/mihomo/constant"
P "github.com/metacubex/mihomo/constant/provider" P "github.com/metacubex/mihomo/constant/provider"
"github.com/metacubex/mihomo/rules/common"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
@ -132,9 +133,7 @@ func (rp *RuleSetProvider) Close() error {
return rp.ruleSetProvider.Close() return rp.ruleSetProvider.Close()
} }
func NewRuleSetProvider(name string, behavior P.RuleBehavior, format P.RuleFormat, interval time.Duration, vehicle P.Vehicle, func NewRuleSetProvider(name string, behavior P.RuleBehavior, format P.RuleFormat, interval time.Duration, vehicle P.Vehicle, parse common.ParseRuleFunc) P.RuleProvider {
parse parseRuleFunc,
) P.RuleProvider {
rp := &ruleSetProvider{ rp := &ruleSetProvider{
ruleSetProviderBase: ruleSetProviderBase{ ruleSetProviderBase: ruleSetProviderBase{
behavior: behavior, behavior: behavior,
@ -160,7 +159,7 @@ func NewRuleSetProvider(name string, behavior P.RuleBehavior, format P.RuleForma
return wrapper return wrapper
} }
func newStrategy(behavior P.RuleBehavior, parse parseRuleFunc) ruleStrategy { func newStrategy(behavior P.RuleBehavior, parse common.ParseRuleFunc) ruleStrategy {
switch behavior { switch behavior {
case P.Domain: case P.Domain:
strategy := NewDomainStrategy() strategy := NewDomainStrategy()
@ -324,12 +323,7 @@ func (i *inlineProvider) MarshalJSON() ([]byte, error) {
}) })
} }
func newInlineProvider( func newInlineProvider(name string, behavior P.RuleBehavior, payload []string, parse common.ParseRuleFunc) P.RuleProvider {
name string,
behavior P.RuleBehavior,
payload []string,
parse parseRuleFunc,
) P.RuleProvider {
rp := &inlineProvider{ rp := &inlineProvider{
ruleSetProviderBase: ruleSetProviderBase{ ruleSetProviderBase: ruleSetProviderBase{
behavior: behavior, behavior: behavior,