github.com/mutagen-io/mutagen@v0.18.0-rc1/pkg/synchronization/compression/algorithm.go (about)

     1  package compression
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  
     7  	"github.com/mutagen-io/mutagen/pkg/stream"
     8  )
     9  
    10  // IsDefault indicates whether or not the algorithm is
    11  // Algorithm_AlgorithmDefault.
    12  func (a Algorithm) IsDefault() bool {
    13  	return a == Algorithm_AlgorithmDefault
    14  }
    15  
    16  // MarshalText implements encoding.TextMarshaler.MarshalText.
    17  func (a Algorithm) MarshalText() ([]byte, error) {
    18  	var result string
    19  	switch a {
    20  	case Algorithm_AlgorithmDefault:
    21  	case Algorithm_AlgorithmNone:
    22  		result = "none"
    23  	case Algorithm_AlgorithmDeflate:
    24  		result = "deflate"
    25  	case Algorithm_AlgorithmZstandard:
    26  		result = "zstandard"
    27  	default:
    28  		result = "unknown"
    29  	}
    30  	return []byte(result), nil
    31  }
    32  
    33  // UnmarshalText implements encoding.TextUnmarshaler.UnmarshalText.
    34  func (a *Algorithm) UnmarshalText(textBytes []byte) error {
    35  	// Convert the bytes to a string.
    36  	text := string(textBytes)
    37  
    38  	// Convert to a compression algorithm.
    39  	switch text {
    40  	case "none":
    41  		*a = Algorithm_AlgorithmNone
    42  	case "deflate":
    43  		*a = Algorithm_AlgorithmDeflate
    44  	case "zstandard":
    45  		*a = Algorithm_AlgorithmZstandard
    46  	default:
    47  		return fmt.Errorf("unknown compression algorithm specification: %s", text)
    48  	}
    49  
    50  	// Success.
    51  	return nil
    52  }
    53  
    54  // AlgorithmSupportStatus encodes support status for a compression algorithm.
    55  type AlgorithmSupportStatus uint8
    56  
    57  const (
    58  	// AlgorithmSupportStatusUnsupported indicates that an algorithm is
    59  	// completely unsupported.
    60  	AlgorithmSupportStatusUnsupported AlgorithmSupportStatus = iota
    61  	// AlgorithmSupportStatusRequiresLicense indicates that an algorithm is
    62  	// supported but requires a (currently absent) Mutagen Pro license.
    63  	AlgorithmSupportStatusRequiresLicense
    64  	// AlgorithmSupportStatusSupported indicates that an algorithm is fully
    65  	// supported, either due to being supported universally in Mutagen or due to
    66  	// the presence of a Mutagen Pro license.
    67  	AlgorithmSupportStatusSupported
    68  )
    69  
    70  // SupportStatus returns the support status for a particular algorithm.
    71  func (a Algorithm) SupportStatus() AlgorithmSupportStatus {
    72  	switch a {
    73  	case Algorithm_AlgorithmNone:
    74  		return AlgorithmSupportStatusSupported
    75  	case Algorithm_AlgorithmDeflate:
    76  		return AlgorithmSupportStatusSupported
    77  	case Algorithm_AlgorithmZstandard:
    78  		return zstandardSupportStatus()
    79  	default:
    80  		return AlgorithmSupportStatusUnsupported
    81  	}
    82  }
    83  
    84  // Description returns a human-readable description of a compression algorithm.
    85  func (a Algorithm) Description() string {
    86  	switch a {
    87  	case Algorithm_AlgorithmDefault:
    88  		return "Default"
    89  	case Algorithm_AlgorithmNone:
    90  		return "None"
    91  	case Algorithm_AlgorithmDeflate:
    92  		return "DEFLATE"
    93  	case Algorithm_AlgorithmZstandard:
    94  		return "Zstandard"
    95  	default:
    96  		return "Unknown"
    97  	}
    98  }
    99  
   100  // Compress creates a compressor that writes compressed output to the specified
   101  // stream using the compression algorithm. If invoked on a default or invalid
   102  // Algorithm value, this method will panic. The Flush and Close methods on the
   103  // resulting compressor only operate on the compressor - they have no effect on
   104  // the compressed stream itself. The compressor should be flushed and/or closed
   105  // before the underlying stream.
   106  func (a Algorithm) Compress(compressed io.Writer) stream.WriteFlushCloser {
   107  	switch a {
   108  	case Algorithm_AlgorithmNone:
   109  		return compressNone(compressed)
   110  	case Algorithm_AlgorithmDeflate:
   111  		return compressDeflate(compressed)
   112  	case Algorithm_AlgorithmZstandard:
   113  		return compressZstandard(compressed)
   114  	default:
   115  		panic("default or unknown compression algorithm")
   116  	}
   117  }
   118  
   119  // Decompress creates a decompressor that reads compressed input from the
   120  // specified stream using the compression algorithm. If invoked on a default or
   121  // invalid Algorithm value, this method will panic. The Close method on the
   122  // resulting decompressor releases decompression resources - it has no effect on
   123  // the compressed stream itself. The decompressor should be closed after the
   124  // underlying stream.
   125  func (a Algorithm) Decompress(compressed io.Reader) io.ReadCloser {
   126  	switch a {
   127  	case Algorithm_AlgorithmNone:
   128  		return decompressNone(compressed)
   129  	case Algorithm_AlgorithmDeflate:
   130  		return decompressDeflate(compressed)
   131  	case Algorithm_AlgorithmZstandard:
   132  		return decompressZstandard(compressed)
   133  	default:
   134  		panic("default or unknown compression algorithm")
   135  	}
   136  }