func Encode(writer io.Writer, cast *Cast) (err error) {
	if writer == nil {
		err = errors.Errorf("a writer must be specified")
		return
	}
	if cast == nil {
		err = errors.Errorf("a cast must be specified")
		return
	}
	encoder := json.NewEncoder(writer)
	encoder.SetIndent("", "")
	err = encoder.Encode(&cast.Header)
	if err != nil {
		err = errors.Wrapf(err,
			"failed to encode header")
		return
	}
	for _, ev := range cast.EventStream {
		err = encoder.Encode([]interface{}{ev.Time, ev.Type, ev.Data})
		if err != nil {
			err = errors.Wrapf(err,
				"failed to encode event")
			return
		}
	}
	return
}
Encode writes the encoding of `Cast` into the writer passed as an argument.

ps.: this method **will not** validate whether the cast is a valid V2
cast or not. Make sure you call `Validate` before.
package cast
import (
	"encoding/json"
	"io"
	"github.com/pkg/errors"
)
type Header struct {
	Version uint8 `json:"version"`
	Width uint `json:"width"`
	Height uint `json:"height"`
	Timestamp uint `json:"timestamp,omitempty"`
	Command string `json:"command,omitempty"`
	Theme struct {
		Fg string `json:"fg,omitempty"`
		Bg string `json:"bg,omitempty"`
		Palette string `json:"palette,omitempty"`
	} `json:"theme,omitempty"`
	Title string `json:"title,omitempty"`
	IdleTimeLimit float64 `json:"idle_time_limit,omitempty"`
	Env struct {
		Shell string `json:"SHELL,omitempty"`
		Term string `json:"TERM,omitempty"`
	} `json:"env,omitempty"`
}
type Event struct {
	Time float64
	Type string
	Data string
}
type Cast struct {
	Header Header
	EventStream []*Event
}
func ValidateHeader(header *Header) (isValid bool, err error) {
	if header == nil {
		err = errors.Errorf("header must not be nil")
		return
	}
	if header.Version != 2 {
		err = errors.Errorf("only casts with version 2 are valid")
		return
	}
	if header.Width == 0 {
		err = errors.Errorf("a valid width (>0) must be specified")
		return
	}
	if header.Height == 0 {
		err = errors.Errorf("a valid height (>0) must be specified")
		return
	}
	isValid = true
	return
}
func ValidateEvent(event *Event) (isValid bool, err error) {
	if event == nil {
		err = errors.Errorf("event must not be nil")
		return
	}
	if event.Type != "i" && event.Type != "o" {
		err = errors.Errorf("type must either be `o` or `i`")
		return
	}
	isValid = true
	return
}
func ValidateEventStream(eventStream []*Event) (isValid bool, err error) {
	var (
		lastTime float64
		ev       *Event
	)
	for _, ev = range eventStream {
		if ev.Time < lastTime {
			err = errors.Errorf("events must be ordered by time")
			return
		}
		_, err = ValidateEvent(ev)
		if err != nil {
			err = errors.Wrapf(err, "invalid event")
			return
		}
		lastTime = ev.Time
	}
	isValid = true
	return
}
func Validate(cast *Cast) (isValid bool, err error) {
	if cast == nil {
		err = errors.Errorf("cast must not be nil")
		return
	}
	_, err = ValidateHeader(&cast.Header)
	if err != nil {
		err = errors.Wrapf(err, "invalid header")
		return
	}
	_, err = ValidateEventStream(cast.EventStream)
	if err != nil {
		err = errors.Wrapf(err, "invalid event stream")
		return
	}
	isValid = true
	return
}