Clash.Meta/tunnel/mode.go

66 lines
1.1 KiB
Go
Raw Normal View History

2018-11-21 13:47:46 +08:00
package tunnel
import (
"encoding/json"
"errors"
)
type TunnelMode int
2018-11-21 13:47:46 +08:00
var (
// ModeMapping is a mapping for Mode enum
ModeMapping = map[string]TunnelMode{
2018-12-05 21:13:29 +08:00
Global.String(): Global,
Rule.String(): Rule,
Direct.String(): Direct,
2018-11-21 13:47:46 +08:00
}
)
const (
Global TunnelMode = iota
2018-11-21 13:47:46 +08:00
Rule
Direct
)
// UnmarshalJSON unserialize Mode
func (m *TunnelMode) UnmarshalJSON(data []byte) error {
2018-11-21 13:47:46 +08:00
var tp string
2018-11-28 10:38:30 +08:00
json.Unmarshal(data, &tp)
2018-11-21 13:47:46 +08:00
mode, exist := ModeMapping[tp]
if !exist {
return errors.New("invalid mode")
}
*m = mode
return nil
}
2018-12-05 21:13:29 +08:00
// UnmarshalYAML unserialize Mode with yaml
func (m *TunnelMode) UnmarshalYAML(unmarshal func(interface{}) error) error {
2018-12-05 21:13:29 +08:00
var tp string
unmarshal(&tp)
mode, exist := ModeMapping[tp]
if !exist {
return errors.New("invalid mode")
}
*m = mode
return nil
}
2018-11-21 13:47:46 +08:00
// MarshalJSON serialize Mode
func (m TunnelMode) MarshalJSON() ([]byte, error) {
2018-11-21 13:47:46 +08:00
return json.Marshal(m.String())
}
func (m TunnelMode) String() string {
2018-11-21 13:47:46 +08:00
switch m {
case Global:
return "Global"
case Rule:
return "Rule"
case Direct:
return "Direct"
default:
2019-07-29 10:12:10 +08:00
return "Unknown"
2018-11-21 13:47:46 +08:00
}
}