package devices import ( "github.com/gobwas/glob" ) // ruleType is the type of rule (attribute or property) type ruleType int8 const ( // match by attribute attributeRule ruleType = iota // match by property propertyRule ) // Rule is a property/attribute rule to match by type Rule struct { name string ruleType glob glob.Glob subsystem string } // NewPropRule will return a new property rule func NewPropRule(property string, value string) *Rule { rule := Rule{ name: property, ruleType: propertyRule, } if property == "SUBSYSTEM" { rule.subsystem = value } g, err := glob.Compile(value) if err == nil { rule.glob = g } return &rule } // NewAttributeRule will return a new attribute rule func NewAttrRule(attribute string, value string) *Rule { rule := Rule{ name: attribute, ruleType: attributeRule, } g, err := glob.Compile(value) if err == nil { rule.glob = g } return &rule } // isAttributeEqual attempts to find and match the device attribute func (r *Rule) isAttributeEqual(device Device) bool { attrValue, found, _ := device.Attribute(r.name) if !found { return false } return r.glob.Match(attrValue) } // isPropertyEqual attempts to find and match the device property func (r *Rule) isPropertyEqual(device Device) bool { propValue, found, _ := device.Property(r.name) if !found { return false } return r.glob.Match(propValue) }