~nromdotcom/gemif

ref: 077db0c242b79e9291028436c3e4d8fe8c8799d8 gemif/pkg/gamemanager/engineconfig.go -rw-r--r-- 1.1 KiB
077db0c2Norm MacLennan Update ASCIInema for new version 1 year, 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package gamemanager

import (
	"errors"
	"fmt"
)

// EngineConfig contains settings information for driving the engine.
type EngineConfig struct {
	RenderDescriptions bool
	StateTokenFormat   SerializationFormat
}

// SerializationFormat determines which format StateTokens are serialized as.
type SerializationFormat int

const (
	json SerializationFormat = iota + 1
	protobuf
)

func getSerDeFormat(format string) SerializationFormat {
	serializationFormats := map[string]SerializationFormat{
		"json":  json,
		"proto": protobuf,
	}

	return serializationFormats[format]
}

// ErrInvalidSerializationFormat occurs when engine config specifies a unsupported format.
var ErrInvalidSerializationFormat = errors.New("invalid serialization format")

// NewEngineConfig news up an EngineConfig based on passed in values.
func NewEngineConfig(renderDesc bool, tokenFormat string) (EngineConfig, error) {
	serdeFormat := getSerDeFormat(tokenFormat)
	if serdeFormat == 0 {
		return EngineConfig{}, fmt.Errorf("%w: %s", ErrInvalidSerializationFormat, tokenFormat)
	}

	return EngineConfig{
		RenderDescriptions: renderDesc,
		StateTokenFormat:   serdeFormat,
	}, nil
}