package thrift

import (
	
	
	
	
	
	

	
)

// CompactProtocol is a Protocol implementation for the compact thrift protocol.
//
// https://github.com/apache/thrift/blob/master/doc/specs/thrift-compact-protocol.md#integer-encoding
type CompactProtocol struct{}

func ( *CompactProtocol) ( io.Reader) Reader {
	return &compactReader{protocol: , binary: binaryReader{r: }}
}

func ( *CompactProtocol) ( io.Writer) Writer {
	return &compactWriter{protocol: , binary: binaryWriter{w: }}
}

func ( *CompactProtocol) () Features {
	return UseDeltaEncoding | CoalesceBoolFields
}

type compactReader struct {
	protocol *CompactProtocol
	binary   binaryReader
}

func ( *compactReader) () Protocol {
	return .protocol
}

func ( *compactReader) () io.Reader {
	return .binary.Reader()
}

func ( *compactReader) () (bool, error) {
	return .binary.ReadBool()
}

func ( *compactReader) () (int8, error) {
	return .binary.ReadInt8()
}

func ( *compactReader) () (int16, error) {
	,  := .readVarint("int16", math.MinInt16, math.MaxInt16)
	return int16(), 
}

func ( *compactReader) () (int32, error) {
	,  := .readVarint("int32", math.MinInt32, math.MaxInt32)
	return int32(), 
}

func ( *compactReader) () (int64, error) {
	return .readVarint("int64", math.MinInt64, math.MaxInt64)
}

func ( *compactReader) () (float64, error) {
	return .binary.ReadFloat64()
}

func ( *compactReader) () ([]byte, error) {
	,  := .ReadLength()
	if  != nil {
		return nil, 
	}
	 := make([]byte, )
	_,  = io.ReadFull(.Reader(), )
	return , 
}

func ( *compactReader) () (string, error) {
	,  := .ReadBytes()
	return unsafecast.String(), 
}

func ( *compactReader) () (int, error) {
	,  := .readUvarint("length", math.MaxInt32)
	return int(), 
}

func ( *compactReader) () (Message, error) {
	 := Message{}

	,  := .ReadByte()
	if  != nil {
		return , 
	}
	if  != 0x82 {
		return , fmt.Errorf("invalid protocol id found when reading thrift message: %#x", )
	}

	,  := .ReadByte()
	if  != nil {
		return , dontExpectEOF()
	}

	,  := .readUvarint("seq id", math.MaxInt32)
	if  != nil {
		return , dontExpectEOF()
	}

	.Type = MessageType() & 0x7
	.SeqID = int32()
	.Name,  = .ReadString()
	return , dontExpectEOF()
}

func ( *compactReader) () (Field, error) {
	 := Field{}

	,  := .ReadByte()
	if  != nil {
		return , 
	}

	if Type() == STOP {
		return , nil
	}

	if ( >> 4) != 0 {
		 = Field{ID: int16( >> 4), Type: Type( & 0xF), Delta: true}
	} else {
		,  := .ReadInt16()
		if  != nil {
			return , dontExpectEOF()
		}
		 = Field{ID: , Type: Type()}
	}

	return , nil
}

func ( *compactReader) () (List, error) {
	,  := .ReadByte()
	if  != nil {
		return List{}, 
	}
	if ( >> 4) != 0xF {
		return List{Size: int32( >> 4), Type: Type( & 0xF)}, nil
	}
	,  := .readUvarint("list size", math.MaxInt32)
	if  != nil {
		return List{}, dontExpectEOF()
	}
	return List{Size: int32(), Type: Type( & 0xF)}, nil
}

func ( *compactReader) () (Set, error) {
	,  := .ReadList()
	return Set(), 
}

func ( *compactReader) () (Map, error) {
	,  := .readUvarint("map size", math.MaxInt32)
	if  != nil {
		return Map{}, 
	}
	if  == 0 { // empty map
		return Map{}, nil
	}
	,  := .ReadByte()
	if  != nil {
		return Map{}, dontExpectEOF()
	}
	return Map{Size: int32(), Key: Type( >> 4), Value: Type( & 0xF)}, nil
}

func ( *compactReader) () (byte, error) {
	return .binary.ReadByte()
}

func ( *compactReader) ( string,  uint64) (uint64, error) {
	var  io.ByteReader

	switch x := .Reader().(type) {
	case *bytes.Buffer:
		 = 
	case *bytes.Reader:
		 = 
	case *bufio.Reader:
		 = 
	case io.ByteReader:
		 = 
	default:
		 = &.binary
	}

	,  := binary.ReadUvarint()
	if  == nil {
		if  >  {
			 = fmt.Errorf("%s varint out of range: %d > %d", , , )
		}
	}
	return , 
}

func ( *compactReader) ( string, ,  int64) (int64, error) {
	var  io.ByteReader

	switch x := .Reader().(type) {
	case *bytes.Buffer:
		 = 
	case *bytes.Reader:
		 = 
	case *bufio.Reader:
		 = 
	case io.ByteReader:
		 = 
	default:
		 = &.binary
	}

	,  := binary.ReadVarint()
	if  == nil {
		if  <  ||  >  {
			 = fmt.Errorf("%s varint out of range: %d not in [%d;%d]", , , , )
		}
	}
	return , 
}

type compactWriter struct {
	protocol *CompactProtocol
	binary   binaryWriter
	varint   [binary.MaxVarintLen64]byte
}

func ( *compactWriter) () Protocol {
	return .protocol
}

func ( *compactWriter) () io.Writer {
	return .binary.Writer()
}

func ( *compactWriter) ( bool) error {
	return .binary.WriteBool()
}

func ( *compactWriter) ( int8) error {
	return .binary.WriteInt8()
}

func ( *compactWriter) ( int16) error {
	return .writeVarint(int64())
}

func ( *compactWriter) ( int32) error {
	return .writeVarint(int64())
}

func ( *compactWriter) ( int64) error {
	return .writeVarint()
}

func ( *compactWriter) ( float64) error {
	return .binary.WriteFloat64()
}

func ( *compactWriter) ( []byte) error {
	if  := .WriteLength(len());  != nil {
		return 
	}
	return .binary.write()
}

func ( *compactWriter) ( string) error {
	if  := .WriteLength(len());  != nil {
		return 
	}
	return .binary.writeString()
}

func ( *compactWriter) ( int) error {
	if  < 0 {
		return fmt.Errorf("negative length cannot be encoded in thrift: %d", )
	}
	if  > math.MaxInt32 {
		return fmt.Errorf("length is too large to be encoded in thrift: %d", )
	}
	return .writeUvarint(uint64())
}

func ( *compactWriter) ( Message) error {
	if  := .binary.writeByte(0x82);  != nil {
		return 
	}
	if  := .binary.writeByte(byte(.Type));  != nil {
		return 
	}
	if  := .writeUvarint(uint64(.SeqID));  != nil {
		return 
	}
	return .WriteString(.Name)
}

func ( *compactWriter) ( Field) error {
	if .Type == STOP {
		return .binary.writeByte(0)
	}
	if .ID <= 15 {
		return .binary.writeByte(byte(.ID<<4) | byte(.Type))
	}
	if  := .binary.writeByte(byte(.Type));  != nil {
		return 
	}
	return .WriteInt16(.ID)
}

func ( *compactWriter) ( List) error {
	if .Size <= 14 {
		return .binary.writeByte(byte(.Size<<4) | byte(.Type))
	}
	if  := .binary.writeByte(0xF0 | byte(.Type));  != nil {
		return 
	}
	return .writeUvarint(uint64(.Size))
}

func ( *compactWriter) ( Set) error {
	return .WriteList(List())
}

func ( *compactWriter) ( Map) error {
	if  := .writeUvarint(uint64(.Size));  != nil || .Size == 0 {
		return 
	}
	return .binary.writeByte((byte(.Key) << 4) | byte(.Value))
}

func ( *compactWriter) ( uint64) error {
	 := binary.PutUvarint(.varint[:], )
	return .binary.write(.varint[:])
}

func ( *compactWriter) ( int64) error {
	 := binary.PutVarint(.varint[:], )
	return .binary.write(.varint[:])
}