aboutsummaryrefslogtreecommitdiffstats
path: root/common/death/parser.go
blob: e86cffbe735ae20296f9e49410c47961f60cea5f (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
package death

import "strings"

type parser interface {
	parse(msg string) (string, string, bool)
}

type suffixParser struct {
	suffix string
}

func (m suffixParser) parse(msg string) (x, y string, ok bool) {
	if !strings.HasSuffix(msg, m.suffix) {
		return "", "", false
	}

	x = strings.TrimSpace(strings.TrimSuffix(msg, m.suffix))
	if x == "" {
		return "", "", false
	}

	return x, "", true
}

type infixParser struct {
	sep    string
	suffix string
}

func (m infixParser) parse(msg string) (x, y string, ok bool) {
	before, after, found := strings.Cut(msg, m.sep)
	if !found {
		return "", "", false
	}

	x = strings.TrimSpace(before)
	if x == "" {
		return "", "", false
	}

	if m.suffix != "" {
		if !strings.HasSuffix(after, m.suffix) {
			return "", "", false
		}
		after = strings.TrimSuffix(after, m.suffix)
	}

	y = strings.TrimSpace(after)
	if y == "" {
		return "", "", false
	}

	return x, y, true
}