github.com/mavryk-network/mvgo@v1.19.9/mavryk/rights.go (about)

     1  // Copyright (c) 2020-2021 Blockwatch Data Inc.
     2  // Author: alex@blockwatch.cc
     3  
     4  package mavryk
     5  
     6  import (
     7  	"fmt"
     8  )
     9  
    10  type RightType byte
    11  
    12  const (
    13  	RightTypeInvalid RightType = iota
    14  	RightTypeBaking
    15  	RightTypeEndorsing
    16  )
    17  
    18  func ParseRightType(s string) RightType {
    19  	switch s {
    20  	case "baking":
    21  		return RightTypeBaking
    22  	case "endorsing":
    23  		return RightTypeEndorsing
    24  	default:
    25  		return RightTypeInvalid
    26  	}
    27  }
    28  
    29  func (r RightType) String() string {
    30  	switch r {
    31  	case RightTypeBaking:
    32  		return "baking"
    33  	case RightTypeEndorsing:
    34  		return "endorsing"
    35  	default:
    36  		return ""
    37  	}
    38  }
    39  
    40  func (r RightType) MarshalText() ([]byte, error) {
    41  	return []byte(r.String()), nil
    42  }
    43  
    44  func (r RightType) IsValid() bool {
    45  	return r != RightTypeInvalid
    46  }
    47  
    48  func (r *RightType) UnmarshalText(data []byte) error {
    49  	vv := ParseRightType(string(data))
    50  	if !vv.IsValid() {
    51  		return fmt.Errorf("tezos: invalid right type '%s'", string(data))
    52  	}
    53  	*r = vv
    54  	return nil
    55  }