Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

Commit 5a9ad0e

Browse files
committed
chore: code cleanup
1 parent bb80324 commit 5a9ad0e

File tree

5 files changed

+18
-21
lines changed

5 files changed

+18
-21
lines changed

rules/common/base.go

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package common
33
import (
44
"errors"
55

6+
C "github.com/metacubex/mihomo/constant"
7+
68
"golang.org/x/exp/slices"
79
)
810

@@ -38,3 +40,5 @@ func ParseParams(params []string) (isSrc bool, noResolve bool) {
3840
}
3941
return
4042
}
43+
44+
type ParseRuleFunc func(tp, payload, target string, params []string, subRules map[string][]C.Rule) (C.Rule, error)

rules/logic/logic.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ type Logic struct {
2323
payloadOnce sync.Once
2424
}
2525

26-
type ParseRuleFunc func(tp, payload, target string, params []string, subRules map[string][]C.Rule) (C.Rule, error)
27-
28-
func NewSubRule(payload, adapter string, subRules map[string][]C.Rule, parseRule ParseRuleFunc) (*Logic, error) {
26+
func NewSubRule(payload, adapter string, subRules map[string][]C.Rule, parseRule common.ParseRuleFunc) (*Logic, error) {
2927
logic := &Logic{Base: &common.Base{}, payload: payload, adapter: adapter, ruleType: C.SubRules, subRules: subRules}
3028
err := logic.parsePayload(fmt.Sprintf("(%s)", payload), parseRule)
3129
if err != nil {
@@ -38,7 +36,7 @@ func NewSubRule(payload, adapter string, subRules map[string][]C.Rule, parseRule
3836
return logic, nil
3937
}
4038

41-
func NewNOT(payload string, adapter string, parseRule ParseRuleFunc) (*Logic, error) {
39+
func NewNOT(payload string, adapter string, parseRule common.ParseRuleFunc) (*Logic, error) {
4240
logic := &Logic{Base: &common.Base{}, payload: payload, adapter: adapter, ruleType: C.NOT}
4341
err := logic.parsePayload(payload, parseRule)
4442
if err != nil {
@@ -51,7 +49,7 @@ func NewNOT(payload string, adapter string, parseRule ParseRuleFunc) (*Logic, er
5149
return logic, nil
5250
}
5351

54-
func NewOR(payload string, adapter string, parseRule ParseRuleFunc) (*Logic, error) {
52+
func NewOR(payload string, adapter string, parseRule common.ParseRuleFunc) (*Logic, error) {
5553
logic := &Logic{Base: &common.Base{}, payload: payload, adapter: adapter, ruleType: C.OR}
5654
err := logic.parsePayload(payload, parseRule)
5755
if err != nil {
@@ -60,7 +58,7 @@ func NewOR(payload string, adapter string, parseRule ParseRuleFunc) (*Logic, err
6058
return logic, nil
6159
}
6260

63-
func NewAND(payload string, adapter string, parseRule ParseRuleFunc) (*Logic, error) {
61+
func NewAND(payload string, adapter string, parseRule common.ParseRuleFunc) (*Logic, error) {
6462
logic := &Logic{Base: &common.Base{}, payload: payload, adapter: adapter, ruleType: C.AND}
6563
err := logic.parsePayload(payload, parseRule)
6664
if err != nil {
@@ -79,7 +77,7 @@ func (r Range) containRange(preStart, preEnd int) bool {
7977
return preStart < r.start && preEnd > r.end
8078
}
8179

82-
func (logic *Logic) payloadToRule(subPayload string, parseRule ParseRuleFunc) (C.Rule, error) {
80+
func (logic *Logic) payloadToRule(subPayload string, parseRule common.ParseRuleFunc) (C.Rule, error) {
8381
splitStr := strings.SplitN(subPayload, ",", 2)
8482
if len(splitStr) < 2 {
8583
return nil, fmt.Errorf("[%s] format is error", subPayload)
@@ -160,7 +158,7 @@ func (logic *Logic) findSubRuleRange(payload string, ruleRanges []Range) []Range
160158
return subRuleRange
161159
}
162160

163-
func (logic *Logic) parsePayload(payload string, parseRule ParseRuleFunc) error {
161+
func (logic *Logic) parsePayload(payload string, parseRule common.ParseRuleFunc) error {
164162
regex, err := regexp.Compile("\\(.*\\)")
165163
if err != nil {
166164
return err

rules/parser.go

+2
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,5 @@ func ParseRule(tp, payload, target string, params []string, subRules map[string]
9191

9292
return
9393
}
94+
95+
var _ RC.ParseRuleFunc = ParseRule

rules/provider/parse.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/metacubex/mihomo/component/resource"
1010
C "github.com/metacubex/mihomo/constant"
1111
P "github.com/metacubex/mihomo/constant/provider"
12+
"github.com/metacubex/mihomo/rules/common"
1213
)
1314

1415
var (
@@ -27,9 +28,7 @@ type ruleProviderSchema struct {
2728
Payload []string `provider:"payload,omitempty"`
2829
}
2930

30-
type parseRuleFunc func(tp, payload, target string, params []string, subRules map[string][]C.Rule) (parsed C.Rule, parseErr error)
31-
32-
func ParseRuleProvider(name string, mapping map[string]any, parse parseRuleFunc) (P.RuleProvider, error) {
31+
func ParseRuleProvider(name string, mapping map[string]any, parse common.ParseRuleFunc) (P.RuleProvider, error) {
3332
schema := &ruleProviderSchema{}
3433
decoder := structure.NewDecoder(structure.Option{TagName: "provider", WeaklyTypedInput: true})
3534
if err := decoder.Decode(mapping, schema); err != nil {

rules/provider/provider.go

+4-10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/metacubex/mihomo/component/resource"
1414
C "github.com/metacubex/mihomo/constant"
1515
P "github.com/metacubex/mihomo/constant/provider"
16+
"github.com/metacubex/mihomo/rules/common"
1617

1718
"gopkg.in/yaml.v3"
1819
)
@@ -132,9 +133,7 @@ func (rp *RuleSetProvider) Close() error {
132133
return rp.ruleSetProvider.Close()
133134
}
134135

135-
func NewRuleSetProvider(name string, behavior P.RuleBehavior, format P.RuleFormat, interval time.Duration, vehicle P.Vehicle,
136-
parse parseRuleFunc,
137-
) P.RuleProvider {
136+
func NewRuleSetProvider(name string, behavior P.RuleBehavior, format P.RuleFormat, interval time.Duration, vehicle P.Vehicle, parse common.ParseRuleFunc) P.RuleProvider {
138137
rp := &ruleSetProvider{
139138
ruleSetProviderBase: ruleSetProviderBase{
140139
behavior: behavior,
@@ -160,7 +159,7 @@ func NewRuleSetProvider(name string, behavior P.RuleBehavior, format P.RuleForma
160159
return wrapper
161160
}
162161

163-
func newStrategy(behavior P.RuleBehavior, parse parseRuleFunc) ruleStrategy {
162+
func newStrategy(behavior P.RuleBehavior, parse common.ParseRuleFunc) ruleStrategy {
164163
switch behavior {
165164
case P.Domain:
166165
strategy := NewDomainStrategy()
@@ -324,12 +323,7 @@ func (i *inlineProvider) MarshalJSON() ([]byte, error) {
324323
})
325324
}
326325

327-
func newInlineProvider(
328-
name string,
329-
behavior P.RuleBehavior,
330-
payload []string,
331-
parse parseRuleFunc,
332-
) P.RuleProvider {
326+
func newInlineProvider(name string, behavior P.RuleBehavior, payload []string, parse common.ParseRuleFunc) P.RuleProvider {
333327
rp := &inlineProvider{
334328
ruleSetProviderBase: ruleSetProviderBase{
335329
behavior: behavior,

0 commit comments

Comments
 (0)