github.com/quay/claircore@v1.5.28/severity.go (about)

     1  package claircore
     2  
     3  import (
     4  	"bytes"
     5  	"database/sql/driver"
     6  	"fmt"
     7  )
     8  
     9  type Severity uint
    10  
    11  const (
    12  	Unknown Severity = iota
    13  	Negligible
    14  	Low
    15  	Medium
    16  	High
    17  	Critical
    18  )
    19  
    20  func (s *Severity) MarshalText() ([]byte, error) {
    21  	return []byte(s.String()), nil
    22  }
    23  
    24  func (s *Severity) UnmarshalText(b []byte) error {
    25  	// This depends on the contents of severity_string.go.
    26  	i := bytes.Index([]byte(_Severity_name), b)
    27  	if i == -1 {
    28  		return fmt.Errorf("unknown severity %q", string(b))
    29  	}
    30  	idx := uint8(i)
    31  	for n, off := range _Severity_index {
    32  		if idx == off {
    33  			*s = Severity(n)
    34  			return nil
    35  		}
    36  	}
    37  	panic("unreachable")
    38  }
    39  
    40  func (s Severity) Value() (driver.Value, error) {
    41  	return s.String(), nil
    42  }
    43  
    44  func (s *Severity) Scan(i interface{}) error {
    45  	switch v := i.(type) {
    46  	case []byte:
    47  		return s.UnmarshalText(v)
    48  	case string:
    49  		return s.UnmarshalText([]byte(v))
    50  	case int64:
    51  		if v >= int64(len(_Severity_index)-1) {
    52  			return fmt.Errorf("unable to scan Severity from enum %d", v)
    53  		}
    54  		*s = Severity(v)
    55  	default:
    56  		return fmt.Errorf("unable to scan Severity from type %T", i)
    57  	}
    58  	return nil
    59  }