clash/tunnel/mode.go

66 lines
1 KiB
Go
Raw Normal View History

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