// Code generated by bebopc-go; DO NOT EDIT.

package generated

import (
	"github.com/200sc/bebop"
	"github.com/200sc/bebop/iohelp"
	"io"
)

var _ bebop.Record = &Fruit{}

type Fruit struct {
	ID uint64
	Name string
}

func (bbp Fruit) MarshalBebopTo(buf []byte) int {
	at := 0
	iohelp.WriteUint64Bytes(buf[at:], bbp.ID)
	at += 8
	iohelp.WriteUint32Bytes(buf[at:], uint32(len(bbp.Name)))
	copy(buf[at+4:at+4+len(bbp.Name)], []byte(bbp.Name))
	at += 4 + len(bbp.Name)
	return at
}

func (bbp *Fruit) UnmarshalBebop(buf []byte) (err error) {
	at := 0
	if len(buf[at:]) < 8 {
		return io.ErrUnexpectedEOF
	}
	bbp.ID = iohelp.ReadUint64Bytes(buf[at:])
	at += 8
	bbp.Name, err = iohelp.ReadStringBytes(buf[at:])
	if err != nil {
		return err
	}
	at += 4 + len(bbp.Name)
	return nil
}

func (bbp *Fruit) MustUnmarshalBebop(buf []byte) {
	at := 0
	bbp.ID = iohelp.ReadUint64Bytes(buf[at:])
	at += 8
	bbp.Name = iohelp.MustReadStringBytes(buf[at:])
	at += 4 + len(bbp.Name)
}

func (bbp Fruit) EncodeBebop(iow io.Writer) (err error) {
	w := iohelp.NewErrorWriter(iow)
	iohelp.WriteUint64(w, bbp.ID)
	iohelp.WriteUint32(w, uint32(len(bbp.Name)))
	w.Write([]byte(bbp.Name))
	return w.Err
}

func (bbp *Fruit) DecodeBebop(ior io.Reader) (err error) {
	r := iohelp.NewErrorReader(ior)
	bbp.ID = iohelp.ReadUint64(r)
	bbp.Name = iohelp.ReadString(r)
	return r.Err
}

func (bbp Fruit) Size() int {
	bodyLen := 0
	bodyLen += 8
	bodyLen += 4 + len(bbp.Name)
	return bodyLen
}

func (bbp Fruit) MarshalBebop() []byte {
	buf := make([]byte, bbp.Size())
	bbp.MarshalBebopTo(buf)
	return buf
}

func MakeFruit(r *iohelp.ErrorReader) (Fruit, error) {
	v := Fruit{}
	err := v.DecodeBebop(r)
	return v, err
}

func MakeFruitFromBytes(buf []byte) (Fruit, error) {
	v := Fruit{}
	err := v.UnmarshalBebop(buf)
	return v, err
}

func MustMakeFruitFromBytes(buf []byte) Fruit {
	v := Fruit{}
	v.MustUnmarshalBebop(buf)
	return v
}

var _ bebop.Record = &Basket{}

type Basket struct {
	Apples Fruit
	Oranges Fruit
	Mangos Fruit
}

func (bbp Basket) MarshalBebopTo(buf []byte) int {
	at := 0
	(bbp.Apples).MarshalBebopTo(buf[at:])
	{
		tmp := (bbp.Apples)
		at += tmp.Size()
	}
	
	(bbp.Oranges).MarshalBebopTo(buf[at:])
	{
		tmp := (bbp.Oranges)
		at += tmp.Size()
	}
	
	(bbp.Mangos).MarshalBebopTo(buf[at:])
	{
		tmp := (bbp.Mangos)
		at += tmp.Size()
	}
	
	return at
}

func (bbp *Basket) UnmarshalBebop(buf []byte) (err error) {
	at := 0
	bbp.Apples, err = MakeFruitFromBytes(buf[at:])
	if err != nil {
		return err
	}
	{
		tmp := (bbp.Apples)
		at += tmp.Size()
	}
	
	bbp.Oranges, err = MakeFruitFromBytes(buf[at:])
	if err != nil {
		return err
	}
	{
		tmp := (bbp.Oranges)
		at += tmp.Size()
	}
	
	bbp.Mangos, err = MakeFruitFromBytes(buf[at:])
	if err != nil {
		return err
	}
	{
		tmp := (bbp.Mangos)
		at += tmp.Size()
	}
	
	return nil
}

func (bbp *Basket) MustUnmarshalBebop(buf []byte) {
	at := 0
	bbp.Apples = MustMakeFruitFromBytes(buf[at:])
	{
		tmp := (bbp.Apples)
		at += tmp.Size()
	}
	
	bbp.Oranges = MustMakeFruitFromBytes(buf[at:])
	{
		tmp := (bbp.Oranges)
		at += tmp.Size()
	}
	
	bbp.Mangos = MustMakeFruitFromBytes(buf[at:])
	{
		tmp := (bbp.Mangos)
		at += tmp.Size()
	}
	
}

func (bbp Basket) EncodeBebop(iow io.Writer) (err error) {
	w := iohelp.NewErrorWriter(iow)
	err = (bbp.Apples).EncodeBebop(w)
	if err != nil {
		return err
	}
	err = (bbp.Oranges).EncodeBebop(w)
	if err != nil {
		return err
	}
	err = (bbp.Mangos).EncodeBebop(w)
	if err != nil {
		return err
	}
	return w.Err
}

func (bbp *Basket) DecodeBebop(ior io.Reader) (err error) {
	r := iohelp.NewErrorReader(ior)
	(bbp.Apples), err = MakeFruit(r)
	if err != nil {
		return err
	}
	(bbp.Oranges), err = MakeFruit(r)
	if err != nil {
		return err
	}
	(bbp.Mangos), err = MakeFruit(r)
	if err != nil {
		return err
	}
	return r.Err
}

func (bbp Basket) Size() int {
	bodyLen := 0
	{
		tmp := (bbp.Apples)
		bodyLen += tmp.Size()
	}
	
	{
		tmp := (bbp.Oranges)
		bodyLen += tmp.Size()
	}
	
	{
		tmp := (bbp.Mangos)
		bodyLen += tmp.Size()
	}
	
	return bodyLen
}

func (bbp Basket) MarshalBebop() []byte {
	buf := make([]byte, bbp.Size())
	bbp.MarshalBebopTo(buf)
	return buf
}

func MakeBasket(r *iohelp.ErrorReader) (Basket, error) {
	v := Basket{}
	err := v.DecodeBebop(r)
	return v, err
}

func MakeBasketFromBytes(buf []byte) (Basket, error) {
	v := Basket{}
	err := v.UnmarshalBebop(buf)
	return v, err
}

func MustMakeBasketFromBytes(buf []byte) Basket {
	v := Basket{}
	v.MustUnmarshalBebop(buf)
	return v
}

