Skip to content

Commit c2a2ce6

Browse files
committed
refactor: move dumps to device
1 parent c6b70fb commit c2a2ce6

File tree

17 files changed

+86
-75
lines changed

17 files changed

+86
-75
lines changed

cmd/dump/main.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
"github.com/mishamyrt/nuga-lib"
1010
"github.com/mishamyrt/nuga-lib/device"
11-
"github.com/mishamyrt/nuga-lib/dump"
1211
"github.com/mishamyrt/nuga-lib/features/keys"
1312
"github.com/mishamyrt/nuga-lib/features/light"
1413
"github.com/mishamyrt/nuga-lib/hid"
@@ -33,7 +32,7 @@ var (
3332
Run: func(_ []string) {
3433
dev, err := nuga.Open()
3534
cli.Must("open device", err)
36-
state, err := dump.Collect(dev.Handle, dev.Name)
35+
state, err := nuga.Collect(dev.Handle, dev.Name)
3736
cli.Must("collect device state", err)
3837
data, err := json.Marshal(&state)
3938
cli.Must("marshal device state", err)
@@ -53,7 +52,7 @@ var (
5352
cli.Must("open device", err)
5453
state, err := readStateDump(inputPath)
5554
cli.Must("read state file", err)
56-
cli.Must("restore device state", dump.Restore(d, state))
55+
cli.Must("restore device state", nuga.Restore(d, state))
5756
},
5857
}
5958

@@ -106,14 +105,14 @@ var (
106105
err = json.Unmarshal(data, &originalDump)
107106
cli.Must("unmarshal file", err)
108107

109-
var nugafile dump.State
108+
var nugafile device.State
110109
nugafile.Model = device.Model(originalDump.Name)
111-
nugafile.Data.Lights = &light.StateData{
110+
nugafile.Data.Lights = &device.LightsState{
112111
Colors: originalDump.Lights.Colors[7:1031],
113112
Params: originalDump.Lights.Params[15:138],
114113
CustomEffect: make([]byte, 1024),
115114
}
116-
nugafile.Data.Keys = &keys.StateData{
115+
nugafile.Data.Keys = &device.KeysState{
117116
Mac: keys.UnpackKeyCodes(originalDump.Keys.Mac),
118117
Win: keys.UnpackKeyCodes(originalDump.Keys.Win),
119118
Macros: make([]byte, 1024),
@@ -145,8 +144,8 @@ type jsonDump struct {
145144
}
146145
}
147146

148-
func readStateDump(path string) (*dump.State, error) {
149-
var state dump.State
147+
func readStateDump(path string) (*device.State, error) {
148+
var state device.State
150149
data, err := os.ReadFile(path)
151150
if err != nil {
152151
return nil, err

dump/collect.go renamed to collect.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Package dump provides device state collector
2-
package dump
2+
package nuga
33

44
import (
55
"github.com/mishamyrt/nuga-lib/device"
@@ -8,8 +8,11 @@ import (
88
)
99

1010
// Collect device state
11-
func Collect(dev hid.Handler, model device.Model) (*State, error) {
12-
f := features.New(dev, model)
11+
func Collect(dev hid.Handler, model device.Model) (*device.State, error) {
12+
f, err := features.New(dev, model)
13+
if err != nil {
14+
return nil, err
15+
}
1316
lights, err := f.Light.GetStateData()
1417
if err != nil {
1518
return nil, err
@@ -18,9 +21,9 @@ func Collect(dev hid.Handler, model device.Model) (*State, error) {
1821
if err != nil {
1922
return nil, err
2023
}
21-
return &State{
24+
return &device.State{
2225
Model: model,
23-
Data: features.StateData{
26+
Data: device.StateData{
2427
Lights: lights,
2528
Keys: keys,
2629
},

device.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,15 @@ func Open() (*Device, error) {
4141
}
4242
model := device.Model(handle.Info.Model)
4343
capabilities := GetCapabilities(model)
44-
featuresRepo := features.New(handle, model)
44+
repo, err := features.New(handle, model)
45+
if err != nil {
46+
return nil, err
47+
}
4548
return &Device{
4649
Name: model,
4750
Path: handle.Info.Path,
4851
Firmware: handle.Info.Firmware,
49-
Features: featuresRepo,
52+
Features: repo,
5053
Capabilities: capabilities,
5154
Handle: handle,
5255
}, nil

dump/defaults.go renamed to device/defaults.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
package dump
1+
package device
22

33
import (
44
"embed"
55
"encoding/json"
66
"fmt"
7-
8-
"github.com/mishamyrt/nuga-lib/device"
97
)
108

119
//go:embed all:defaults
1210
var defaults embed.FS
1311

1412
// GetDefaults returns default state for given model
15-
func GetDefaults(model device.Model) (*State, error) {
13+
func GetDefaults(model Model) (*State, error) {
1614
filePath := fmt.Sprintf("defaults/%v.nugafile", string(model))
1715
data, err := defaults.ReadFile(filePath)
1816
if err != nil {
File renamed without changes.
File renamed without changes.
File renamed without changes.

dump/defaults_test.go renamed to device/defaults_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
package dump_test
1+
package device_test
22

33
import (
44
"testing"
55

66
"github.com/mishamyrt/nuga-lib/device"
7-
"github.com/mishamyrt/nuga-lib/dump"
87
)
98

109
func TestGetDefaults(t *testing.T) {
@@ -21,7 +20,7 @@ func TestGetDefaults(t *testing.T) {
2120
// Run tests
2221
for _, test := range tests {
2322
t.Run(string(test.model), func(t *testing.T) {
24-
state, err := dump.GetDefaults(test.model)
23+
state, err := device.GetDefaults(test.model)
2524

2625
if test.expectingError {
2726
if err == nil {

device/state.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package device
2+
3+
// LightsState represents raw lights state.
4+
type LightsState struct {
5+
Colors []byte `json:"colors"`
6+
Params []byte `json:"effects"`
7+
CustomEffect []byte `json:"custom_effect"`
8+
}
9+
10+
// StateData represents raw keys state.
11+
type KeysState struct {
12+
Mac []byte `json:"mac"`
13+
Win []byte `json:"win"`
14+
Macros []byte `json:"macros"`
15+
}
16+
17+
// StateData represents raw state data.
18+
type StateData struct {
19+
Lights *LightsState `json:"lights"`
20+
Keys *KeysState `json:"keys"`
21+
}
22+
23+
// State represents raw device state. It contains data of all supported features
24+
type State struct {
25+
Model Model `json:"model"`
26+
Data StateData `json:"state"`
27+
}

dump/state.go

Lines changed: 0 additions & 12 deletions
This file was deleted.

features/keys/state.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package keys
22

3-
import "github.com/mishamyrt/nuga-lib/layout"
3+
import (
4+
"github.com/mishamyrt/nuga-lib/device"
5+
"github.com/mishamyrt/nuga-lib/layout"
6+
)
47

58
// State represents parsed keys state.
69
type State struct {
@@ -10,29 +13,22 @@ type State struct {
1013
}
1114

1215
// Data returns raw keys state.
13-
func (s *State) Data(tpl *layout.Template) (*StateData, error) {
16+
func (s *State) Data(tpl *layout.Template, defaults *device.KeysState) (*device.KeysState, error) {
1417
macros, err := s.Macros.Bytes()
1518
if err != nil {
1619
return nil, err
1720
}
18-
mac := s.Mac.Bytes(tpl)
19-
win := s.Win.Bytes(tpl)
20-
return &StateData{
21+
mac := s.Mac.Bytes(tpl, defaults.Mac)
22+
win := s.Win.Bytes(tpl, defaults.Win)
23+
return &device.KeysState{
2124
Mac: mac,
2225
Win: win,
2326
Macros: macros,
2427
}, nil
2528
}
2629

27-
// StateData represents raw keys state.
28-
type StateData struct {
29-
Mac []byte `json:"mac"`
30-
Win []byte `json:"win"`
31-
Macros []byte `json:"macros"`
32-
}
33-
3430
// Parse raw state data.
35-
func (s *StateData) Parse(tpl *layout.Template) (*State, error) {
31+
func ParseState(s *device.KeysState, tpl *layout.Template) (*State, error) {
3632
if tpl == nil {
3733
return nil, ErrNoTemplate
3834
}

features/light/feature.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func (f *Feature) SetCustomEffect(colors *CustomEffectMap) error {
131131
}
132132

133133
// GetStateData returns current keyboard light state.
134-
func (f *Feature) GetStateData() (*StateData, error) {
134+
func (f *Feature) GetStateData() (*device.LightsState, error) {
135135
colors, err := f.GetRawBacklightColors()
136136
if err != nil {
137137
return nil, err
@@ -144,15 +144,15 @@ func (f *Feature) GetStateData() (*StateData, error) {
144144
if err != nil {
145145
return nil, err
146146
}
147-
return &StateData{
147+
return &device.LightsState{
148148
Colors: colors,
149149
Params: params,
150150
CustomEffect: customEffect,
151151
}, nil
152152
}
153153

154154
// SetStateData sets keyboard light state.
155-
func (f *Feature) SetStateData(data *StateData) error {
155+
func (f *Feature) SetStateData(data *device.LightsState) error {
156156
if err := f.SetRawBacklightColors(data.Colors); err != nil {
157157
return err
158158
}

features/light/feature_simulation.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ type FeatureSimulation struct {
1212
}
1313

1414
// NewSimulation creates simulated light from template.
15-
func NewSimulation(s *StateData, model device.Model) (*FeatureSimulation, error) {
15+
func NewSimulation(s *device.LightsState, model device.Model) (*FeatureSimulation, error) {
1616
template := layout.GetBacklightTemplate(model)
17-
state, err := s.Parse(template)
17+
state, err := ParseState(s, template)
1818
if err != nil {
1919
return nil, err
2020
}
@@ -64,13 +64,13 @@ func (f *FeatureSimulation) SetCustomEffect(c *CustomEffectMap) error {
6464
}
6565

6666
// GetStateData returns current simulated state.
67-
func (f *FeatureSimulation) GetStateData() (*StateData, error) {
67+
func (f *FeatureSimulation) GetStateData() (*device.LightsState, error) {
6868
return f.state.Data(f.template), nil
6969
}
7070

7171
// SetStateData sets current simulated state.
72-
func (f *FeatureSimulation) SetStateData(s *StateData) error {
73-
state, err := s.Parse(f.template)
72+
func (f *FeatureSimulation) SetStateData(s *device.LightsState) error {
73+
state, err := ParseState(s, f.template)
7474
if err != nil {
7575
return err
7676
}

features/light/state.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package light
22

33
import (
4+
"github.com/mishamyrt/nuga-lib/device"
45
"github.com/mishamyrt/nuga-lib/layout"
56
"github.com/pkg/errors"
67
)
@@ -13,30 +14,23 @@ type State struct {
1314
}
1415

1516
// Data returns raw state data.
16-
func (s *State) Data(tpl *layout.Template) *StateData {
17+
func (s *State) Data(tpl *layout.Template) *device.LightsState {
1718
var customEffect []byte
1819
if tpl != nil {
1920
custom, err := s.CustomEffect.Bytes(tpl)
2021
if err == nil {
2122
customEffect = custom
2223
}
2324
}
24-
return &StateData{
25+
return &device.LightsState{
2526
Colors: s.Colors.Bytes(),
2627
Params: s.Effects.Bytes(),
2728
CustomEffect: customEffect,
2829
}
2930
}
3031

31-
// StateData represents raw lights state.
32-
type StateData struct {
33-
Colors []byte `json:"colors"`
34-
Params []byte `json:"effects"`
35-
CustomEffect []byte `json:"custom_effect"`
36-
}
37-
3832
// Parse raw state data.
39-
func (s *StateData) Parse(tpl *layout.Template) (*State, error) {
33+
func ParseState(s *device.LightsState, tpl *layout.Template) (*State, error) {
4034
colors, err := ParseBacklightColors(s.Colors)
4135
if err != nil {
4236
return nil, errors.Wrap(err, "colors")

dump/restore.go renamed to restore.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
package dump
1+
package nuga
22

33
import (
4+
"github.com/mishamyrt/nuga-lib/device"
45
"github.com/mishamyrt/nuga-lib/features"
56
"github.com/mishamyrt/nuga-lib/hid"
67
)
78

89
// Restore device state
9-
func Restore(dev hid.Handler, s *State) error {
10-
f := features.New(dev, s.Model)
10+
func Restore(dev hid.Handler, s *device.State) error {
11+
f, err := features.New(dev, s.Model)
12+
if err != nil {
13+
return err
14+
}
1115
if err := f.Light.SetStateData(s.Data.Lights); err != nil {
1216
return err
1317
}

simulation.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package nuga
22

33
import (
4-
"github.com/mishamyrt/nuga-lib/dump"
4+
"github.com/mishamyrt/nuga-lib/device"
55
"github.com/mishamyrt/nuga-lib/features"
66
)
77

@@ -11,8 +11,8 @@ const (
1111
)
1212

1313
// FromTemplate creates simulated keyboard
14-
func FromTemplate(t *dump.State) (*Device, error) {
15-
f, err := features.NewSimulation(&t.Data, t.Model)
14+
func FromTemplate(t *device.State) (*Device, error) {
15+
f, err := features.NewSimulation(t)
1616
if err != nil {
1717
return nil, err
1818
}

simulation_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ import (
88
"testing"
99

1010
"github.com/mishamyrt/nuga-lib"
11-
"github.com/mishamyrt/nuga-lib/dump"
11+
"github.com/mishamyrt/nuga-lib/device"
1212
"github.com/mishamyrt/nuga-lib/features/keys"
1313
)
1414

15-
func readTemplate(model string) (*dump.State, error) {
16-
path := fmt.Sprintf("dump/defaults/%v.nugafile", model)
15+
func readTemplate(model string) (*device.State, error) {
16+
path := fmt.Sprintf("device/defaults/%v.nugafile", model)
1717
content, err := os.ReadFile(path)
1818
if err != nil {
1919
return nil, err
2020
}
21-
var template dump.State
21+
var template device.State
2222
err = json.Unmarshal(content, &template)
2323
if err != nil {
2424
return nil, err

0 commit comments

Comments
 (0)