github.com/safing/portbase@v0.19.5/formats/dsd/format.go (about)

     1  package dsd
     2  
     3  import "errors"
     4  
     5  // Errors.
     6  var (
     7  	ErrIncompatibleFormat = errors.New("dsd: format is incompatible with operation")
     8  	ErrIsRaw              = errors.New("dsd: given data is in raw format")
     9  	ErrUnknownFormat      = errors.New("dsd: format is unknown")
    10  )
    11  
    12  // Format types.
    13  const (
    14  	AUTO = 0
    15  
    16  	// Serialization types.
    17  	RAW     = 1
    18  	CBOR    = 67 // C
    19  	GenCode = 71 // G
    20  	JSON    = 74 // J
    21  	MsgPack = 77 // M
    22  	YAML    = 89 // Y
    23  
    24  	// Compression types.
    25  	GZIP = 90 // Z
    26  
    27  	// Special types.
    28  	LIST = 76 // L
    29  )
    30  
    31  // Default Formats.
    32  var (
    33  	DefaultSerializationFormat uint8 = JSON
    34  	DefaultCompressionFormat   uint8 = GZIP
    35  )
    36  
    37  // ValidateSerializationFormat validates if the format is for serialization,
    38  // and returns the validated format as well as the result of the validation.
    39  // If called on the AUTO format, it returns the default serialization format.
    40  func ValidateSerializationFormat(format uint8) (validatedFormat uint8, ok bool) {
    41  	switch format {
    42  	case AUTO:
    43  		return DefaultSerializationFormat, true
    44  	case RAW:
    45  		return format, true
    46  	case CBOR:
    47  		return format, true
    48  	case GenCode:
    49  		return format, true
    50  	case JSON:
    51  		return format, true
    52  	case YAML:
    53  		return format, true
    54  	case MsgPack:
    55  		return format, true
    56  	default:
    57  		return 0, false
    58  	}
    59  }
    60  
    61  // ValidateCompressionFormat validates if the format is for compression,
    62  // and returns the validated format as well as the result of the validation.
    63  // If called on the AUTO format, it returns the default compression format.
    64  func ValidateCompressionFormat(format uint8) (validatedFormat uint8, ok bool) {
    65  	switch format {
    66  	case AUTO:
    67  		return DefaultCompressionFormat, true
    68  	case GZIP:
    69  		return format, true
    70  	default:
    71  		return 0, false
    72  	}
    73  }