util.go 1.32 KB
Newer Older
Eric Myhre's avatar
Eric Myhre committed
1 2 3 4 5 6
package jst

import (
	"fmt"
	"io"

tavit ohanian's avatar
tavit ohanian committed
7
	ld "gitlab.dms3.io/ld/go-ld-prime"
Eric Myhre's avatar
Eric Myhre committed
8 9 10 11 12 13 14 15 16
)

func max(a, b int) int { // honestly golang
	if a > b {
		return a
	}
	return b
}

tavit ohanian's avatar
tavit ohanian committed
17
func mustFirstKeyAsString(mapNode ld.Node) string {
Eric Myhre's avatar
Eric Myhre committed
18 19 20 21 22 23 24 25 26 27 28 29
	itr := mapNode.MapIterator()
	k, _, err := itr.Next()
	if err != nil {
		panic(err)
	}
	ks, err := k.AsString()
	if err != nil {
		panic(err)
	}
	return ks
}

30 31 32 33 34 35 36 37 38
func indexOf(list []columnName, cn columnName) int {
	for i, v := range list {
		if v == cn {
			return i
		}
	}
	return -1
}

Eric Myhre's avatar
Eric Myhre committed
39
type codecAborted struct {
tavit ohanian's avatar
tavit ohanian committed
40
	p ld.Path
Eric Myhre's avatar
Eric Myhre committed
41 42 43 44
	e error
}

func (e codecAborted) Error() string {
45
	return fmt.Sprintf("codec aborted: %s", e.e)
Eric Myhre's avatar
Eric Myhre committed
46 47 48 49 50 51 52
}

// attempt to record where the codec efforts encountered an error.
// this is currently a bit slapdash.
// a better system might also count byte and line offsets in the serial form (but this would require also integrating with the serial code pretty closely).
// a spectacularly general system might be ready to count serial offsets *twice* *or* path offsets *twice* (but this is simply waiting for someone's enthusiasm).
func recordErrorPosition(ctx *state, e error) error {
tavit ohanian's avatar
tavit ohanian committed
53
	return codecAborted{ld.Path{ /*TODO*/ }, e}
Eric Myhre's avatar
Eric Myhre committed
54 55 56 57 58 59 60 61 62
}

// not yet used, but you'd probably want this for better error position purposes.
type writerNanny struct {
	totalOffset  int
	lineOffset   int
	offsetInLine int
	w            io.Writer
}