1package playlistfs
2
3import (
4 "fmt"
5 "strconv"
6)
7
8type Volume struct {
9 Levels []uint
10}
11
12func VolCmd(buf []byte) (*Volume, error) {
13 var vol Volume
14
15 fields, err := parseFields(buf, 2, -1)
16 if err != nil {
17 return nil, err
18 }
19
20 for _, field := range fields {
21 for i := 0; i < len(field); i++ {
22 data := field[i]
23 switch i {
24 case 0:
25 if data != "volume" {
26 return nil, ErrNoVol
27 }
28 default:
29 lvl, err := strconv.ParseUint(field[i], 10, 8)
30 if err != nil {
31 return nil, err
32 } else if lvl > 100 {
33 return nil, ErrInvalidVol
34 }
35
36 vol.Levels = append(vol.Levels, uint(lvl))
37 }
38 }
39 }
40
41 return &vol, nil
42}
43
44func (v *Volume) String() string {
45 numLvls := len(v.Levels)
46 if numLvls == 1 {
47 return fmt.Sprintf("volume %d", v.Levels[0])
48 }
49
50 var lvlSet string
51 for i, lvl := range v.Levels {
52 lvlSet += fmt.Sprintf("%d", lvl)
53 if i != numLvls {
54 lvlSet += " "
55 }
56 }
57
58 return fmt.Sprintf("volume '%s'", lvlSet)
59}