// Code generated by https://github.com/gagliardetto/anchor-go. DO NOT EDIT.
// This file contains parsers for the types defined in the IDL.

package signer_registry_solana

import (
	"bytes"
	"fmt"
	errors "github.com/gagliardetto/anchor-go/errors"
	binary "github.com/gagliardetto/binary"
	solanago "github.com/gagliardetto/solana-go"
)

type Config struct {
	Owner         solanago.PublicKey  `json:"owner"`
	ProposedOwner *solanago.PublicKey `bin:"optional" json:"proposedOwner,omitempty"`
	ConfigBump    uint8               `json:"configBump"`
	SignersBump   uint8               `json:"signersBump"`
}

func (obj Config) MarshalWithEncoder(encoder *binary.Encoder) (err error) {
	// Serialize `Owner`:
	err = encoder.Encode(obj.Owner)
	if err != nil {
		return errors.NewField("Owner", err)
	}
	// Serialize `ProposedOwner` (optional):
	{
		if obj.ProposedOwner == nil {
			err = encoder.WriteOption(false)
			if err != nil {
				return errors.NewOption("ProposedOwner", fmt.Errorf("error while encoding optionality: %w", err))
			}
		} else {
			err = encoder.WriteOption(true)
			if err != nil {
				return errors.NewOption("ProposedOwner", fmt.Errorf("error while encoding optionality: %w", err))
			}
			err = encoder.Encode(obj.ProposedOwner)
			if err != nil {
				return errors.NewField("ProposedOwner", err)
			}
		}
	}
	// Serialize `ConfigBump`:
	err = encoder.Encode(obj.ConfigBump)
	if err != nil {
		return errors.NewField("ConfigBump", err)
	}
	// Serialize `SignersBump`:
	err = encoder.Encode(obj.SignersBump)
	if err != nil {
		return errors.NewField("SignersBump", err)
	}
	return nil
}

func (obj Config) Marshal() ([]byte, error) {
	buf := bytes.NewBuffer(nil)
	encoder := binary.NewBorshEncoder(buf)
	err := obj.MarshalWithEncoder(encoder)
	if err != nil {
		return nil, fmt.Errorf("error while encoding Config: %w", err)
	}
	return buf.Bytes(), nil
}

func (obj *Config) UnmarshalWithDecoder(decoder *binary.Decoder) (err error) {
	// Deserialize `Owner`:
	err = decoder.Decode(&obj.Owner)
	if err != nil {
		return errors.NewField("Owner", err)
	}
	// Deserialize `ProposedOwner` (optional):
	{
		ok, err := decoder.ReadOption()
		if err != nil {
			return errors.NewOption("ProposedOwner", fmt.Errorf("error while reading optionality: %w", err))
		}
		if ok {
			err = decoder.Decode(&obj.ProposedOwner)
			if err != nil {
				return errors.NewField("ProposedOwner", err)
			}
		}
	}
	// Deserialize `ConfigBump`:
	err = decoder.Decode(&obj.ConfigBump)
	if err != nil {
		return errors.NewField("ConfigBump", err)
	}
	// Deserialize `SignersBump`:
	err = decoder.Decode(&obj.SignersBump)
	if err != nil {
		return errors.NewField("SignersBump", err)
	}
	return nil
}

func (obj *Config) Unmarshal(buf []byte) error {
	err := obj.UnmarshalWithDecoder(binary.NewBorshDecoder(buf))
	if err != nil {
		return fmt.Errorf("error while unmarshaling Config: %w", err)
	}
	return nil
}

func UnmarshalConfig(buf []byte) (*Config, error) {
	obj := new(Config)
	err := obj.Unmarshal(buf)
	if err != nil {
		return nil, err
	}
	return obj, nil
}

type Initialized struct {
	Owner solanago.PublicKey `json:"owner"`
}

func (obj Initialized) MarshalWithEncoder(encoder *binary.Encoder) (err error) {
	// Serialize `Owner`:
	err = encoder.Encode(obj.Owner)
	if err != nil {
		return errors.NewField("Owner", err)
	}
	return nil
}

func (obj Initialized) Marshal() ([]byte, error) {
	buf := bytes.NewBuffer(nil)
	encoder := binary.NewBorshEncoder(buf)
	err := obj.MarshalWithEncoder(encoder)
	if err != nil {
		return nil, fmt.Errorf("error while encoding Initialized: %w", err)
	}
	return buf.Bytes(), nil
}

func (obj *Initialized) UnmarshalWithDecoder(decoder *binary.Decoder) (err error) {
	// Deserialize `Owner`:
	err = decoder.Decode(&obj.Owner)
	if err != nil {
		return errors.NewField("Owner", err)
	}
	return nil
}

func (obj *Initialized) Unmarshal(buf []byte) error {
	err := obj.UnmarshalWithDecoder(binary.NewBorshDecoder(buf))
	if err != nil {
		return fmt.Errorf("error while unmarshaling Initialized: %w", err)
	}
	return nil
}

func UnmarshalInitialized(buf []byte) (*Initialized, error) {
	obj := new(Initialized)
	err := obj.Unmarshal(buf)
	if err != nil {
		return nil, err
	}
	return obj, nil
}

type NewOwnerProposed struct {
	CurrentOwner  solanago.PublicKey `json:"currentOwner"`
	ProposedOwner solanago.PublicKey `json:"proposedOwner"`
}

func (obj NewOwnerProposed) MarshalWithEncoder(encoder *binary.Encoder) (err error) {
	// Serialize `CurrentOwner`:
	err = encoder.Encode(obj.CurrentOwner)
	if err != nil {
		return errors.NewField("CurrentOwner", err)
	}
	// Serialize `ProposedOwner`:
	err = encoder.Encode(obj.ProposedOwner)
	if err != nil {
		return errors.NewField("ProposedOwner", err)
	}
	return nil
}

func (obj NewOwnerProposed) Marshal() ([]byte, error) {
	buf := bytes.NewBuffer(nil)
	encoder := binary.NewBorshEncoder(buf)
	err := obj.MarshalWithEncoder(encoder)
	if err != nil {
		return nil, fmt.Errorf("error while encoding NewOwnerProposed: %w", err)
	}
	return buf.Bytes(), nil
}

func (obj *NewOwnerProposed) UnmarshalWithDecoder(decoder *binary.Decoder) (err error) {
	// Deserialize `CurrentOwner`:
	err = decoder.Decode(&obj.CurrentOwner)
	if err != nil {
		return errors.NewField("CurrentOwner", err)
	}
	// Deserialize `ProposedOwner`:
	err = decoder.Decode(&obj.ProposedOwner)
	if err != nil {
		return errors.NewField("ProposedOwner", err)
	}
	return nil
}

func (obj *NewOwnerProposed) Unmarshal(buf []byte) error {
	err := obj.UnmarshalWithDecoder(binary.NewBorshDecoder(buf))
	if err != nil {
		return fmt.Errorf("error while unmarshaling NewOwnerProposed: %w", err)
	}
	return nil
}

func UnmarshalNewOwnerProposed(buf []byte) (*NewOwnerProposed, error) {
	obj := new(NewOwnerProposed)
	err := obj.Unmarshal(buf)
	if err != nil {
		return nil, err
	}
	return obj, nil
}

type OwnerChanged struct {
	OldOwner solanago.PublicKey `json:"oldOwner"`
	NewOwner solanago.PublicKey `json:"newOwner"`
}

func (obj OwnerChanged) MarshalWithEncoder(encoder *binary.Encoder) (err error) {
	// Serialize `OldOwner`:
	err = encoder.Encode(obj.OldOwner)
	if err != nil {
		return errors.NewField("OldOwner", err)
	}
	// Serialize `NewOwner`:
	err = encoder.Encode(obj.NewOwner)
	if err != nil {
		return errors.NewField("NewOwner", err)
	}
	return nil
}

func (obj OwnerChanged) Marshal() ([]byte, error) {
	buf := bytes.NewBuffer(nil)
	encoder := binary.NewBorshEncoder(buf)
	err := obj.MarshalWithEncoder(encoder)
	if err != nil {
		return nil, fmt.Errorf("error while encoding OwnerChanged: %w", err)
	}
	return buf.Bytes(), nil
}

func (obj *OwnerChanged) UnmarshalWithDecoder(decoder *binary.Decoder) (err error) {
	// Deserialize `OldOwner`:
	err = decoder.Decode(&obj.OldOwner)
	if err != nil {
		return errors.NewField("OldOwner", err)
	}
	// Deserialize `NewOwner`:
	err = decoder.Decode(&obj.NewOwner)
	if err != nil {
		return errors.NewField("NewOwner", err)
	}
	return nil
}

func (obj *OwnerChanged) Unmarshal(buf []byte) error {
	err := obj.UnmarshalWithDecoder(binary.NewBorshDecoder(buf))
	if err != nil {
		return fmt.Errorf("error while unmarshaling OwnerChanged: %w", err)
	}
	return nil
}

func UnmarshalOwnerChanged(buf []byte) (*OwnerChanged, error) {
	obj := new(OwnerChanged)
	err := obj.Unmarshal(buf)
	if err != nil {
		return nil, err
	}
	return obj, nil
}

type Signer struct {
	EvmAddress    [20]uint8  `json:"evmAddress"`
	NewEvmAddress *[20]uint8 `bin:"optional" json:"newEvmAddress,omitempty"`
}

func (obj Signer) MarshalWithEncoder(encoder *binary.Encoder) (err error) {
	// Serialize `EvmAddress`:
	err = encoder.Encode(obj.EvmAddress)
	if err != nil {
		return errors.NewField("EvmAddress", err)
	}
	// Serialize `NewEvmAddress` (optional):
	{
		if obj.NewEvmAddress == nil {
			err = encoder.WriteOption(false)
			if err != nil {
				return errors.NewOption("NewEvmAddress", fmt.Errorf("error while encoding optionality: %w", err))
			}
		} else {
			err = encoder.WriteOption(true)
			if err != nil {
				return errors.NewOption("NewEvmAddress", fmt.Errorf("error while encoding optionality: %w", err))
			}
			err = encoder.Encode(obj.NewEvmAddress)
			if err != nil {
				return errors.NewField("NewEvmAddress", err)
			}
		}
	}
	return nil
}

func (obj Signer) Marshal() ([]byte, error) {
	buf := bytes.NewBuffer(nil)
	encoder := binary.NewBorshEncoder(buf)
	err := obj.MarshalWithEncoder(encoder)
	if err != nil {
		return nil, fmt.Errorf("error while encoding Signer: %w", err)
	}
	return buf.Bytes(), nil
}

func (obj *Signer) UnmarshalWithDecoder(decoder *binary.Decoder) (err error) {
	// Deserialize `EvmAddress`:
	err = decoder.Decode(&obj.EvmAddress)
	if err != nil {
		return errors.NewField("EvmAddress", err)
	}
	// Deserialize `NewEvmAddress` (optional):
	{
		ok, err := decoder.ReadOption()
		if err != nil {
			return errors.NewOption("NewEvmAddress", fmt.Errorf("error while reading optionality: %w", err))
		}
		if ok {
			err = decoder.Decode(&obj.NewEvmAddress)
			if err != nil {
				return errors.NewField("NewEvmAddress", err)
			}
		}
	}
	return nil
}

func (obj *Signer) Unmarshal(buf []byte) error {
	err := obj.UnmarshalWithDecoder(binary.NewBorshDecoder(buf))
	if err != nil {
		return fmt.Errorf("error while unmarshaling Signer: %w", err)
	}
	return nil
}

func UnmarshalSigner(buf []byte) (*Signer, error) {
	obj := new(Signer)
	err := obj.Unmarshal(buf)
	if err != nil {
		return nil, err
	}
	return obj, nil
}

type SignerAdded struct {
	Signer Signer `json:"signer"`
}

func (obj SignerAdded) MarshalWithEncoder(encoder *binary.Encoder) (err error) {
	// Serialize `Signer`:
	err = encoder.Encode(obj.Signer)
	if err != nil {
		return errors.NewField("Signer", err)
	}
	return nil
}

func (obj SignerAdded) Marshal() ([]byte, error) {
	buf := bytes.NewBuffer(nil)
	encoder := binary.NewBorshEncoder(buf)
	err := obj.MarshalWithEncoder(encoder)
	if err != nil {
		return nil, fmt.Errorf("error while encoding SignerAdded: %w", err)
	}
	return buf.Bytes(), nil
}

func (obj *SignerAdded) UnmarshalWithDecoder(decoder *binary.Decoder) (err error) {
	// Deserialize `Signer`:
	err = decoder.Decode(&obj.Signer)
	if err != nil {
		return errors.NewField("Signer", err)
	}
	return nil
}

func (obj *SignerAdded) Unmarshal(buf []byte) error {
	err := obj.UnmarshalWithDecoder(binary.NewBorshDecoder(buf))
	if err != nil {
		return fmt.Errorf("error while unmarshaling SignerAdded: %w", err)
	}
	return nil
}

func UnmarshalSignerAdded(buf []byte) (*SignerAdded, error) {
	obj := new(SignerAdded)
	err := obj.Unmarshal(buf)
	if err != nil {
		return nil, err
	}
	return obj, nil
}

type SignerModified struct {
	OldSigner Signer `json:"oldSigner"`
	NewSigner Signer `json:"newSigner"`
}

func (obj SignerModified) MarshalWithEncoder(encoder *binary.Encoder) (err error) {
	// Serialize `OldSigner`:
	err = encoder.Encode(obj.OldSigner)
	if err != nil {
		return errors.NewField("OldSigner", err)
	}
	// Serialize `NewSigner`:
	err = encoder.Encode(obj.NewSigner)
	if err != nil {
		return errors.NewField("NewSigner", err)
	}
	return nil
}

func (obj SignerModified) Marshal() ([]byte, error) {
	buf := bytes.NewBuffer(nil)
	encoder := binary.NewBorshEncoder(buf)
	err := obj.MarshalWithEncoder(encoder)
	if err != nil {
		return nil, fmt.Errorf("error while encoding SignerModified: %w", err)
	}
	return buf.Bytes(), nil
}

func (obj *SignerModified) UnmarshalWithDecoder(decoder *binary.Decoder) (err error) {
	// Deserialize `OldSigner`:
	err = decoder.Decode(&obj.OldSigner)
	if err != nil {
		return errors.NewField("OldSigner", err)
	}
	// Deserialize `NewSigner`:
	err = decoder.Decode(&obj.NewSigner)
	if err != nil {
		return errors.NewField("NewSigner", err)
	}
	return nil
}

func (obj *SignerModified) Unmarshal(buf []byte) error {
	err := obj.UnmarshalWithDecoder(binary.NewBorshDecoder(buf))
	if err != nil {
		return fmt.Errorf("error while unmarshaling SignerModified: %w", err)
	}
	return nil
}

func UnmarshalSignerModified(buf []byte) (*SignerModified, error) {
	obj := new(SignerModified)
	err := obj.Unmarshal(buf)
	if err != nil {
		return nil, err
	}
	return obj, nil
}

type SignerRemoved struct {
	Signer Signer `json:"signer"`
}

func (obj SignerRemoved) MarshalWithEncoder(encoder *binary.Encoder) (err error) {
	// Serialize `Signer`:
	err = encoder.Encode(obj.Signer)
	if err != nil {
		return errors.NewField("Signer", err)
	}
	return nil
}

func (obj SignerRemoved) Marshal() ([]byte, error) {
	buf := bytes.NewBuffer(nil)
	encoder := binary.NewBorshEncoder(buf)
	err := obj.MarshalWithEncoder(encoder)
	if err != nil {
		return nil, fmt.Errorf("error while encoding SignerRemoved: %w", err)
	}
	return buf.Bytes(), nil
}

func (obj *SignerRemoved) UnmarshalWithDecoder(decoder *binary.Decoder) (err error) {
	// Deserialize `Signer`:
	err = decoder.Decode(&obj.Signer)
	if err != nil {
		return errors.NewField("Signer", err)
	}
	return nil
}

func (obj *SignerRemoved) Unmarshal(buf []byte) error {
	err := obj.UnmarshalWithDecoder(binary.NewBorshDecoder(buf))
	if err != nil {
		return fmt.Errorf("error while unmarshaling SignerRemoved: %w", err)
	}
	return nil
}

func UnmarshalSignerRemoved(buf []byte) (*SignerRemoved, error) {
	obj := new(SignerRemoved)
	err := obj.Unmarshal(buf)
	if err != nil {
		return nil, err
	}
	return obj, nil
}

type Signers struct {
	Signers []Signer `json:"signers"`
}

func (obj Signers) MarshalWithEncoder(encoder *binary.Encoder) (err error) {
	// Serialize `Signers`:
	err = encoder.Encode(obj.Signers)
	if err != nil {
		return errors.NewField("Signers", err)
	}
	return nil
}

func (obj Signers) Marshal() ([]byte, error) {
	buf := bytes.NewBuffer(nil)
	encoder := binary.NewBorshEncoder(buf)
	err := obj.MarshalWithEncoder(encoder)
	if err != nil {
		return nil, fmt.Errorf("error while encoding Signers: %w", err)
	}
	return buf.Bytes(), nil
}

func (obj *Signers) UnmarshalWithDecoder(decoder *binary.Decoder) (err error) {
	// Deserialize `Signers`:
	err = decoder.Decode(&obj.Signers)
	if err != nil {
		return errors.NewField("Signers", err)
	}
	return nil
}

func (obj *Signers) Unmarshal(buf []byte) error {
	err := obj.UnmarshalWithDecoder(binary.NewBorshDecoder(buf))
	if err != nil {
		return fmt.Errorf("error while unmarshaling Signers: %w", err)
	}
	return nil
}

func UnmarshalSigners(buf []byte) (*Signers, error) {
	obj := new(Signers)
	err := obj.Unmarshal(buf)
	if err != nil {
		return nil, err
	}
	return obj, nil
}
