-
Notifications
You must be signed in to change notification settings - Fork 0
/
id.go
48 lines (36 loc) · 946 Bytes
/
id.go
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
package insteon
import (
"encoding/hex"
"fmt"
)
// ID represents a device ID.
type ID [3]byte
func (i ID) String() string {
return hex.EncodeToString(i[:])
}
// AsGroup returns the ID as a group. Result is only meaningful if the identity
// is the target of a broadcast message.
func (i ID) AsGroup() Group {
return Group(i[2])
}
// UnmarshalText implements text unmarshalling.
func (i *ID) UnmarshalText(b []byte) error {
data, err := hex.DecodeString(string(b))
if err != nil {
return fmt.Errorf("failed to hex-decode string: %s", err)
}
if len(data) != 3 {
return fmt.Errorf("invalid size for identity: expected 3 but got %d byte(s)", len(data))
}
copy((*i)[:], data)
return nil
}
// MarshalText implements text marshaling.
func (i ID) MarshalText() ([]byte, error) {
return []byte(i.String()), nil
}
// ParseID parses an ID.
func ParseID(s string) (id ID, err error) {
err = id.UnmarshalText([]byte(s))
return
}