mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2025-12-19 16:30:07 +08:00
50 lines
850 B
Go
50 lines
850 B
Go
package tunnel
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
)
|
|
|
|
type TunnelStatus int32
|
|
|
|
// StatusMapping is a mapping for Status enum
|
|
var StatusMapping = map[string]TunnelStatus{
|
|
Suspend.String(): Suspend,
|
|
Inner.String(): Inner,
|
|
Running.String(): Running,
|
|
}
|
|
|
|
const (
|
|
Suspend TunnelStatus = iota
|
|
Inner
|
|
Running
|
|
)
|
|
|
|
// UnmarshalText unserialize Status
|
|
func (s *TunnelStatus) UnmarshalText(data []byte) error {
|
|
status, exist := StatusMapping[strings.ToLower(string(data))]
|
|
if !exist {
|
|
return errors.New("invalid status")
|
|
}
|
|
*s = status
|
|
return nil
|
|
}
|
|
|
|
// MarshalText serialize Status
|
|
func (s TunnelStatus) MarshalText() ([]byte, error) {
|
|
return []byte(s.String()), nil
|
|
}
|
|
|
|
func (s TunnelStatus) String() string {
|
|
switch s {
|
|
case Suspend:
|
|
return "suspend"
|
|
case Inner:
|
|
return "inner"
|
|
case Running:
|
|
return "running"
|
|
default:
|
|
return "Unknown"
|
|
}
|
|
}
|