github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/kbfsmd/flags.go (about)

     1  // Copyright 2017 Keybase Inc. All rights reserved.
     2  // Use of this source code is governed by a BSD
     3  // license that can be found in the LICENSE file.
     4  
     5  package kbfsmd
     6  
     7  import (
     8  	"fmt"
     9  	"strings"
    10  )
    11  
    12  // MetadataFlags bitfield.
    13  type MetadataFlags byte
    14  
    15  // Possible flags set in the MetadataFlags bitfield.
    16  const (
    17  	MetadataFlagRekey MetadataFlags = 1 << iota
    18  	MetadataFlagWriterMetadataCopied
    19  	MetadataFlagFinal
    20  )
    21  
    22  var metadataFlagStringMap = map[int]string{
    23  	int(MetadataFlagRekey):                "Rekey",
    24  	int(MetadataFlagWriterMetadataCopied): "WriterMetadataCopied",
    25  	int(MetadataFlagFinal):                "Final",
    26  }
    27  
    28  func flagsToString(flags int, flagStringMap map[int]string) string {
    29  	var flagStrings []string
    30  	for f, s := range flagStringMap {
    31  		if flags&f != 0 {
    32  			flagStrings = append(flagStrings, s)
    33  			flags &^= f
    34  		}
    35  	}
    36  	if flags != 0 {
    37  		flagStrings = append(flagStrings, fmt.Sprintf("%b", flags))
    38  	}
    39  	return strings.Join(flagStrings, " | ")
    40  }
    41  
    42  func (flags MetadataFlags) String() string {
    43  	return fmt.Sprintf("MetadataFlags(%s)", flagsToString(int(flags), metadataFlagStringMap))
    44  }
    45  
    46  // WriterFlags bitfield.
    47  type WriterFlags byte
    48  
    49  // Possible flags set in the WriterFlags bitfield.
    50  const (
    51  	MetadataFlagUnmerged WriterFlags = 1 << iota
    52  )
    53  
    54  var writerFlagStringMap = map[int]string{
    55  	int(MetadataFlagUnmerged): "Unmerged",
    56  }
    57  
    58  func (flags WriterFlags) String() string {
    59  	return fmt.Sprintf("WriterFlags(%s)", flagsToString(int(flags), writerFlagStringMap))
    60  }