forked from fossabot/clash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmode.go
71 lines (62 loc) · 1.24 KB
/
mode.go
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package tunnel
import (
"encoding/json"
"errors"
"strings"
)
type TunnelMode int
var (
// ModeMapping is a mapping for Mode enum
ModeMapping = map[string]TunnelMode{
Global.String(): Global,
Rule.String(): Rule,
Direct.String(): Direct,
}
)
const (
Global TunnelMode = iota
Rule
Direct
)
// UnmarshalJSON unserialize Mode
func (m *TunnelMode) UnmarshalJSON(data []byte) error {
var tp string
json.Unmarshal(data, &tp)
mode, exist := ModeMapping[strings.ToLower(tp)]
if !exist {
return errors.New("invalid mode")
}
*m = mode
return nil
}
// UnmarshalYAML unserialize Mode with yaml
func (m *TunnelMode) UnmarshalYAML(unmarshal func(interface{}) error) error {
var tp string
unmarshal(&tp)
mode, exist := ModeMapping[strings.ToLower(tp)]
if !exist {
return errors.New("invalid mode")
}
*m = mode
return nil
}
// MarshalJSON serialize Mode
func (m TunnelMode) MarshalJSON() ([]byte, error) {
return json.Marshal(m.String())
}
// MarshalYAML serialize TunnelMode with yaml
func (m TunnelMode) MarshalYAML() (interface{}, error) {
return m.String(), nil
}
func (m TunnelMode) String() string {
switch m {
case Global:
return "global"
case Rule:
return "rule"
case Direct:
return "direct"
default:
return "Unknown"
}
}