github.com/metacubex/mihomo@v1.18.5/component/process/find_process_mode.go (about)

     1  package process
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"strings"
     7  )
     8  
     9  const (
    10  	FindProcessAlways = "always"
    11  	FindProcessStrict = "strict"
    12  	FindProcessOff    = "off"
    13  )
    14  
    15  var (
    16  	validModes = map[string]struct{}{
    17  		FindProcessAlways: {},
    18  		FindProcessOff:    {},
    19  		FindProcessStrict: {},
    20  	}
    21  )
    22  
    23  type FindProcessMode string
    24  
    25  func (m FindProcessMode) Always() bool {
    26  	return m == FindProcessAlways
    27  }
    28  
    29  func (m FindProcessMode) Off() bool {
    30  	return m == FindProcessOff
    31  }
    32  
    33  func (m *FindProcessMode) UnmarshalYAML(unmarshal func(any) error) error {
    34  	var tp string
    35  	if err := unmarshal(&tp); err != nil {
    36  		return err
    37  	}
    38  	return m.Set(tp)
    39  }
    40  
    41  func (m *FindProcessMode) UnmarshalJSON(data []byte) error {
    42  	var tp string
    43  	if err := json.Unmarshal(data, &tp); err != nil {
    44  		return err
    45  	}
    46  	return m.Set(tp)
    47  }
    48  
    49  func (m *FindProcessMode) Set(value string) error {
    50  	mode := strings.ToLower(value)
    51  	_, exist := validModes[mode]
    52  	if !exist {
    53  		return errors.New("invalid find process mode")
    54  	}
    55  	*m = FindProcessMode(mode)
    56  	return nil
    57  }