...
1 package devices
2
3 import (
4 "github.com/gobwas/glob"
5 )
6
7
8 type ruleType int8
9
10 const (
11
12 attributeRule ruleType = iota
13
14 propertyRule
15 )
16
17
18 type Rule struct {
19 name string
20 ruleType
21 glob glob.Glob
22 subsystem string
23 }
24
25
26 func NewPropRule(property string, value string) *Rule {
27 rule := Rule{
28 name: property,
29 ruleType: propertyRule,
30 }
31 if property == "SUBSYSTEM" {
32 rule.subsystem = value
33 }
34 g, err := glob.Compile(value)
35 if err == nil {
36 rule.glob = g
37 }
38 return &rule
39 }
40
41
42 func NewAttrRule(attribute string, value string) *Rule {
43 rule := Rule{
44 name: attribute,
45 ruleType: attributeRule,
46 }
47 g, err := glob.Compile(value)
48 if err == nil {
49 rule.glob = g
50 }
51 return &rule
52 }
53
54
55 func (r *Rule) isAttributeEqual(device Device) bool {
56 attrValue, found, _ := device.Attribute(r.name)
57 if !found {
58 return false
59 }
60 return r.glob.Match(attrValue)
61 }
62
63
64 func (r *Rule) isPropertyEqual(device Device) bool {
65 propValue, found, _ := device.Property(r.name)
66 if !found {
67 return false
68 }
69 return r.glob.Match(propValue)
70 }
71
View as plain text