forked from MJKWoolnough/ics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsections_alarm.go
130 lines (115 loc) · 2.6 KB
/
sections_alarm.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package ics
import (
"io"
"github.com/MJKWoolnough/errors"
"github.com/MJKWoolnough/parser"
)
type psuedoTokeniser []parser.Phrase
func (p *psuedoTokeniser) GetPhrase() (parser.Phrase, error) {
if len(*p) == 0 {
return parser.Phrase{
Type: parser.PhraseError,
Data: nil,
}, io.ErrUnexpectedEOF
}
ph := (*p)[0]
*p = (*p)[1:]
return ph, nil
}
// AlarmType is an interface this is fulfilled by AlarmAudio, AlarmDisplay and
// AlarmEmail
type AlarmType interface {
section
Type() string
}
// Alarm is the encompassing type for the three alarm types
type Alarm struct {
AlarmType
}
func (a *Alarm) decode(t tokeniser) error {
var pt psuedoTokeniser
Loop:
for {
ph, err := t.GetPhrase()
if err != nil {
return err
}
pt = append(pt, ph)
switch ph.Data[0].Data {
case "BEGIN":
return ErrInvalidStructure
case "ACTION":
if a.AlarmType != nil {
return ErrInvalidStructure
}
switch ph.Data[len(ph.Data)-1].Data {
case "AUDIO":
a.AlarmType = new(AlarmAudio)
case "DISPLAY":
a.AlarmType = new(AlarmDisplay)
case "EMAIL":
a.AlarmType = new(AlarmEmail)
case "URI":
a.AlarmType = new(AlarmURI)
case "NONE":
a.AlarmType = new(AlarmNone)
}
case "END":
break Loop
}
}
if a.AlarmType == nil {
return ErrMissingAlarmAction
}
return a.AlarmType.decode(&pt)
}
func (a *Alarm) encode(w writer) {
w.WriteString("BEGIN:VALARM\r\n")
switch a.AlarmType.(type) {
case *AlarmAudio:
w.WriteString("ACTION:AUDIO\r\n")
case *AlarmDisplay:
w.WriteString("ACTION:DISPLAY\r\n")
case *AlarmEmail:
w.WriteString("ACTION:EMAIL\r\n")
case *AlarmURI:
w.WriteString("ACTION:URI\r\n")
case *AlarmNone:
w.WriteString("ACTION:NONE\r\n")
}
a.AlarmType.encode(w)
w.WriteString("END:VALARM\r\n")
}
func (a *Alarm) valid() error {
switch a.AlarmType.(type) {
case *AlarmAudio, *AlarmDisplay, *AlarmEmail, *AlarmURI, *AlarmNone:
return a.AlarmType.valid()
}
return ErrInvalidAlarm
}
// Type returns the type of the alarm "AUDIO"
func (AlarmAudio) Type() string {
return "AUDIO"
}
// Type returns the type of the alarm "DISPLAY"
func (AlarmDisplay) Type() string {
return "DISPLAY"
}
// Type returns the type of the alarm "EMAIL"
func (AlarmEmail) Type() string {
return "EMAIL"
}
// Type returns the type of the alarm "URI"
func (AlarmURI) Type() string {
return "URI"
}
// Type returns the type of the alarm "NONE"
func (AlarmNone) Type() string {
return "NONE"
}
// Errors
const (
ErrInvalidStructure errors.Error = "invalid structure"
ErrMissingAlarmAction errors.Error = "missing alarm action"
ErrInvalidAlarm errors.Error = "invalid alarm type"
)