aboutsummaryrefslogtreecommitdiff
path: root/plugin/pkg/response/classify.go
blob: 2e705cb0bde6e0673ee3f6d79c5752e9d51044be (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
package response

import "fmt"

// Class holds sets of Types
type Class int

const (
	// All is a meta class encompassing all the classes.
	All Class = iota
	// Success is a class for a successful response.
	Success
	// Denial is a class for denying existence (NXDOMAIN, or a nodata: type does not exist)
	Denial
	// Error is a class for errors, right now defined as not Success and not Denial
	Error
)

func (c Class) String() string {
	switch c {
	case All:
		return "all"
	case Success:
		return "success"
	case Denial:
		return "denial"
	case Error:
		return "error"
	}
	return ""
}

// ClassFromString returns the class from the string s. If not class matches
// the All class and an error are returned
func ClassFromString(s string) (Class, error) {
	switch s {
	case "all":
		return All, nil
	case "success":
		return Success, nil
	case "denial":
		return Denial, nil
	case "error":
		return Error, nil
	}
	return All, fmt.Errorf("invalid Class: %s", s)
}

// Classify classifies the Type t, it returns its Class.
func Classify(t Type) Class {
	switch t {
	case NoError, Delegation:
		return Success
	case NameError, NoData:
		return Denial
	case OtherError:
		fallthrough
	default:
		return Error
	}
}