aboutsummaryrefslogtreecommitdiffstats
path: root/common/death/parser.go
diff options
context:
space:
mode:
Diffstat (limited to 'common/death/parser.go')
-rw-r--r--common/death/parser.go55
1 files changed, 55 insertions, 0 deletions
diff --git a/common/death/parser.go b/common/death/parser.go
new file mode 100644
index 0000000..e86cffb
--- /dev/null
+++ b/common/death/parser.go
@@ -0,0 +1,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
+}