aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/shell/state.go
blob: 1eed62f2942c18f0f34dffcf0cbb3f76f884e93b (plain) (blame)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package shell

import (
	"bytes"
	"fmt"
	"strings"

	"github.com/jpappel/atlas/pkg/index"
	"github.com/jpappel/atlas/pkg/query"
)

type ValueType int

const (
	VAL_INVALID ValueType = iota
	VAL_INT
	VAL_STRING
	VAL_TOKENS
	VAL_CLAUSE
	VAL_ARTIFACT
	VAL_RESULTS
)

type Value struct {
	Type ValueType
	Val  any
}

type State map[string]Value

func (t ValueType) String() string {
	switch t {
	case VAL_INVALID:
		return "Invalid"
	case VAL_INT:
		return "Integer"
	case VAL_STRING:
		return "String"
	case VAL_TOKENS:
		return "Tokens"
	case VAL_CLAUSE:
		return "Clause"
	case VAL_ARTIFACT:
		return "Compilation Artifact"
	case VAL_RESULTS:
		return "Query Result"
	default:
		return "Unkown"
	}
}

func (v Value) String() string {
	switch v.Type {
	case VAL_INT:
		i, ok := v.Val.(int)
		if !ok {
			panic("Corrupted Type (expected int)")
		}
		return fmt.Sprint(i)
	case VAL_STRING:
		s, ok := v.Val.(string)
		if !ok {
			panic("Corrupted Type (expected string)")
		}
		return s
	case VAL_TOKENS:
		ts, ok := v.Val.([]query.Token)
		if !ok {
			panic("Corrupted Type (expected []query.Token)")
		}
		return query.TokensStringify(ts)
	case VAL_CLAUSE:
		clause, ok := v.Val.(*query.Clause)
		if !ok {
			panic("Corrupted Type (expected *query.Clause)")
		}
		return clause.String()
	case VAL_ARTIFACT:
		artifact, ok := v.Val.(query.CompilationArtifact)
		if !ok {
			panic("Corrupted Type (expected query.CompilationArtifact)")
		}
		return artifact.String()
	case VAL_RESULTS:
		results, ok := v.Val.([]*index.Document)
		if !ok {
			panic("Corrupted Type (expected []*index.Document)")
		}

		b := bytes.Buffer{}
		yo := query.YamlOutput{}
		if _, err := yo.OutputTo(&b, results); err != nil {
			panic(err)
		}
		return b.String()
	case VAL_INVALID:
		return "Invalid"
	}
	return fmt.Sprintf("Unknown @ %p", v.Val)
}

func (s State) String() string {
	b := strings.Builder{}

	for k, v := range s {
		b.WriteString(k)
		b.WriteByte(':')
		b.WriteByte(' ')
		switch v.Type {
		case VAL_INVALID:
			b.WriteString("Invalid")
		case VAL_INT:
			b.WriteString("Integer")
		case VAL_STRING:
			b.WriteString("String")
		case VAL_TOKENS:
			b.WriteString("Tokens")
		case VAL_CLAUSE:
			b.WriteString("Clause")
		case VAL_ARTIFACT:
			b.WriteString("Artifact")
		case VAL_RESULTS:
			b.WriteString("Results")
		default:
			fmt.Fprintf(&b, "Unknown (%d)", v.Val)
		}
		b.WriteByte('\n')
	}

	return b.String()
}